Skip to content

Commit 9244f87

Browse files
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.
1 parent d804a59 commit 9244f87

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

node/endpoints.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ func StartHTTPEndpoint(endpoint string, timeouts rpc.HTTPTimeouts, handler http.
3939
CheckTimeouts(&timeouts)
4040
// Bundle and start the HTTP server
4141
httpSrv := &http.Server{
42-
Handler: handler,
43-
ReadTimeout: timeouts.ReadTimeout,
44-
WriteTimeout: timeouts.WriteTimeout,
45-
IdleTimeout: timeouts.IdleTimeout,
42+
Handler: handler,
43+
ReadTimeout: timeouts.ReadTimeout,
44+
ReadHeaderTimeout: timeouts.ReadHeaderTimeout,
45+
WriteTimeout: timeouts.WriteTimeout,
46+
IdleTimeout: timeouts.IdleTimeout,
4647
}
4748
go httpSrv.Serve(listener)
4849
return httpSrv, listener.Addr(), err
@@ -75,6 +76,10 @@ func CheckTimeouts(timeouts *rpc.HTTPTimeouts) {
7576
log.Warn("Sanitizing invalid HTTP read timeout", "provided", timeouts.ReadTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadTimeout)
7677
timeouts.ReadTimeout = rpc.DefaultHTTPTimeouts.ReadTimeout
7778
}
79+
if timeouts.ReadHeaderTimeout < time.Second {
80+
log.Warn("Sanitizing invalid HTTP read header timeout", "provided", timeouts.ReadHeaderTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadHeaderTimeout)
81+
timeouts.ReadHeaderTimeout = rpc.DefaultHTTPTimeouts.ReadHeaderTimeout
82+
}
7883
if timeouts.WriteTimeout < time.Second {
7984
log.Warn("Sanitizing invalid HTTP write timeout", "provided", timeouts.WriteTimeout, "updated", rpc.DefaultHTTPTimeouts.WriteTimeout)
8085
timeouts.WriteTimeout = rpc.DefaultHTTPTimeouts.WriteTimeout

node/rpcstack.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func (h *httpServer) start() error {
134134
if h.timeouts != (rpc.HTTPTimeouts{}) {
135135
CheckTimeouts(&h.timeouts)
136136
h.server.ReadTimeout = h.timeouts.ReadTimeout
137+
h.server.ReadHeaderTimeout = h.timeouts.ReadHeaderTimeout
137138
h.server.WriteTimeout = h.timeouts.WriteTimeout
138139
h.server.IdleTimeout = h.timeouts.IdleTimeout
139140
}

rpc/http.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ type HTTPTimeouts struct {
8787
// ReadHeaderTimeout. It is valid to use them both.
8888
ReadTimeout time.Duration
8989

90+
// ReadHeaderTimeout is the amount of time allowed to read
91+
// request headers. The connection's read deadline is reset
92+
// after reading the headers and the Handler can decide what
93+
// is considered too slow for the body. If ReadHeaderTimeout
94+
// is zero, the value of ReadTimeout is used. If both are
95+
// zero, there is no timeout.
96+
ReadHeaderTimeout time.Duration
97+
9098
// WriteTimeout is the maximum duration before timing out
9199
// writes of the response. It is reset whenever a new
92100
// request's header is read. Like ReadTimeout, it does not
@@ -103,9 +111,10 @@ type HTTPTimeouts struct {
103111
// DefaultHTTPTimeouts represents the default timeout values used if further
104112
// configuration is not provided.
105113
var DefaultHTTPTimeouts = HTTPTimeouts{
106-
ReadTimeout: 30 * time.Second,
107-
WriteTimeout: 30 * time.Second,
108-
IdleTimeout: 120 * time.Second,
114+
ReadTimeout: 30 * time.Second,
115+
ReadHeaderTimeout: 30 * time.Second,
116+
WriteTimeout: 30 * time.Second,
117+
IdleTimeout: 120 * time.Second,
109118
}
110119

111120
// DialHTTPWithClient creates a new RPC client that connects to an RPC server over HTTP

0 commit comments

Comments
 (0)