Skip to content

Commit 0e14c22

Browse files
authored
[confighttp] add confighttp.NewDefaultServerConfig() (#10275)
#### Description add a new default method to instantiate the HTTP server config. <!-- Issue number if applicable --> #### Link to tracking issue #9655
1 parent 7d5b1ba commit 0e14c22

File tree

4 files changed

+103
-17
lines changed

4 files changed

+103
-17
lines changed

.chloggen/newdefaultserverconfig.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confighttp
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add `confighttp.NewDefaultServerConfig()` to instantiate the default HTTP server configuration
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [9655]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

config/confighttp/confighttp.go

+53-2
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,51 @@ type ServerConfig struct {
306306

307307
// CompressionAlgorithms configures the list of compression algorithms the server can accept. Default: ["", "gzip", "zstd", "zlib", "snappy", "deflate"]
308308
CompressionAlgorithms []string `mapstructure:"compression_algorithms"`
309+
310+
// ReadTimeout is the maximum duration for reading the entire
311+
// request, including the body. A zero or negative value means
312+
// there will be no timeout.
313+
//
314+
// Because ReadTimeout does not let Handlers make per-request
315+
// decisions on each request body's acceptable deadline or
316+
// upload rate, most users will prefer to use
317+
// ReadHeaderTimeout. It is valid to use them both.
318+
ReadTimeout time.Duration `mapstructure:"read_timeout"`
319+
320+
// ReadHeaderTimeout is the amount of time allowed to read
321+
// request headers. The connection's read deadline is reset
322+
// after reading the headers and the Handler can decide what
323+
// is considered too slow for the body. If ReadHeaderTimeout
324+
// is zero, the value of ReadTimeout is used. If both are
325+
// zero, there is no timeout.
326+
ReadHeaderTimeout time.Duration `mapstructure:"read_header_timeout"`
327+
328+
// WriteTimeout is the maximum duration before timing out
329+
// writes of the response. It is reset whenever a new
330+
// request's header is read. Like ReadTimeout, it does not
331+
// let Handlers make decisions on a per-request basis.
332+
// A zero or negative value means there will be no timeout.
333+
WriteTimeout time.Duration `mapstructure:"write_timeout"`
334+
335+
// IdleTimeout is the maximum amount of time to wait for the
336+
// next request when keep-alives are enabled. If IdleTimeout
337+
// is zero, the value of ReadTimeout is used. If both are
338+
// zero, there is no timeout.
339+
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
340+
}
341+
342+
// NewDefaultServerConfig returns ServerConfig type object with default values.
343+
// We encourage to use this function to create an object of ServerConfig.
344+
func NewDefaultServerConfig() ServerConfig {
345+
tlsDefaultServerConfig := configtls.NewDefaultServerConfig()
346+
return ServerConfig{
347+
ResponseHeaders: map[string]configopaque.String{},
348+
TLSSetting: &tlsDefaultServerConfig,
349+
CORS: &CORSConfig{},
350+
WriteTimeout: 30 * time.Second,
351+
ReadHeaderTimeout: 1 * time.Minute,
352+
IdleTimeout: 1 * time.Minute,
353+
}
309354
}
310355

311356
type AuthConfig struct {
@@ -437,9 +482,15 @@ func (hss *ServerConfig) ToServer(_ context.Context, host component.Host, settin
437482
includeMetadata: hss.IncludeMetadata,
438483
}
439484

440-
return &http.Server{
485+
server := &http.Server{
441486
Handler: handler,
442-
}, nil
487+
}
488+
server.ReadTimeout = hss.ReadTimeout
489+
server.ReadHeaderTimeout = hss.ReadHeaderTimeout
490+
server.WriteTimeout = hss.WriteTimeout
491+
server.IdleTimeout = hss.IdleTimeout
492+
493+
return server, nil
443494
}
444495

445496
func responseHeadersHandler(handler http.Handler, headers map[string]configopaque.String) http.Handler {

config/confighttp/confighttp_test.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -1009,9 +1009,9 @@ func verifyHeadersResp(t *testing.T, url string, expected map[string]configopaqu
10091009
}
10101010

10111011
func ExampleServerConfig() {
1012-
settings := ServerConfig{
1013-
Endpoint: "localhost:443",
1014-
}
1012+
settings := NewDefaultServerConfig()
1013+
settings.Endpoint = "localhost:443"
1014+
10151015
s, err := settings.ToServer(
10161016
context.Background(),
10171017
componenttest.NewNopHost(),
@@ -1283,9 +1283,8 @@ func TestServerWithErrorHandler(t *testing.T) {
12831283

12841284
func TestServerWithDecoder(t *testing.T) {
12851285
// prepare
1286-
hss := ServerConfig{
1287-
Endpoint: "localhost:0",
1288-
}
1286+
hss := NewDefaultServerConfig()
1287+
hss.Endpoint = "localhost:0"
12891288
decoder := func(body io.ReadCloser) (io.ReadCloser, error) {
12901289
return body, nil
12911290
}
@@ -1552,3 +1551,14 @@ func BenchmarkHttpRequest(b *testing.B) {
15521551
})
15531552
}
15541553
}
1554+
1555+
func TestDefaultHTTPServerSettings(t *testing.T) {
1556+
httpServerSettings := NewDefaultServerConfig()
1557+
assert.NotNil(t, httpServerSettings.ResponseHeaders)
1558+
assert.NotNil(t, httpServerSettings.CORS)
1559+
assert.NotNil(t, httpServerSettings.TLSSetting)
1560+
assert.Equal(t, 1*time.Minute, httpServerSettings.IdleTimeout)
1561+
assert.Equal(t, 30*time.Second, httpServerSettings.WriteTimeout)
1562+
assert.Equal(t, time.Duration(0), httpServerSettings.ReadTimeout)
1563+
assert.Equal(t, 1*time.Minute, httpServerSettings.ReadHeaderTimeout)
1564+
}

receiver/otlpreceiver/factory_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ func TestCreateTracesReceiver(t *testing.T) {
5858
Transport: confignet.TransportTypeTCP,
5959
},
6060
}
61+
defaultServerConfig := confighttp.NewDefaultServerConfig()
62+
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
6163
defaultHTTPSettings := &HTTPConfig{
62-
ServerConfig: &confighttp.ServerConfig{
63-
Endpoint: testutil.GetAvailableLocalAddress(t),
64-
},
64+
ServerConfig: &defaultServerConfig,
6565
TracesURLPath: defaultTracesURLPath,
6666
MetricsURLPath: defaultMetricsURLPath,
6767
LogsURLPath: defaultLogsURLPath,
@@ -152,10 +152,10 @@ func TestCreateMetricReceiver(t *testing.T) {
152152
Transport: confignet.TransportTypeTCP,
153153
},
154154
}
155+
defaultServerConfig := confighttp.NewDefaultServerConfig()
156+
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
155157
defaultHTTPSettings := &HTTPConfig{
156-
ServerConfig: &confighttp.ServerConfig{
157-
Endpoint: testutil.GetAvailableLocalAddress(t),
158-
},
158+
ServerConfig: &defaultServerConfig,
159159
TracesURLPath: defaultTracesURLPath,
160160
MetricsURLPath: defaultMetricsURLPath,
161161
LogsURLPath: defaultLogsURLPath,
@@ -246,10 +246,10 @@ func TestCreateLogReceiver(t *testing.T) {
246246
Transport: confignet.TransportTypeTCP,
247247
},
248248
}
249+
defaultServerConfig := confighttp.NewDefaultServerConfig()
250+
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
249251
defaultHTTPSettings := &HTTPConfig{
250-
ServerConfig: &confighttp.ServerConfig{
251-
Endpoint: testutil.GetAvailableLocalAddress(t),
252-
},
252+
ServerConfig: &defaultServerConfig,
253253
TracesURLPath: defaultTracesURLPath,
254254
MetricsURLPath: defaultMetricsURLPath,
255255
LogsURLPath: defaultLogsURLPath,

0 commit comments

Comments
 (0)