Mercurial > gemma
diff pkg/soap/soap.go @ 3161:6a44a89ffb51
SOAP: Added a globally configurable timeout (default 1min) till a SOAP request is canceled.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 06 May 2019 13:19:59 +0200 |
parents | 7ee9bdaac336 |
children | bd1385c00b59 |
line wrap: on
line diff
--- a/pkg/soap/soap.go Mon May 06 13:05:49 2019 +0200 +++ b/pkg/soap/soap.go Mon May 06 13:19:59 2019 +0200 @@ -15,6 +15,7 @@ import ( "bytes" + "context" "crypto/tls" "encoding/xml" "fmt" @@ -23,7 +24,10 @@ "math/rand" "net" "net/http" + "sync" "time" + + "gemma.intevation.de/gemma/pkg/config" ) const timeout = time.Duration(30 * time.Second) @@ -272,6 +276,21 @@ client := &http.Client{Transport: tr} + timeout := config.SOAPTimeout() + + ctx, cancel := context.WithTimeout(context.Background(), timeout) + + var once sync.Once + + defer once.Do(cancel) + + req = req.WithContext(ctx) + + go func() { + defer once.Do(cancel) + <-ctx.Done() + }() + res, err := client.Do(req) if err != nil { return err