@@ -47,6 +47,9 @@ var DefaultHTTPClientConfig = HTTPClientConfig{
47
47
var defaultHTTPClientOptions = httpClientOptions {
48
48
keepAlivesEnabled : true ,
49
49
http2Enabled : true ,
50
+ // 5 minutes is typically above the maximum sane scrape interval. So we can
51
+ // use keepalive for all configurations.
52
+ idleConnTimeout : 5 * time .Minute ,
50
53
}
51
54
52
55
type closeIdler interface {
@@ -283,6 +286,7 @@ type httpClientOptions struct {
283
286
dialContextFunc DialContextFunc
284
287
keepAlivesEnabled bool
285
288
http2Enabled bool
289
+ idleConnTimeout time.Duration
286
290
}
287
291
288
292
// HTTPClientOption defines an option that can be applied to the HTTP client.
@@ -309,6 +313,13 @@ func WithHTTP2Disabled() HTTPClientOption {
309
313
}
310
314
}
311
315
316
+ // WithIdleConnTimeout allows setting the idle connection timeout.
317
+ func WithIdleConnTimeout (timeout time.Duration ) HTTPClientOption {
318
+ return func (opts * httpClientOptions ) {
319
+ opts .idleConnTimeout = timeout
320
+ }
321
+ }
322
+
312
323
// NewClient returns a http.Client using the specified http.RoundTripper.
313
324
func newClient (rt http.RoundTripper ) * http.Client {
314
325
return & http.Client {Transport : rt }
@@ -357,15 +368,13 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
357
368
// The only timeout we care about is the configured scrape timeout.
358
369
// It is applied on request. So we leave out any timings here.
359
370
var rt http.RoundTripper = & http.Transport {
360
- Proxy : http .ProxyURL (cfg .ProxyURL .URL ),
361
- MaxIdleConns : 20000 ,
362
- MaxIdleConnsPerHost : 1000 , // see https://github.com/golang/go/issues/13801
363
- DisableKeepAlives : ! opts .keepAlivesEnabled ,
364
- TLSClientConfig : tlsConfig ,
365
- DisableCompression : true ,
366
- // 5 minutes is typically above the maximum sane scrape interval. So we can
367
- // use keepalive for all configurations.
368
- IdleConnTimeout : 5 * time .Minute ,
371
+ Proxy : http .ProxyURL (cfg .ProxyURL .URL ),
372
+ MaxIdleConns : 20000 ,
373
+ MaxIdleConnsPerHost : 1000 , // see https://github.com/golang/go/issues/13801
374
+ DisableKeepAlives : ! opts .keepAlivesEnabled ,
375
+ TLSClientConfig : tlsConfig ,
376
+ DisableCompression : true ,
377
+ IdleConnTimeout : opts .idleConnTimeout ,
369
378
TLSHandshakeTimeout : 10 * time .Second ,
370
379
ExpectContinueTimeout : 1 * time .Second ,
371
380
DialContext : dialContext ,
0 commit comments