mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 06:59:59 +08:00
fix: api group set timeout: 0s not working. (#4785)
This commit is contained in:
@@ -62,8 +62,8 @@ func (ng *engine) addRoutes(r featuredRoutes) {
|
||||
|
||||
// need to guarantee the timeout is the max of all routes
|
||||
// otherwise impossible to set http.Server.ReadTimeout & WriteTimeout
|
||||
if r.timeout > ng.timeout {
|
||||
ng.timeout = r.timeout
|
||||
if r.timeout != nil {
|
||||
ng.timeout = *r.timeout
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,9 +192,9 @@ func (ng *engine) checkedMaxBytes(bytes int64) int64 {
|
||||
return ng.conf.MaxBytes
|
||||
}
|
||||
|
||||
func (ng *engine) checkedTimeout(timeout time.Duration) time.Duration {
|
||||
if timeout > 0 {
|
||||
return timeout
|
||||
func (ng *engine) checkedTimeout(timeout *time.Duration) time.Duration {
|
||||
if timeout != nil {
|
||||
return *timeout
|
||||
}
|
||||
|
||||
return time.Duration(ng.conf.Timeout) * time.Millisecond
|
||||
|
||||
@@ -64,6 +64,9 @@ Verbose: true
|
||||
`,
|
||||
}
|
||||
|
||||
minuteDuration := time.Minute
|
||||
secondDuration := time.Second
|
||||
|
||||
routes := []featuredRoutes{
|
||||
{
|
||||
jwt: jwtSetting{},
|
||||
@@ -73,7 +76,7 @@ Verbose: true
|
||||
Path: "/",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {},
|
||||
}},
|
||||
timeout: time.Minute,
|
||||
timeout: &minuteDuration,
|
||||
},
|
||||
{
|
||||
priority: true,
|
||||
@@ -84,7 +87,7 @@ Verbose: true
|
||||
Path: "/",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {},
|
||||
}},
|
||||
timeout: time.Second,
|
||||
timeout: &secondDuration,
|
||||
},
|
||||
{
|
||||
priority: true,
|
||||
@@ -227,8 +230,8 @@ Verbose: true
|
||||
}))
|
||||
|
||||
timeout := time.Second * 3
|
||||
if route.timeout > timeout {
|
||||
timeout = route.timeout
|
||||
if route.timeout != nil {
|
||||
timeout = *route.timeout
|
||||
}
|
||||
assert.Equal(t, timeout, ng.timeout)
|
||||
})
|
||||
@@ -236,10 +239,14 @@ Verbose: true
|
||||
}
|
||||
}
|
||||
|
||||
func getPtrTimeDuration(dur time.Duration) *time.Duration {
|
||||
return &dur
|
||||
}
|
||||
|
||||
func TestEngine_checkedTimeout(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
timeout time.Duration
|
||||
timeout *time.Duration
|
||||
expect time.Duration
|
||||
}{
|
||||
{
|
||||
@@ -248,19 +255,24 @@ func TestEngine_checkedTimeout(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "less",
|
||||
timeout: time.Millisecond * 500,
|
||||
timeout: getPtrTimeDuration(time.Millisecond * 500),
|
||||
expect: time.Millisecond * 500,
|
||||
},
|
||||
{
|
||||
name: "equal",
|
||||
timeout: time.Second,
|
||||
timeout: getPtrTimeDuration(time.Second),
|
||||
expect: time.Second,
|
||||
},
|
||||
{
|
||||
name: "more",
|
||||
timeout: time.Millisecond * 1500,
|
||||
timeout: getPtrTimeDuration(time.Millisecond * 1500),
|
||||
expect: time.Millisecond * 1500,
|
||||
},
|
||||
{
|
||||
name: "set zero",
|
||||
timeout: getPtrTimeDuration(0),
|
||||
expect: 0,
|
||||
},
|
||||
}
|
||||
|
||||
ng := newEngine(RestConf{
|
||||
|
||||
@@ -283,14 +283,14 @@ func WithSignature(signature SignatureConf) RouteOption {
|
||||
func WithSSE() RouteOption {
|
||||
return func(r *featuredRoutes) {
|
||||
r.sse = true
|
||||
r.timeout = 0
|
||||
r.timeout = nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithTimeout returns a RouteOption to set timeout with given value.
|
||||
func WithTimeout(timeout time.Duration) RouteOption {
|
||||
return func(r *featuredRoutes) {
|
||||
r.timeout = timeout
|
||||
r.timeout = &timeout
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -345,7 +345,7 @@ func TestWithPriority(t *testing.T) {
|
||||
func TestWithTimeout(t *testing.T) {
|
||||
var fr featuredRoutes
|
||||
WithTimeout(time.Hour)(&fr)
|
||||
assert.Equal(t, time.Hour, fr.timeout)
|
||||
assert.Equal(t, time.Hour, *fr.timeout)
|
||||
}
|
||||
|
||||
func TestWithTLSConfig(t *testing.T) {
|
||||
|
||||
@@ -31,7 +31,7 @@ type (
|
||||
}
|
||||
|
||||
featuredRoutes struct {
|
||||
timeout time.Duration
|
||||
timeout *time.Duration
|
||||
priority bool
|
||||
jwt jwtSetting
|
||||
signature signatureSetting
|
||||
|
||||
Reference in New Issue
Block a user