Skip to content

Commit 19fec36

Browse files
authored
fix: api group set timeout: 0s not working. (#4785)
1 parent f037bf3 commit 19fec36

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

rest/engine.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func (ng *engine) addRoutes(r featuredRoutes) {
6262

6363
// need to guarantee the timeout is the max of all routes
6464
// otherwise impossible to set http.Server.ReadTimeout & WriteTimeout
65-
if r.timeout > ng.timeout {
66-
ng.timeout = r.timeout
65+
if r.timeout != nil {
66+
ng.timeout = *r.timeout
6767
}
6868
}
6969

@@ -192,9 +192,9 @@ func (ng *engine) checkedMaxBytes(bytes int64) int64 {
192192
return ng.conf.MaxBytes
193193
}
194194

195-
func (ng *engine) checkedTimeout(timeout time.Duration) time.Duration {
196-
if timeout > 0 {
197-
return timeout
195+
func (ng *engine) checkedTimeout(timeout *time.Duration) time.Duration {
196+
if timeout != nil {
197+
return *timeout
198198
}
199199

200200
return time.Duration(ng.conf.Timeout) * time.Millisecond

rest/engine_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Verbose: true
6464
`,
6565
}
6666

67+
minuteDuration := time.Minute
68+
secondDuration := time.Second
69+
6770
routes := []featuredRoutes{
6871
{
6972
jwt: jwtSetting{},
@@ -73,7 +76,7 @@ Verbose: true
7376
Path: "/",
7477
Handler: func(w http.ResponseWriter, r *http.Request) {},
7578
}},
76-
timeout: time.Minute,
79+
timeout: &minuteDuration,
7780
},
7881
{
7982
priority: true,
@@ -84,7 +87,7 @@ Verbose: true
8487
Path: "/",
8588
Handler: func(w http.ResponseWriter, r *http.Request) {},
8689
}},
87-
timeout: time.Second,
90+
timeout: &secondDuration,
8891
},
8992
{
9093
priority: true,
@@ -227,19 +230,23 @@ Verbose: true
227230
}))
228231

229232
timeout := time.Second * 3
230-
if route.timeout > timeout {
231-
timeout = route.timeout
233+
if route.timeout != nil {
234+
timeout = *route.timeout
232235
}
233236
assert.Equal(t, timeout, ng.timeout)
234237
})
235238
}
236239
}
237240
}
238241

242+
func getPtrTimeDuration(dur time.Duration) *time.Duration {
243+
return &dur
244+
}
245+
239246
func TestEngine_checkedTimeout(t *testing.T) {
240247
tests := []struct {
241248
name string
242-
timeout time.Duration
249+
timeout *time.Duration
243250
expect time.Duration
244251
}{
245252
{
@@ -248,19 +255,24 @@ func TestEngine_checkedTimeout(t *testing.T) {
248255
},
249256
{
250257
name: "less",
251-
timeout: time.Millisecond * 500,
258+
timeout: getPtrTimeDuration(time.Millisecond * 500),
252259
expect: time.Millisecond * 500,
253260
},
254261
{
255262
name: "equal",
256-
timeout: time.Second,
263+
timeout: getPtrTimeDuration(time.Second),
257264
expect: time.Second,
258265
},
259266
{
260267
name: "more",
261-
timeout: time.Millisecond * 1500,
268+
timeout: getPtrTimeDuration(time.Millisecond * 1500),
262269
expect: time.Millisecond * 1500,
263270
},
271+
{
272+
name: "set zero",
273+
timeout: getPtrTimeDuration(0),
274+
expect: 0,
275+
},
264276
}
265277

266278
ng := newEngine(RestConf{

rest/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,14 @@ func WithSignature(signature SignatureConf) RouteOption {
283283
func WithSSE() RouteOption {
284284
return func(r *featuredRoutes) {
285285
r.sse = true
286-
r.timeout = 0
286+
r.timeout = nil
287287
}
288288
}
289289

290290
// WithTimeout returns a RouteOption to set timeout with given value.
291291
func WithTimeout(timeout time.Duration) RouteOption {
292292
return func(r *featuredRoutes) {
293-
r.timeout = timeout
293+
r.timeout = &timeout
294294
}
295295
}
296296

rest/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func TestWithPriority(t *testing.T) {
345345
func TestWithTimeout(t *testing.T) {
346346
var fr featuredRoutes
347347
WithTimeout(time.Hour)(&fr)
348-
assert.Equal(t, time.Hour, fr.timeout)
348+
assert.Equal(t, time.Hour, *fr.timeout)
349349
}
350350

351351
func TestWithTLSConfig(t *testing.T) {

rest/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type (
3131
}
3232

3333
featuredRoutes struct {
34-
timeout time.Duration
34+
timeout *time.Duration
3535
priority bool
3636
jwt jwtSetting
3737
signature signatureSetting

0 commit comments

Comments
 (0)