From 2c36c5a8f79ae3d48a5c18df5cf89e31d8441653 Mon Sep 17 00:00:00 2001 From: Russell Rollins Date: Fri, 29 Jun 2018 09:46:03 -0400 Subject: [PATCH] Update HTTPConfig with KeepAlive setting. Signed-off-by: Russell Rollins --- config/http_config.go | 9 ++++++++- config/http_config_test.go | 14 ++++++++++++++ config/testdata/http.conf.good.yml | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/config/http_config.go b/config/http_config.go index da5d59014..2e1bf3573 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -74,6 +74,8 @@ type HTTPClientConfig struct { ProxyURL URL `yaml:"proxy_url,omitempty"` // TLSConfig to use to connect to the targets. TLSConfig TLSConfig `yaml:"tls_config,omitempty"` + // If set, override whether to use HTTP KeepAlive - defaults ON + KeepAlive *bool `yaml:"keep_alive,omitempty"` } // Validate validates the HTTPClientConfig to check only one of BearerToken, @@ -128,13 +130,18 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string) (http.RoundTri if err != nil { return nil, err } + // If not specified in config, default HTTP connections to allow keep-alive + disableKeepAlives := false + if cfg.KeepAlive != nil { + disableKeepAlives = !*cfg.KeepAlive + } // The only timeout we care about is the configured scrape timeout. // It is applied on request. So we leave out any timings here. var rt http.RoundTripper = &http.Transport{ Proxy: http.ProxyURL(cfg.ProxyURL.URL), MaxIdleConns: 20000, MaxIdleConnsPerHost: 1000, // see https://github.com/golang/go/issues/13801 - DisableKeepAlives: false, + DisableKeepAlives: disableKeepAlives, TLSClientConfig: tlsConfig, DisableCompression: true, // 5 minutes is typically above the maximum sane scrape interval. So we can diff --git a/config/http_config_test.go b/config/http_config_test.go index 4639ae475..2a9e6e76f 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -93,6 +93,7 @@ func newTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httpt } func TestNewClientFromConfig(t *testing.T) { + fVal := false var newClientValidConfig = []struct { clientConfig HTTPClientConfig handler func(w http.ResponseWriter, r *http.Request) @@ -121,6 +122,19 @@ func TestNewClientFromConfig(t *testing.T) { handler: func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, ExpectedMessage) }, + }, { + clientConfig: HTTPClientConfig{ + TLSConfig: TLSConfig{ + CAFile: TLSCAChainPath, + CertFile: BarneyCertificatePath, + KeyFile: BarneyKeyNoPassPath, + ServerName: "", + InsecureSkipVerify: false}, + KeepAlive: &fVal, + }, + handler: func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, ExpectedMessage) + }, }, { clientConfig: HTTPClientConfig{ BearerToken: BearerToken, diff --git a/config/testdata/http.conf.good.yml b/config/testdata/http.conf.good.yml index 46ca63908..bb53cffde 100644 --- a/config/testdata/http.conf.good.yml +++ b/config/testdata/http.conf.good.yml @@ -2,3 +2,4 @@ basic_auth: username: username password: "mysecret" proxy_url: "http://remote.host" +keep_alive: false