Skip to content

Commit 8281fb2

Browse files
author
Julien Pivotto
authored
Merge pull request #308 from austince/feat/with-idle-conn-timeout
Add WithIdleConnTimeout HTTP client option
2 parents 9e276a1 + 5018d4d commit 8281fb2

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

config/http_config.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ var DefaultHTTPClientConfig = HTTPClientConfig{
4747
var 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

5255
type 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.
313324
func 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,

config/http_config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,25 @@ func TestCustomDialContextFunc(t *testing.T) {
456456
}
457457
}
458458

459+
func TestCustomIdleConnTimeout(t *testing.T) {
460+
timeout := time.Second * 5
461+
462+
cfg := HTTPClientConfig{}
463+
rt, err := NewRoundTripperFromConfig(cfg, "test", WithIdleConnTimeout(timeout))
464+
if err != nil {
465+
t.Fatalf("Can't create a round-tripper from this config: %+v", cfg)
466+
}
467+
468+
transport, ok := rt.(*http.Transport)
469+
if !ok {
470+
t.Fatalf("Unexpected transport: %+v", transport)
471+
}
472+
473+
if transport.IdleConnTimeout != timeout {
474+
t.Fatalf("Unexpected idle connection timeout: %+v", timeout)
475+
}
476+
}
477+
459478
func TestMissingBearerAuthFile(t *testing.T) {
460479
cfg := HTTPClientConfig{
461480
BearerTokenFile: MissingBearerTokenFile,

0 commit comments

Comments
 (0)