comparison 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
comparison
equal deleted inserted replaced
3160:94935895e6d7 3161:6a44a89ffb51
13 13
14 package soap 14 package soap
15 15
16 import ( 16 import (
17 "bytes" 17 "bytes"
18 "context"
18 "crypto/tls" 19 "crypto/tls"
19 "encoding/xml" 20 "encoding/xml"
20 "fmt" 21 "fmt"
21 "io/ioutil" 22 "io/ioutil"
22 "log" 23 "log"
23 "math/rand" 24 "math/rand"
24 "net" 25 "net"
25 "net/http" 26 "net/http"
27 "sync"
26 "time" 28 "time"
29
30 "gemma.intevation.de/gemma/pkg/config"
27 ) 31 )
28 32
29 const timeout = time.Duration(30 * time.Second) 33 const timeout = time.Duration(30 * time.Second)
30 34
31 func dialTimeout(network, addr string) (net.Conn, error) { 35 func dialTimeout(network, addr string) (net.Conn, error) {
270 Dial: dialTimeout, 274 Dial: dialTimeout,
271 } 275 }
272 276
273 client := &http.Client{Transport: tr} 277 client := &http.Client{Transport: tr}
274 278
279 timeout := config.SOAPTimeout()
280
281 ctx, cancel := context.WithTimeout(context.Background(), timeout)
282
283 var once sync.Once
284
285 defer once.Do(cancel)
286
287 req = req.WithContext(ctx)
288
289 go func() {
290 defer once.Do(cancel)
291 <-ctx.Done()
292 }()
293
275 res, err := client.Do(req) 294 res, err := client.Do(req)
276 if err != nil { 295 if err != nil {
277 return err 296 return err
278 } 297 }
279 if res.StatusCode < http.StatusOK || res.StatusCode > 299 { 298 if res.StatusCode < http.StatusOK || res.StatusCode > 299 {