From 4d19847a32695a9b2b0973bd4754767eac334d83 Mon Sep 17 00:00:00 2001 From: Ravi Rupapara Date: Fri, 12 Nov 2021 12:37:13 +0530 Subject: [PATCH] Make all new parameter type to pointer --- config/confighttp/confighttp.go | 19 +++++++++++++------ config/confighttp/confighttp_test.go | 6 ++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/config/confighttp/confighttp.go b/config/confighttp/confighttp.go index 1daa7d611a2..d7b25d67306 100644 --- a/config/confighttp/confighttp.go +++ b/config/confighttp/confighttp.go @@ -60,18 +60,20 @@ type HTTPClientSettings struct { Auth *configauth.Authentication `mapstructure:"auth,omitempty"` // MaxIdleConns is used to set a limit to the maximum idle HTTP connections the client can keep open. - // Here pointer is used to differentiate `no input` from `zero value input` + // There's an already set value, and we want to override it only if an explicit value provided MaxIdleConns *int `mapstructure:"max_idle_conns"` // MaxIdleConnsPerHost is used to set a limit to the maximum idle HTTP connections the host can keep open. - MaxIdleConnsPerHost int `mapstructure:"max_idle_conns_per_host"` + // There's an already set value, and we want to override it only if an explicit value provided + MaxIdleConnsPerHost *int `mapstructure:"max_idle_conns_per_host"` // MaxConnsPerHost limits the total number of connections per host, including connections in the dialing, // active, and idle states. - MaxConnsPerHost int `mapstructure:"max_conns_per_host"` + // There's an already set value, and we want to override it only if an explicit value provided + MaxConnsPerHost *int `mapstructure:"max_conns_per_host"` // IdleConnTimeout is the maximum amount of time a connection will remain open before closing itself. - // Here pointer is used to differentiate `no input` from `zero value input` + // There's an already set value, and we want to override it only if an explicit value provided IdleConnTimeout *time.Duration `mapstructure:"idle_conn_timeout"` } @@ -96,8 +98,13 @@ func (hcs *HTTPClientSettings) ToClient(ext map[config.ComponentID]component.Ext transport.MaxIdleConns = *hcs.MaxIdleConns } - transport.MaxIdleConnsPerHost = hcs.MaxIdleConnsPerHost - transport.MaxConnsPerHost = hcs.MaxConnsPerHost + if hcs.MaxIdleConnsPerHost != nil { + transport.MaxIdleConnsPerHost = *hcs.MaxIdleConnsPerHost + } + + if hcs.MaxConnsPerHost != nil { + transport.MaxConnsPerHost = *hcs.MaxConnsPerHost + } if hcs.IdleConnTimeout != nil { transport.IdleConnTimeout = *hcs.IdleConnTimeout diff --git a/config/confighttp/confighttp_test.go b/config/confighttp/confighttp_test.go index 247add0d3b2..98f2febd183 100644 --- a/config/confighttp/confighttp_test.go +++ b/config/confighttp/confighttp_test.go @@ -49,6 +49,8 @@ func TestAllHTTPClientSettings(t *testing.T) { config.NewComponentID("testauth"): &configauth.MockClientAuthenticator{ResultRoundTripper: &customRoundTripper{}}, } maxIdleConns := 50 + maxIdleConnsPerHost := 40 + maxConnsPerHost := 45 idleConnTimeout := 30 * time.Second tests := []struct { name string @@ -65,8 +67,8 @@ func TestAllHTTPClientSettings(t *testing.T) { ReadBufferSize: 1024, WriteBufferSize: 512, MaxIdleConns: &maxIdleConns, - MaxIdleConnsPerHost: 40, - MaxConnsPerHost: 45, + MaxIdleConnsPerHost: &maxIdleConnsPerHost, + MaxConnsPerHost: &maxConnsPerHost, IdleConnTimeout: &idleConnTimeout, CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return next, nil }, },