Skip to content
Snippets Groups Projects
Commit 305126af authored by ale's avatar ale
Browse files

Allow setting a timeout for request handling

This is a high-level timeout for the http.Handler application code,
not a low-level network timeout.
parent c165311f
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,7 @@ var gracefulShutdownTimeout = 3 * time.Second
type ServerConfig struct {
TLS *TLSServerConfig `yaml:"tls"`
MaxInflightRequests int `yaml:"max_inflight_requests"`
RequestTimeoutSecs int `yaml:"request_timeout"`
TrustedForwarders []string `yaml:"trusted_forwarders"`
}
......@@ -60,11 +61,18 @@ func (config *ServerConfig) buildHTTPServer(h http.Handler) (*http.Server, error
}
}
// Wrap the handler with a TimeoutHandler if 'request_timeout'
// is set.
h = addDefaultHandlers(h)
if config.RequestTimeoutSecs > 0 {
h = http.TimeoutHandler(h, time.Duration(config.RequestTimeoutSecs)*time.Second, "")
}
// These are not meant to be external-facing servers, so we
// can be generous with the timeouts to keep the number of
// reconnections low.
return &http.Server{
Handler: defaultHandler(h),
Handler: h,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 600 * time.Second,
......@@ -134,7 +142,7 @@ func Serve(h http.Handler, config *ServerConfig, addr string) error {
return nil
}
func defaultHandler(h http.Handler) http.Handler {
func addDefaultHandlers(h http.Handler) http.Handler {
root := http.NewServeMux()
// Add an endpoint for HTTP health checking probes.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment