Skip to content

Commit a44954a

Browse files
authored
fix: don't set read/write timeout if timeout middleware disabled (#4895)
Signed-off-by: kevin <wanjunfeng@gmail.com>
1 parent f3edd4b commit a44954a

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

rest/engine.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ func (ng *engine) getShedder(priority bool) load.Shedder {
228228
return ng.shedder
229229
}
230230

231+
func (ng *engine) hasTimeout() bool {
232+
return ng.conf.Middlewares.Timeout && ng.timeout > 0
233+
}
234+
231235
// notFoundHandler returns a middleware that handles 404 not found requests.
232236
func (ng *engine) notFoundHandler(next http.Handler) http.Handler {
233237
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -354,16 +358,17 @@ func (ng *engine) use(middleware Middleware) {
354358

355359
func (ng *engine) withTimeout() internal.StartOption {
356360
return func(svr *http.Server) {
357-
timeout := ng.timeout
358-
if timeout > 0 {
359-
// factor 0.8, to avoid clients send longer content-length than the actual content,
360-
// without this timeout setting, the server will time out and respond 503 Service Unavailable,
361-
// which triggers the circuit breaker.
362-
svr.ReadTimeout = 4 * timeout / 5
363-
// factor 1.1, to avoid servers don't have enough time to write responses.
364-
// setting the factor less than 1.0 may lead clients not receiving the responses.
365-
svr.WriteTimeout = 11 * timeout / 10
361+
if !ng.hasTimeout() {
362+
return
366363
}
364+
365+
// factor 0.8, to avoid clients send longer content-length than the actual content,
366+
// without this timeout setting, the server will time out and respond 503 Service Unavailable,
367+
// which triggers the circuit breaker.
368+
svr.ReadTimeout = 4 * ng.timeout / 5
369+
// factor 1.1, to avoid servers don't have enough time to write responses.
370+
// setting the factor less than 1.0 may lead clients not receiving the responses.
371+
svr.WriteTimeout = 11 * ng.timeout / 10
367372
}
368373
}
369374

rest/engine_test.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,12 @@ func TestEngine_withTimeout(t *testing.T) {
394394
for _, test := range tests {
395395
test := test
396396
t.Run(test.name, func(t *testing.T) {
397-
ng := newEngine(RestConf{Timeout: test.timeout})
397+
ng := newEngine(RestConf{
398+
Timeout: test.timeout,
399+
Middlewares: MiddlewaresConf{
400+
Timeout: true,
401+
},
402+
})
398403
svr := &http.Server{}
399404
ng.withTimeout()(svr)
400405

@@ -406,6 +411,62 @@ func TestEngine_withTimeout(t *testing.T) {
406411
}
407412
}
408413

414+
func TestEngine_ReadWriteTimeout(t *testing.T) {
415+
logx.Disable()
416+
417+
tests := []struct {
418+
name string
419+
timeout int64
420+
middleware bool
421+
}{
422+
{
423+
name: "0/false",
424+
timeout: 0,
425+
middleware: false,
426+
},
427+
{
428+
name: "0/true",
429+
timeout: 0,
430+
middleware: true,
431+
},
432+
{
433+
name: "set/false",
434+
timeout: 1000,
435+
middleware: false,
436+
},
437+
{
438+
name: "both set",
439+
timeout: 1000,
440+
middleware: true,
441+
},
442+
}
443+
444+
for _, test := range tests {
445+
test := test
446+
t.Run(test.name, func(t *testing.T) {
447+
ng := newEngine(RestConf{
448+
Timeout: test.timeout,
449+
Middlewares: MiddlewaresConf{
450+
Timeout: test.middleware,
451+
},
452+
})
453+
svr := &http.Server{}
454+
ng.withTimeout()(svr)
455+
456+
assert.Equal(t, time.Duration(0), svr.ReadHeaderTimeout)
457+
assert.Equal(t, time.Duration(0), svr.IdleTimeout)
458+
459+
if test.timeout > 0 && test.middleware {
460+
assert.Equal(t, time.Duration(test.timeout)*time.Millisecond*4/5, svr.ReadTimeout)
461+
assert.Equal(t, time.Duration(test.timeout)*time.Millisecond*11/10, svr.WriteTimeout)
462+
} else {
463+
assert.Equal(t, time.Duration(0), svr.ReadTimeout)
464+
assert.Equal(t, time.Duration(0), svr.WriteTimeout)
465+
}
466+
})
467+
}
468+
}
469+
409470
func TestEngine_start(t *testing.T) {
410471
logx.Disable()
411472

0 commit comments

Comments
 (0)