9005: Copy default transport if possible. Move magics to consts.
authorTom Clegg <tom@curoverse.com>
Wed, 31 May 2017 19:03:10 +0000 (15:03 -0400)
committerTom Clegg <tom@curoverse.com>
Wed, 31 May 2017 19:03:10 +0000 (15:03 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curoverse.com>

sdk/go/keepclient/keepclient.go
services/keepproxy/keepproxy.go

index b886684c348aed30abd0aa6ccaeb16a24af2aad7..76ea17517fc6d2b59e0c96e2a33fb3561fcd38df 100644 (file)
@@ -23,6 +23,18 @@ import (
 // A Keep "block" is 64MB.
 const BLOCKSIZE = 64 * 1024 * 1024
 
+var (
+       DefaultRequestTimeout      = 20 * time.Second
+       DefaultConnectTimeout      = 2 * time.Second
+       DefaultTLSHandshakeTimeout = 4 * time.Second
+       DefaultKeepAlive           = 180 * time.Second
+
+       DefaultProxyRequestTimeout      = 300 * time.Second
+       DefaultProxyConnectTimeout      = 30 * time.Second
+       DefaultProxyTLSHandshakeTimeout = 10 * time.Second
+       DefaultProxyKeepAlive           = 120 * time.Second
+)
+
 // Error interface with an error and boolean indicating whether the error is temporary
 type Error interface {
        error
@@ -452,34 +464,44 @@ func (kc *KeepClient) httpClient() HTTPClient {
                return c
        }
 
-       var requestTimeout, connectTimeout, keepAliveInterval, tlsTimeout time.Duration
+       var requestTimeout, connectTimeout, keepAlive, tlsTimeout time.Duration
        if kc.foundNonDiskSvc {
                // Use longer timeouts when connecting to a proxy,
                // because this usually means the intervening network
                // is slower.
-               requestTimeout = 300 * time.Second
-               connectTimeout = 30 * time.Second
-               tlsTimeout = 10 * time.Second
-               keepAliveInterval = 120 * time.Second
+               requestTimeout = DefaultProxyRequestTimeout
+               connectTimeout = DefaultProxyConnectTimeout
+               tlsTimeout = DefaultProxyTLSHandshakeTimeout
+               keepAlive = DefaultProxyKeepAlive
        } else {
-               requestTimeout = 20 * time.Second
-               connectTimeout = 2 * time.Second
-               tlsTimeout = 4 * time.Second
-               keepAliveInterval = 180 * time.Second
-       }
-       transport := &http.Transport{
-               Dial: (&net.Dialer{
-                       Timeout:   connectTimeout,
-                       KeepAlive: keepAliveInterval,
-               }).Dial,
-               TLSClientConfig:     arvadosclient.MakeTLSConfig(kc.Arvados.ApiInsecure),
-               TLSHandshakeTimeout: tlsTimeout,
+               requestTimeout = DefaultRequestTimeout
+               connectTimeout = DefaultConnectTimeout
+               tlsTimeout = DefaultTLSHandshakeTimeout
+               keepAlive = DefaultKeepAlive
        }
-       go func() {
-               for range time.NewTicker(10 * time.Minute).C {
-                       transport.CloseIdleConnections()
+
+       transport, ok := http.DefaultTransport.(*http.Transport)
+       if ok {
+               copy := *transport
+               transport = &copy
+       } else {
+               // Evidently the application has replaced
+               // http.DefaultTransport with a different type, so we
+               // need to build our own from scratch using the Go 1.8
+               // defaults.
+               transport = &http.Transport{
+                       MaxIdleConns:          100,
+                       IdleConnTimeout:       90 * time.Second,
+                       ExpectContinueTimeout: time.Second,
                }
-       }()
+       }
+       transport.DialContext = (&net.Dialer{
+               Timeout:   connectTimeout,
+               KeepAlive: keepAlive,
+               DualStack: true,
+       }).DialContext
+       transport.TLSHandshakeTimeout = tlsTimeout
+       transport.TLSClientConfig = arvadosclient.MakeTLSConfig(kc.Arvados.ApiInsecure)
        c := &http.Client{
                Timeout:   requestTimeout,
                Transport: transport,
index 604e93c0fee8de8314e5f69e14fafa3a43b1dc00..44ecae451f87110452c0d4d44b6b796696f2af33 100644 (file)
@@ -248,18 +248,21 @@ type proxyHandler struct {
 // requests to the appropriate handlers.
 func MakeRESTRouter(enable_get bool, enable_put bool, kc *keepclient.KeepClient, timeout time.Duration) http.Handler {
        rest := mux.NewRouter()
+
+       transport := *(http.DefaultTransport.(*http.Transport))
+       transport.DialContext = (&net.Dialer{
+               Timeout:   keepclient.DefaultConnectTimeout,
+               KeepAlive: keepclient.DefaultKeepAlive,
+               DualStack: true,
+       }).DialContext
+       transport.TLSClientConfig = arvadosclient.MakeTLSConfig(kc.Arvados.ApiInsecure)
+       transport.TLSHandshakeTimeout = keepclient.DefaultTLSHandshakeTimeout
+
        h := &proxyHandler{
                Handler:    rest,
                KeepClient: kc,
                timeout:    timeout,
-               transport: &http.Transport{
-                       Dial: (&net.Dialer{
-                               Timeout:   20 * time.Second,
-                               KeepAlive: 10 * time.Second,
-                       }).Dial,
-                       TLSClientConfig:     arvadosclient.MakeTLSConfig(kc.Arvados.ApiInsecure),
-                       TLSHandshakeTimeout: 10 * time.Second,
-               },
+               transport:  &transport,
                ApiTokenCache: &ApiTokenCache{
                        tokens:     make(map[string]int64),
                        expireTime: 300,