Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[confighttp] add confighttp.NewDefaultServerConfig() #10275

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/newdefaultserverconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `confighttp.NewDefaultServerConfig()` to instantiate the default HTTP server configuration

# One or more tracking issues or pull requests related to the change
issues: [9655]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
55 changes: 53 additions & 2 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +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 `mapstructure:"read_timeout"`

// 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 `mapstructure:"read_header_timeout"`

// 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 `mapstructure:"write_timeout"`

// 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 `mapstructure:"idle_timeout"`
}

// NewDefaultServerConfig returns ServerConfig type object with default values.
// We encourage to use this function to create an object of ServerConfig.
func NewDefaultServerConfig() ServerConfig {
tlsDefaultServerConfig := configtls.NewDefaultServerConfig()
return ServerConfig{
ResponseHeaders: map[string]configopaque.String{},
TLSSetting: &tlsDefaultServerConfig,
CORS: &CORSConfig{},
atoulme marked this conversation as resolved.
Show resolved Hide resolved
WriteTimeout: 30 * time.Second,
ReadHeaderTimeout: 1 * time.Minute,
IdleTimeout: 1 * time.Minute,
}
}

// ToListener creates a net.Listener.
Expand Down Expand Up @@ -428,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
22 changes: 16 additions & 6 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,9 +1007,9 @@ func verifyHeadersResp(t *testing.T, url string, expected map[string]configopaqu
}

func ExampleServerConfig() {
settings := ServerConfig{
Endpoint: "localhost:443",
}
settings := NewDefaultServerConfig()
settings.Endpoint = "localhost:443"

s, err := settings.ToServer(
context.Background(),
componenttest.NewNopHost(),
Expand Down Expand Up @@ -1275,9 +1275,8 @@ func TestServerWithErrorHandler(t *testing.T) {

func TestServerWithDecoder(t *testing.T) {
// prepare
hss := ServerConfig{
Endpoint: "localhost:0",
}
hss := NewDefaultServerConfig()
hss.Endpoint = "localhost:0"
decoder := func(body io.ReadCloser) (io.ReadCloser, error) {
return body, nil
}
Expand Down Expand Up @@ -1502,3 +1501,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)
}
18 changes: 9 additions & 9 deletions receiver/otlpreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func TestCreateTracesReceiver(t *testing.T) {
Transport: confignet.TransportTypeTCP,
},
}
defaultServerConfig := confighttp.NewDefaultServerConfig()
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
defaultHTTPSettings := &HTTPConfig{
ServerConfig: &confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
ServerConfig: &defaultServerConfig,
TracesURLPath: defaultTracesURLPath,
MetricsURLPath: defaultMetricsURLPath,
LogsURLPath: defaultLogsURLPath,
Expand Down Expand Up @@ -152,10 +152,10 @@ func TestCreateMetricReceiver(t *testing.T) {
Transport: confignet.TransportTypeTCP,
},
}
defaultServerConfig := confighttp.NewDefaultServerConfig()
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
defaultHTTPSettings := &HTTPConfig{
ServerConfig: &confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
ServerConfig: &defaultServerConfig,
TracesURLPath: defaultTracesURLPath,
MetricsURLPath: defaultMetricsURLPath,
LogsURLPath: defaultLogsURLPath,
Expand Down Expand Up @@ -246,10 +246,10 @@ func TestCreateLogReceiver(t *testing.T) {
Transport: confignet.TransportTypeTCP,
},
}
defaultServerConfig := confighttp.NewDefaultServerConfig()
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
defaultHTTPSettings := &HTTPConfig{
ServerConfig: &confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
ServerConfig: &defaultServerConfig,
TracesURLPath: defaultTracesURLPath,
MetricsURLPath: defaultMetricsURLPath,
LogsURLPath: defaultLogsURLPath,
Expand Down
Loading