@@ -47,6 +47,9 @@ var DefaultHTTPClientConfig = HTTPClientConfig{
4747var defaultHTTPClientOptions = httpClientOptions {
4848 keepAlivesEnabled : true ,
4949 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 ,
5053}
5154
5255type closeIdler interface {
@@ -283,6 +286,7 @@ type httpClientOptions struct {
283286 dialContextFunc DialContextFunc
284287 keepAlivesEnabled bool
285288 http2Enabled bool
289+ idleConnTimeout time.Duration
286290}
287291
288292// HTTPClientOption defines an option that can be applied to the HTTP client.
@@ -309,6 +313,13 @@ func WithHTTP2Disabled() HTTPClientOption {
309313 }
310314}
311315
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+
312323// NewClient returns a http.Client using the specified http.RoundTripper.
313324func newClient (rt http.RoundTripper ) * http.Client {
314325 return & http.Client {Transport : rt }
@@ -357,15 +368,13 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
357368 // The only timeout we care about is the configured scrape timeout.
358369 // It is applied on request. So we leave out any timings here.
359370 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 ,
369378 TLSHandshakeTimeout : 10 * time .Second ,
370379 ExpectContinueTimeout : 1 * time .Second ,
371380 DialContext : dialContext ,
0 commit comments