Revert "fix: api group set timeout: 0s not working." (#4931)

This commit is contained in:
Kevin Wan
2025-06-08 23:14:38 +08:00
committed by GitHub
parent 19fec36d24
commit 0f2b589d4d
5 changed files with 17 additions and 29 deletions

View File

@@ -62,8 +62,8 @@ func (ng *engine) addRoutes(r featuredRoutes) {
// need to guarantee the timeout is the max of all routes // need to guarantee the timeout is the max of all routes
// otherwise impossible to set http.Server.ReadTimeout & WriteTimeout // otherwise impossible to set http.Server.ReadTimeout & WriteTimeout
if r.timeout != nil { if r.timeout > ng.timeout {
ng.timeout = *r.timeout ng.timeout = r.timeout
} }
} }
@@ -192,9 +192,9 @@ func (ng *engine) checkedMaxBytes(bytes int64) int64 {
return ng.conf.MaxBytes return ng.conf.MaxBytes
} }
func (ng *engine) checkedTimeout(timeout *time.Duration) time.Duration { func (ng *engine) checkedTimeout(timeout time.Duration) time.Duration {
if timeout != nil { if timeout > 0 {
return *timeout return timeout
} }
return time.Duration(ng.conf.Timeout) * time.Millisecond return time.Duration(ng.conf.Timeout) * time.Millisecond

View File

@@ -64,9 +64,6 @@ Verbose: true
`, `,
} }
minuteDuration := time.Minute
secondDuration := time.Second
routes := []featuredRoutes{ routes := []featuredRoutes{
{ {
jwt: jwtSetting{}, jwt: jwtSetting{},
@@ -76,7 +73,7 @@ Verbose: true
Path: "/", Path: "/",
Handler: func(w http.ResponseWriter, r *http.Request) {}, Handler: func(w http.ResponseWriter, r *http.Request) {},
}}, }},
timeout: &minuteDuration, timeout: time.Minute,
}, },
{ {
priority: true, priority: true,
@@ -87,7 +84,7 @@ Verbose: true
Path: "/", Path: "/",
Handler: func(w http.ResponseWriter, r *http.Request) {}, Handler: func(w http.ResponseWriter, r *http.Request) {},
}}, }},
timeout: &secondDuration, timeout: time.Second,
}, },
{ {
priority: true, priority: true,
@@ -230,8 +227,8 @@ Verbose: true
})) }))
timeout := time.Second * 3 timeout := time.Second * 3
if route.timeout != nil { if route.timeout > timeout {
timeout = *route.timeout timeout = route.timeout
} }
assert.Equal(t, timeout, ng.timeout) assert.Equal(t, timeout, ng.timeout)
}) })
@@ -239,14 +236,10 @@ Verbose: true
} }
} }
func getPtrTimeDuration(dur time.Duration) *time.Duration {
return &dur
}
func TestEngine_checkedTimeout(t *testing.T) { func TestEngine_checkedTimeout(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
timeout *time.Duration timeout time.Duration
expect time.Duration expect time.Duration
}{ }{
{ {
@@ -255,24 +248,19 @@ func TestEngine_checkedTimeout(t *testing.T) {
}, },
{ {
name: "less", name: "less",
timeout: getPtrTimeDuration(time.Millisecond * 500), timeout: time.Millisecond * 500,
expect: time.Millisecond * 500, expect: time.Millisecond * 500,
}, },
{ {
name: "equal", name: "equal",
timeout: getPtrTimeDuration(time.Second), timeout: time.Second,
expect: time.Second, expect: time.Second,
}, },
{ {
name: "more", name: "more",
timeout: getPtrTimeDuration(time.Millisecond * 1500), timeout: time.Millisecond * 1500,
expect: time.Millisecond * 1500, expect: time.Millisecond * 1500,
}, },
{
name: "set zero",
timeout: getPtrTimeDuration(0),
expect: 0,
},
} }
ng := newEngine(RestConf{ ng := newEngine(RestConf{

View File

@@ -283,14 +283,14 @@ func WithSignature(signature SignatureConf) RouteOption {
func WithSSE() RouteOption { func WithSSE() RouteOption {
return func(r *featuredRoutes) { return func(r *featuredRoutes) {
r.sse = true r.sse = true
r.timeout = nil r.timeout = 0
} }
} }
// WithTimeout returns a RouteOption to set timeout with given value. // WithTimeout returns a RouteOption to set timeout with given value.
func WithTimeout(timeout time.Duration) RouteOption { func WithTimeout(timeout time.Duration) RouteOption {
return func(r *featuredRoutes) { return func(r *featuredRoutes) {
r.timeout = &timeout r.timeout = timeout
} }
} }

View File

@@ -345,7 +345,7 @@ func TestWithPriority(t *testing.T) {
func TestWithTimeout(t *testing.T) { func TestWithTimeout(t *testing.T) {
var fr featuredRoutes var fr featuredRoutes
WithTimeout(time.Hour)(&fr) WithTimeout(time.Hour)(&fr)
assert.Equal(t, time.Hour, *fr.timeout) assert.Equal(t, time.Hour, fr.timeout)
} }
func TestWithTLSConfig(t *testing.T) { func TestWithTLSConfig(t *testing.T) {

View File

@@ -31,7 +31,7 @@ type (
} }
featuredRoutes struct { featuredRoutes struct {
timeout *time.Duration timeout time.Duration
priority bool priority bool
jwt jwtSetting jwt jwtSetting
signature signatureSetting signature signatureSetting