Skip to content

Commit 72df968

Browse files
elizabethhealygithub-actions[bot]
authored andcommitted
feat(core): Add the ability to configure the http server settings (#2522)
### Proposed Changes * ability to specify read, write, readheader, and idle timeouts and maxheaderbytes via the opentdf.yaml ### Checklist - [ ] I have added or updated unit tests - [ ] I have added or updated integration tests (if appropriate) - [ ] I have added or updated documentation ### Testing Instructions (cherry picked from commit b1472df)
1 parent 6f0e708 commit 72df968

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

opentdf-dev.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ server:
144144
maxage: 3600
145145
grpc:
146146
reflectionEnabled: true # Default is false
147+
# http:
148+
# # HTTP server configuration
149+
# # Negative values indicate no timeout, default will be used if the timeout is set to 0
150+
# readTimeout: 15s
151+
# writeTimeout: 15s
152+
# readHeaderTimeout: 10s
153+
# idleTimeout: 20s
154+
# maxHeaderBytes: 1048576 # 1 MB
147155
cryptoProvider:
148156
type: standard
149157
standard:

service/internal/server/server.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import (
3737
)
3838

3939
const (
40-
writeTimeout time.Duration = 5 * time.Second
41-
readTimeout time.Duration = 10 * time.Second
42-
shutdownTimeout time.Duration = 5 * time.Second
40+
defaultWriteTimeout time.Duration = 5 * time.Second
41+
defaultReadTimeout time.Duration = 10 * time.Second
42+
shutdownTimeout time.Duration = 5 * time.Second
4343
)
4444

4545
type Error string
@@ -64,7 +64,8 @@ type Config struct {
6464
Port int `mapstructure:"port" json:"port" default:"8080"`
6565
Host string `mapstructure:"host,omitempty" json:"host"`
6666
PublicHostname string `mapstructure:"public_hostname,omitempty" json:"publicHostname"`
67-
67+
// Http server config
68+
HTTPServerConfig HTTPServerConfig `mapstructure:"http" json:"http"`
6869
// Enable pprof
6970
EnablePprof bool `mapstructure:"enable_pprof" json:"enable_pprof" default:"false"`
7071
// Trace is for configuring open telemetry based tracing.
@@ -109,6 +110,14 @@ type TLSConfig struct {
109110
Key string `mapstructure:"key" json:"key"`
110111
}
111112

113+
type HTTPServerConfig struct {
114+
ReadTimeout time.Duration `mapstructure:"readTimeout" json:"readTimeout"`
115+
ReadHeaderTimeout time.Duration `mapstructure:"readHeaderTimeout" json:"readHeaderTimeout"`
116+
WriteTimeout time.Duration `mapstructure:"writeTimeout" json:"writeTimeout"`
117+
IdleTimeout time.Duration `mapstructure:"idleTimeout" json:"idleTimeout"`
118+
MaxHeaderBytes int `mapstructure:"maxHeaderBytes" json:"maxHeaderBytes"`
119+
}
120+
112121
// CORS Configuration for the server
113122
type CORSConfig struct {
114123
// Enable CORS for the server (default: true)
@@ -283,9 +292,8 @@ func (rw *grpcGatewayResponseWriter) Write(data []byte) (int, error) {
283292
// newHTTPServer creates a new http server with the given handler and grpc server
284293
func newHTTPServer(c Config, connectRPC http.Handler, originalGrpcGateway http.Handler, a *auth.Authentication, l *logger.Logger) (*http.Server, error) {
285294
var (
286-
err error
287-
tc *tls.Config
288-
writeTimeoutOverride = writeTimeout
295+
err error
296+
tc *tls.Config
289297
)
290298

291299
// Adds deprecation header to any grpcGateway responses.
@@ -333,7 +341,9 @@ func newHTTPServer(c Config, connectRPC http.Handler, originalGrpcGateway http.H
333341
if c.EnablePprof {
334342
grpcGateway = pprofHandler(grpcGateway)
335343
// Need to extend write timeout to collect pprof data.
336-
writeTimeoutOverride = 30 * time.Second //nolint:mnd // easier to read that we are overriding the default
344+
if c.HTTPServerConfig.WriteTimeout < 30*time.Second {
345+
c.HTTPServerConfig.WriteTimeout = 30 * time.Second //nolint:mnd // easier to read that we are overriding the default
346+
}
337347
}
338348

339349
var handler http.Handler
@@ -347,12 +357,22 @@ func newHTTPServer(c Config, connectRPC http.Handler, originalGrpcGateway http.H
347357
handler = routeConnectRPCRequests(connectRPC, grpcGateway)
348358
}
349359

360+
if c.HTTPServerConfig.ReadTimeout == 0 {
361+
c.HTTPServerConfig.ReadTimeout = defaultReadTimeout
362+
}
363+
if c.HTTPServerConfig.WriteTimeout == 0 {
364+
c.HTTPServerConfig.WriteTimeout = defaultWriteTimeout
365+
}
366+
350367
return &http.Server{
351-
Addr: fmt.Sprintf("%s:%d", c.Host, c.Port),
352-
WriteTimeout: writeTimeoutOverride,
353-
ReadTimeout: readTimeout,
354-
Handler: handler,
355-
TLSConfig: tc,
368+
Addr: fmt.Sprintf("%s:%d", c.Host, c.Port),
369+
WriteTimeout: c.HTTPServerConfig.WriteTimeout,
370+
ReadTimeout: c.HTTPServerConfig.ReadTimeout,
371+
ReadHeaderTimeout: c.HTTPServerConfig.ReadHeaderTimeout,
372+
IdleTimeout: c.HTTPServerConfig.IdleTimeout,
373+
MaxHeaderBytes: c.HTTPServerConfig.MaxHeaderBytes,
374+
Handler: handler,
375+
TLSConfig: tc,
356376
}, nil
357377
}
358378

0 commit comments

Comments
 (0)