Skip to content

Commit

Permalink
[chore] set default transport defaults on NewDefaultClientConfig (#10688
Browse files Browse the repository at this point in the history
)

#### Description
Set the defaults on `NewDefaultClientConfig`. This change is a no-op
since those defaults are set if the value is not set.

Add documentation to all fields as well.

#### Link to tracking issue
#9478

---------

Co-authored-by: Pablo Baeyens <pablo.baeyens@datadoghq.com>
  • Loading branch information
atoulme and mx-psi authored Aug 21, 2024
1 parent 26fb7cb commit 4d0b088
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ type ClientConfig struct {
TLSSetting configtls.ClientConfig `mapstructure:"tls"`

// ReadBufferSize for HTTP client. See http.Transport.ReadBufferSize.
// Default is 0.
ReadBufferSize int `mapstructure:"read_buffer_size"`

// WriteBufferSize for HTTP client. See http.Transport.WriteBufferSize.
// Default is 0.
WriteBufferSize int `mapstructure:"write_buffer_size"`

// Timeout parameter configures `http.Client.Timeout`.
// Default is 0 (unlimited).
Timeout time.Duration `mapstructure:"timeout"`

// Additional headers attached to each HTTP request sent by the client.
Expand All @@ -67,20 +70,20 @@ type ClientConfig struct {
Compression configcompression.Type `mapstructure:"compression"`

// MaxIdleConns is used to set a limit to the maximum idle HTTP connections the client can keep open.
// There's an already set value, and we want to override it only if an explicit value provided
// By default, it is set to 100.
MaxIdleConns *int `mapstructure:"max_idle_conns"`

// MaxIdleConnsPerHost is used to set a limit to the maximum idle HTTP connections the host can keep open.
// There's an already set value, and we want to override it only if an explicit value provided
// By default, it is set to [http.DefaultTransport.MaxIdleConnsPerHost].
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.
// There's an already set value, and we want to override it only if an explicit value provided
// By default, it is set to [http.DefaultTransport.MaxConnsPerHost].
MaxConnsPerHost *int `mapstructure:"max_conns_per_host"`

// IdleConnTimeout is the maximum amount of time a connection will remain open before closing itself.
// There's an already set value, and we want to override it only if an explicit value provided
// By default, it is set to [http.DefaultTransport.IdleConnTimeout]
IdleConnTimeout *time.Duration `mapstructure:"idle_conn_timeout"`

// DisableKeepAlives, if true, disables HTTP keep-alives and will only use the connection to the server
Expand Down Expand Up @@ -111,17 +114,21 @@ type CookiesConfig struct {
}

// NewDefaultClientConfig returns ClientConfig type object with
// the default values of 'MaxIdleConns' and 'IdleConnTimeout'.
// the default values of 'MaxIdleConns' and 'IdleConnTimeout', as well as [http.DefaultTransport] values.
// 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 ClientConfig.
func NewDefaultClientConfig() ClientConfig {
// The default values are taken from the values of 'DefaultTransport' of 'http' package.
maxIdleConns := 100
idleConnTimeout := 90 * time.Second
defaultTransport := http.DefaultTransport.(*http.Transport)

return ClientConfig{
MaxIdleConns: &maxIdleConns,
IdleConnTimeout: &idleConnTimeout,
ReadBufferSize: defaultTransport.ReadBufferSize,
WriteBufferSize: defaultTransport.WriteBufferSize,
Headers: map[string]configopaque.String{},
MaxIdleConns: &defaultTransport.MaxIdleConns,
MaxIdleConnsPerHost: &defaultTransport.MaxIdleConnsPerHost,
MaxConnsPerHost: &defaultTransport.MaxConnsPerHost,
IdleConnTimeout: &defaultTransport.IdleConnTimeout,
}
}

Expand Down

0 comments on commit 4d0b088

Please sign in to comment.