@@ -13,7 +13,7 @@ import (
13
13
// DefaultTransport returns a new http.Transport with similar default values to
14
14
// http.DefaultTransport, but with idle connections and keepalives disabled.
15
15
func DefaultTransport () * http.Transport {
16
- transport := DefaultPooledTransport ( )
16
+ transport := DefaultPooledTransportWithMin ( 0 )
17
17
transport .DisableKeepAlives = true
18
18
transport .MaxIdleConnsPerHost = - 1
19
19
return transport
@@ -24,6 +24,14 @@ func DefaultTransport() *http.Transport {
24
24
// it can leak file descriptors over time. Only use this for transports that
25
25
// will be re-used for the same host(s).
26
26
func DefaultPooledTransport () * http.Transport {
27
+ return DefaultPooledTransportWithMin (0 )
28
+ }
29
+
30
+ // DefaultPooledTransportWithMin returns a new http.Transport with a minimum
31
+ // value for MaxIdleConnsPerHost. Do not use this for transient transports as
32
+ // it can leak file descriptors over time. Only use this for transports that
33
+ // will be re-used for the same host(s).
34
+ func DefaultPooledTransportWithMin (minIdleConnsPerHost int ) * http.Transport {
27
35
transport := & http.Transport {
28
36
Proxy : http .ProxyFromEnvironment ,
29
37
DialContext : (& net.Dialer {
@@ -36,7 +44,13 @@ func DefaultPooledTransport() *http.Transport {
36
44
TLSHandshakeTimeout : 10 * time .Second ,
37
45
ExpectContinueTimeout : 1 * time .Second ,
38
46
ForceAttemptHTTP2 : true ,
39
- MaxIdleConnsPerHost : runtime .GOMAXPROCS (0 ) + 1 ,
47
+ MaxIdleConnsPerHost : func () int {
48
+ maxIdleConnsPerHost := runtime .GOMAXPROCS (0 ) + 1
49
+ if maxIdleConnsPerHost < minIdleConnsPerHost {
50
+ return minIdleConnsPerHost
51
+ }
52
+ return maxIdleConnsPerHost
53
+ }(),
40
54
}
41
55
return transport
42
56
}
@@ -55,7 +69,15 @@ func DefaultClient() *http.Client {
55
69
// transient clients as it can leak file descriptors over time. Only use this
56
70
// for clients that will be re-used for the same host(s).
57
71
func DefaultPooledClient () * http.Client {
72
+ return DefaultPooledClientWithMin (0 )
73
+ }
74
+
75
+ // DefaultPooledClient returns a new http.Client with similar default values to
76
+ // http.Client, but with a shared Transport and minimum value for MaxIdleConnsPerHost.
77
+ // Do not use this function for transient clients as it can leak file descriptors
78
+ // over time. Only use this for clients that will be re-used for the same host(s).
79
+ func DefaultPooledClientWithMin (minIdleConnsPerHost int ) * http.Client {
58
80
return & http.Client {
59
- Transport : DefaultPooledTransport ( ),
81
+ Transport : DefaultPooledTransportWithMin ( minIdleConnsPerHost ),
60
82
}
61
83
}
0 commit comments