Skip to content

Commit

Permalink
Add timeout fields to the server config. Set defaults for them. Initi…
Browse files Browse the repository at this point in the history
…alize structs as part of the default server config
  • Loading branch information
atoulme committed Jun 25, 2024
1 parent 1caab1b commit b7852d4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
52 changes: 48 additions & 4 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,51 @@ type ServerConfig struct {

// CompressionAlgorithms configures the list of compression algorithms the server can accept. Default: ["", "gzip", "zstd", "zlib", "snappy", "deflate"]
CompressionAlgorithms []string `mapstructure:"compression_algorithms"`

// ReadTimeout is the maximum duration for reading the entire
// request, including the body. A zero or negative value means
// there will be no timeout.
//
// Because ReadTimeout does not let Handlers make per-request
// decisions on each request body's acceptable deadline or
// upload rate, most users will prefer to use
// ReadHeaderTimeout. It is valid to use them both.
ReadTimeout time.Duration

// ReadHeaderTimeout is the amount of time allowed to read
// request headers. The connection's read deadline is reset
// after reading the headers and the Handler can decide what
// is considered too slow for the body. If ReadHeaderTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, there is no timeout.
ReadHeaderTimeout time.Duration

// WriteTimeout is the maximum duration before timing out
// writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not
// let Handlers make decisions on a per-request basis.
// A zero or negative value means there will be no timeout.
WriteTimeout time.Duration

// IdleTimeout is the maximum amount of time to wait for the
// next request when keep-alives are enabled. If IdleTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, there is no timeout.
IdleTimeout time.Duration
}

// NewDefaultServerConfig returns ServerConfig type object with default values.
// Currently, config options are all initialized as zero values.
// We encourage to use this function to create an object of ServerConfig.
func NewDefaultServerConfig() ServerConfig {
return ServerConfig{}
tlsDefaultServerConfig := configtls.NewDefaultServerConfig()
return ServerConfig{
ResponseHeaders: map[string]configopaque.String{},
TLSSetting: &tlsDefaultServerConfig,
CORS: &CORSConfig{},
WriteTimeout: 30 * time.Second,
ReadHeaderTimeout: 1 * time.Minute,
IdleTimeout: 1 * time.Minute,
}
}

// ToListener creates a net.Listener.
Expand Down Expand Up @@ -435,9 +473,15 @@ func (hss *ServerConfig) ToServer(_ context.Context, host component.Host, settin
includeMetadata: hss.IncludeMetadata,
}

return &http.Server{
server := &http.Server{
Handler: handler,
}, nil
}
server.ReadTimeout = hss.ReadTimeout
server.ReadHeaderTimeout = hss.ReadHeaderTimeout
server.WriteTimeout = hss.WriteTimeout
server.IdleTimeout = hss.IdleTimeout

return server, nil
}

func responseHeadersHandler(handler http.Handler, headers map[string]configopaque.String) http.Handler {
Expand Down
11 changes: 11 additions & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1492,3 +1492,14 @@ func BenchmarkHttpRequest(b *testing.B) {
})
}
}

func TestDefaultHTTPServerSettings(t *testing.T) {
httpServerSettings := NewDefaultServerConfig()
assert.NotNil(t, httpServerSettings.ResponseHeaders)
assert.NotNil(t, httpServerSettings.CORS)
assert.NotNil(t, httpServerSettings.TLSSetting)
assert.Equal(t, 1*time.Minute, httpServerSettings.IdleTimeout)
assert.Equal(t, 30*time.Second, httpServerSettings.WriteTimeout)
assert.Equal(t, time.Duration(0), httpServerSettings.ReadTimeout)
assert.Equal(t, 1*time.Minute, httpServerSettings.ReadHeaderTimeout)
}
6 changes: 3 additions & 3 deletions receiver/otlpreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ func NewFactory() receiver.Factory {

// createDefaultConfig creates the default configuration for receiver.
func createDefaultConfig() component.Config {
serverConfig := confighttp.NewDefaultServerConfig()
serverConfig.Endpoint = localhostgate.EndpointForPort(httpPort)
return &Config{
Protocols: Protocols{
GRPC: &configgrpc.ServerConfig{
Expand All @@ -52,7 +50,9 @@ func createDefaultConfig() component.Config {
ReadBufferSize: 512 * 1024,
},
HTTP: &HTTPConfig{
ServerConfig: &serverConfig,
ServerConfig: &confighttp.ServerConfig{
Endpoint: localhostgate.EndpointForPort(httpPort),
},
TracesURLPath: defaultTracesURLPath,
MetricsURLPath: defaultMetricsURLPath,
LogsURLPath: defaultLogsURLPath,
Expand Down
2 changes: 1 addition & 1 deletion receiver/otlpreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestCreateMetricReceiver(t *testing.T) {
defaultServerConfig := confighttp.NewDefaultServerConfig()
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
defaultHTTPSettings := &HTTPConfig{
ServerConfig: &defaultServerConfig,
ServerConfig: &confighttp.ServerConfig{},
TracesURLPath: defaultTracesURLPath,
MetricsURLPath: defaultMetricsURLPath,
LogsURLPath: defaultLogsURLPath,
Expand Down

0 comments on commit b7852d4

Please sign in to comment.