Skip to content

Commit 4d381d7

Browse files
authored
transport/http: don't reuse a base transport between clients (#532)
Previously sync.Once was used to cache a fallback base transport. This caused a race condition when setting MaxIdleConnsPerHost and TLSClientConfig in defaultBaseTransport (above). Because of these settings, it isn't valid to share the base transport across clients. I conferred with @neild and determined that it is impossible to implement a transport.Clone for < Go 1.13, so instead we should initialize a new transport with default values each time in this case. Fixes googleapis/google-cloud-go#2405
1 parent 9a7bb2b commit 4d381d7

1 file changed

Lines changed: 13 additions & 22 deletions

File tree

transport/http/dial.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"net/url"
1717
"os"
1818
"strings"
19-
"sync"
2019
"time"
2120

2221
"go.opencensus.io/plugin/ochttp"
@@ -191,31 +190,23 @@ func defaultBaseTransport(ctx context.Context, clientCertSource cert.Source) htt
191190
return trans
192191
}
193192

194-
var fallback struct {
195-
*http.Transport
196-
sync.Once
197-
}
198-
199193
// fallbackBaseTransport is used in <go1.13 as well as in the rare case if
200194
// http.DefaultTransport has been reassigned something that's not a
201195
// *http.Transport.
202196
func fallbackBaseTransport() *http.Transport {
203-
fallback.Do(func() {
204-
fallback.Transport = &http.Transport{
205-
Proxy: http.ProxyFromEnvironment,
206-
DialContext: (&net.Dialer{
207-
Timeout: 30 * time.Second,
208-
KeepAlive: 30 * time.Second,
209-
DualStack: true,
210-
}).DialContext,
211-
MaxIdleConns: 100,
212-
MaxIdleConnsPerHost: 100,
213-
IdleConnTimeout: 90 * time.Second,
214-
TLSHandshakeTimeout: 10 * time.Second,
215-
ExpectContinueTimeout: 1 * time.Second,
216-
}
217-
})
218-
return fallback.Transport
197+
return &http.Transport{
198+
Proxy: http.ProxyFromEnvironment,
199+
DialContext: (&net.Dialer{
200+
Timeout: 30 * time.Second,
201+
KeepAlive: 30 * time.Second,
202+
DualStack: true,
203+
}).DialContext,
204+
MaxIdleConns: 100,
205+
MaxIdleConnsPerHost: 100,
206+
IdleConnTimeout: 90 * time.Second,
207+
TLSHandshakeTimeout: 10 * time.Second,
208+
ExpectContinueTimeout: 1 * time.Second,
209+
}
219210
}
220211

221212
func addOCTransport(trans http.RoundTripper, settings *internal.DialSettings) http.RoundTripper {

0 commit comments

Comments
 (0)