node, rpc: add ReadHeaderTimeout config option (#25338)

This change makes http.Server.ReadHeaderTimeout configurable separately
from ReadTimeout for RPC servers. The default is set to the same as
ReadTimeout, which in order to cause no change in existing deployments.
This commit is contained in:
Tristan-Wilson 2022-08-03 07:50:12 -07:00 committed by GitHub
parent d804a59ee1
commit 9244f87dc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 7 deletions

View File

@ -41,6 +41,7 @@ func StartHTTPEndpoint(endpoint string, timeouts rpc.HTTPTimeouts, handler http.
httpSrv := &http.Server{ httpSrv := &http.Server{
Handler: handler, Handler: handler,
ReadTimeout: timeouts.ReadTimeout, ReadTimeout: timeouts.ReadTimeout,
ReadHeaderTimeout: timeouts.ReadHeaderTimeout,
WriteTimeout: timeouts.WriteTimeout, WriteTimeout: timeouts.WriteTimeout,
IdleTimeout: timeouts.IdleTimeout, IdleTimeout: timeouts.IdleTimeout,
} }
@ -75,6 +76,10 @@ func CheckTimeouts(timeouts *rpc.HTTPTimeouts) {
log.Warn("Sanitizing invalid HTTP read timeout", "provided", timeouts.ReadTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadTimeout) log.Warn("Sanitizing invalid HTTP read timeout", "provided", timeouts.ReadTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadTimeout)
timeouts.ReadTimeout = rpc.DefaultHTTPTimeouts.ReadTimeout timeouts.ReadTimeout = rpc.DefaultHTTPTimeouts.ReadTimeout
} }
if timeouts.ReadHeaderTimeout < time.Second {
log.Warn("Sanitizing invalid HTTP read header timeout", "provided", timeouts.ReadHeaderTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadHeaderTimeout)
timeouts.ReadHeaderTimeout = rpc.DefaultHTTPTimeouts.ReadHeaderTimeout
}
if timeouts.WriteTimeout < time.Second { if timeouts.WriteTimeout < time.Second {
log.Warn("Sanitizing invalid HTTP write timeout", "provided", timeouts.WriteTimeout, "updated", rpc.DefaultHTTPTimeouts.WriteTimeout) log.Warn("Sanitizing invalid HTTP write timeout", "provided", timeouts.WriteTimeout, "updated", rpc.DefaultHTTPTimeouts.WriteTimeout)
timeouts.WriteTimeout = rpc.DefaultHTTPTimeouts.WriteTimeout timeouts.WriteTimeout = rpc.DefaultHTTPTimeouts.WriteTimeout

View File

@ -134,6 +134,7 @@ func (h *httpServer) start() error {
if h.timeouts != (rpc.HTTPTimeouts{}) { if h.timeouts != (rpc.HTTPTimeouts{}) {
CheckTimeouts(&h.timeouts) CheckTimeouts(&h.timeouts)
h.server.ReadTimeout = h.timeouts.ReadTimeout h.server.ReadTimeout = h.timeouts.ReadTimeout
h.server.ReadHeaderTimeout = h.timeouts.ReadHeaderTimeout
h.server.WriteTimeout = h.timeouts.WriteTimeout h.server.WriteTimeout = h.timeouts.WriteTimeout
h.server.IdleTimeout = h.timeouts.IdleTimeout h.server.IdleTimeout = h.timeouts.IdleTimeout
} }

View File

@ -87,6 +87,14 @@ type HTTPTimeouts struct {
// ReadHeaderTimeout. It is valid to use them both. // ReadHeaderTimeout. It is valid to use them both.
ReadTimeout time.Duration ReadTimeout time.Duration
// 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
// WriteTimeout is the maximum duration before timing out // WriteTimeout is the maximum duration before timing out
// writes of the response. It is reset whenever a new // writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not // request's header is read. Like ReadTimeout, it does not
@ -104,6 +112,7 @@ type HTTPTimeouts struct {
// configuration is not provided. // configuration is not provided.
var DefaultHTTPTimeouts = HTTPTimeouts{ var DefaultHTTPTimeouts = HTTPTimeouts{
ReadTimeout: 30 * time.Second, ReadTimeout: 30 * time.Second,
ReadHeaderTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second, IdleTimeout: 120 * time.Second,
} }