@@ -37,9 +37,9 @@ import (
37
37
)
38
38
39
39
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
43
43
)
44
44
45
45
type Error string
@@ -64,7 +64,8 @@ type Config struct {
64
64
Port int `mapstructure:"port" json:"port" default:"8080"`
65
65
Host string `mapstructure:"host,omitempty" json:"host"`
66
66
PublicHostname string `mapstructure:"public_hostname,omitempty" json:"publicHostname"`
67
-
67
+ // Http server config
68
+ HTTPServerConfig HTTPServerConfig `mapstructure:"http" json:"http"`
68
69
// Enable pprof
69
70
EnablePprof bool `mapstructure:"enable_pprof" json:"enable_pprof" default:"false"`
70
71
// Trace is for configuring open telemetry based tracing.
@@ -109,6 +110,14 @@ type TLSConfig struct {
109
110
Key string `mapstructure:"key" json:"key"`
110
111
}
111
112
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
+
112
121
// CORS Configuration for the server
113
122
type CORSConfig struct {
114
123
// Enable CORS for the server (default: true)
@@ -283,9 +292,8 @@ func (rw *grpcGatewayResponseWriter) Write(data []byte) (int, error) {
283
292
// newHTTPServer creates a new http server with the given handler and grpc server
284
293
func newHTTPServer (c Config , connectRPC http.Handler , originalGrpcGateway http.Handler , a * auth.Authentication , l * logger.Logger ) (* http.Server , error ) {
285
294
var (
286
- err error
287
- tc * tls.Config
288
- writeTimeoutOverride = writeTimeout
295
+ err error
296
+ tc * tls.Config
289
297
)
290
298
291
299
// Adds deprecation header to any grpcGateway responses.
@@ -333,7 +341,9 @@ func newHTTPServer(c Config, connectRPC http.Handler, originalGrpcGateway http.H
333
341
if c .EnablePprof {
334
342
grpcGateway = pprofHandler (grpcGateway )
335
343
// 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
+ }
337
347
}
338
348
339
349
var handler http.Handler
@@ -347,12 +357,22 @@ func newHTTPServer(c Config, connectRPC http.Handler, originalGrpcGateway http.H
347
357
handler = routeConnectRPCRequests (connectRPC , grpcGateway )
348
358
}
349
359
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
+
350
367
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 ,
356
376
}, nil
357
377
}
358
378
0 commit comments