Skip to content

Commit f81211d

Browse files
authored
Add ReadHeaderTimeout to config and HTTP InboundOptions (#2396)
1 parent 1794aa8 commit f81211d

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

transport/http/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func (ts *transportSpec) buildTransport(tc *TransportConfig, k *yarpcconfig.Kit)
164164
// - x-foo
165165
// - x-bar
166166
// shutdownTimeout: 5s
167+
// readHeaderTimeout: 5s
167168
// readTimeout: 10s
168169
// writeTimeout: 10s
169170
type InboundConfig struct {
@@ -172,6 +173,8 @@ type InboundConfig struct {
172173
// The additional headers, starting with x, that should be
173174
// propagated to handlers. This field is optional.
174175
GrabHeaders []string `config:"grabHeaders"`
176+
// ReadHeaderTimeout value set on the http.Server
177+
ReadHeaderTimeout *time.Duration `config:"readHeaderTimeout"`
175178
// ReadTimeout value set on the http.Server
176179
ReadTimeout *time.Duration `config:"readTimeout"`
177180
// WriteTimeout value set on the http.Server
@@ -210,6 +213,13 @@ func (ts *transportSpec) buildInbound(ic *InboundConfig, t transport.Transport,
210213
inboundOptions = append(inboundOptions, ShutdownTimeout(*ic.ShutdownTimeout))
211214
}
212215

216+
if ic.ReadHeaderTimeout != nil {
217+
if *ic.ReadHeaderTimeout < 0 {
218+
return nil, fmt.Errorf("readHeaderTimeout must not be negative, got: %q", ic.ReadHeaderTimeout)
219+
}
220+
inboundOptions = append(inboundOptions, ReadHeaderTimeout(*ic.ReadHeaderTimeout))
221+
}
222+
213223
if ic.ReadTimeout != nil {
214224
if *ic.ReadTimeout < 0 {
215225
return nil, fmt.Errorf("readTimeout must not be negative, got: %q", ic.ReadTimeout)

transport/http/config_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ func TestTransportSpec(t *testing.T) {
7575
}
7676

7777
type wantInbound struct {
78-
Address string
79-
Mux *http.ServeMux
80-
MuxPattern string
81-
GrabHeaders map[string]struct{}
82-
ShutdownTimeout time.Duration
83-
TLSMode yarpctls.Mode
84-
DisableHTTP2 bool
85-
ReadTimeout time.Duration
86-
WriteTimeout time.Duration
78+
Address string
79+
Mux *http.ServeMux
80+
MuxPattern string
81+
GrabHeaders map[string]struct{}
82+
ShutdownTimeout time.Duration
83+
TLSMode yarpctls.Mode
84+
DisableHTTP2 bool
85+
ReadHeaderTimeout time.Duration
86+
ReadTimeout time.Duration
87+
WriteTimeout time.Duration
8788
}
8889

8990
type inboundTest struct {
@@ -261,6 +262,17 @@ func TestTransportSpec(t *testing.T) {
261262
opts: []Option{},
262263
wantInbound: &wantInbound{Address: ":8080", ShutdownTimeout: defaultShutdownTimeout, DisableHTTP2: false},
263264
},
265+
{
266+
desc: "readHeaderTimeout",
267+
cfg: attrs{"address": ":8080", "readHeaderTimeout": "5s"},
268+
opts: []Option{},
269+
wantInbound: &wantInbound{Address: ":8080", ShutdownTimeout: defaultShutdownTimeout, ReadHeaderTimeout: 5 * time.Second},
270+
},
271+
{
272+
desc: "readHeaderTimeout err",
273+
cfg: attrs{"address": ":8080", "readHeaderTimeout": "-1s"},
274+
wantErrors: []string{`readHeaderTimeout must not be negative, got: "-1s"`},
275+
},
264276
{
265277
desc: "ReadTimeout/WriteTimeout",
266278
cfg: attrs{"address": ":8080", "readTimeout": "1s", "writeTimeout": "10s"},
@@ -609,6 +621,7 @@ func TestTransportSpec(t *testing.T) {
609621
assert.Equal(t, "foo", ib.transport.serviceName, "service name must match")
610622
assert.Equal(t, want.TLSMode, ib.tlsMode, "tlsMode should match")
611623
assert.Equal(t, want.DisableHTTP2, ib.disableHTTP2, "disableHTTP2 should match")
624+
assert.Equal(t, want.ReadHeaderTimeout, ib.server.ReadHeaderTimeout, "ReadHeaderTimeout should match")
612625
assert.Equal(t, want.WriteTimeout, ib.server.WriteTimeout, "WriteTimeout should match")
613626
assert.Equal(t, want.ReadTimeout, ib.server.ReadTimeout, "ReadTimeout should match")
614627
}

transport/http/inbound.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ func DisableHTTP2(flag bool) InboundOption {
136136
}
137137
}
138138

139+
// ReadHeaderTimeout returns an InboundOption that sets the http.Server ReadHeaderTimeout
140+
func ReadHeaderTimeout(timeout time.Duration) InboundOption {
141+
return func(i *Inbound) {
142+
i.server.ReadHeaderTimeout = timeout
143+
}
144+
}
145+
139146
// ReadTimeout returns an InboundOption that sets the http.Server ReadTimeout
140147
func ReadTimeout(timeout time.Duration) InboundOption {
141148
return func(i *Inbound) {

transport/http/inbound_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,13 @@ func TestInboundWithHTTPVersion(t *testing.T) {
460460
}
461461

462462
func TestInboundWithTimeouts(t *testing.T) {
463+
t.Run("ReadHeaderTimeout", func(t *testing.T) {
464+
testTransport := NewTransport()
465+
inbound := testTransport.NewInbound("127.0.0.1:8888", ReadHeaderTimeout(5*time.Second))
466+
467+
require.Equal(t, 5*time.Second, inbound.server.ReadHeaderTimeout)
468+
})
469+
463470
t.Run("WriteTimeout", func(t *testing.T) {
464471
testTransport := NewTransport()
465472
inbound := testTransport.NewInbound("127.0.0.1:8888", WriteTimeout(5*time.Second))

0 commit comments

Comments
 (0)