diff --git a/config/confighttp/confighttp.go b/config/confighttp/confighttp.go index d7b25d67306..7e19bdb7741 100644 --- a/config/confighttp/confighttp.go +++ b/config/confighttp/confighttp.go @@ -77,6 +77,21 @@ type HTTPClientSettings struct { IdleConnTimeout *time.Duration `mapstructure:"idle_conn_timeout"` } +// DefaultHTTPClientSettings returns HTTPClientSettings type object with +// the default values of 'MaxIdleConns' and 'IdleConnTimeout'. +// Other config options are not added as they are initialized with 'zero value' by GoLang as default. +// We encourage to use this function to create an object of HTTPClientSettings. +func DefaultHTTPClientSettings() HTTPClientSettings { + // The default values are taken from the values of 'DefaultTransport' of 'http' package. + maxIdleConns := 100 + idleConnTimeout := 90 * time.Second + + return HTTPClientSettings{ + MaxIdleConns: &maxIdleConns, + IdleConnTimeout: &idleConnTimeout, + } +} + // ToClient creates an HTTP client. func (hcs *HTTPClientSettings) ToClient(ext map[config.ComponentID]component.Extension) (*http.Client, error) { tlsCfg, err := hcs.TLSSetting.LoadTLSConfig() diff --git a/config/confighttp/confighttp_test.go b/config/confighttp/confighttp_test.go index 98f2febd183..834708779db 100644 --- a/config/confighttp/confighttp_test.go +++ b/config/confighttp/confighttp_test.go @@ -149,6 +149,12 @@ func TestPartialHTTPClientSettings(t *testing.T) { } } +func TestDefaultHTTPClientSettings(t *testing.T) { + httpClientSettings := DefaultHTTPClientSettings() + assert.EqualValues(t, 100, *httpClientSettings.MaxIdleConns) + assert.EqualValues(t, 90*time.Second, *httpClientSettings.IdleConnTimeout) +} + func TestHTTPClientSettingsError(t *testing.T) { tests := []struct { settings HTTPClientSettings