Skip to content

Commit

Permalink
Add DefaultHTTPClientSettings function in confighttp
Browse files Browse the repository at this point in the history
  • Loading branch information
rrupapara committed Dec 6, 2021
1 parent 0904856 commit c27c673
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c27c673

Please sign in to comment.