Skip to content

Commit 0f2b589

Browse files
authored
Revert "fix: api group set timeout: 0s not working." (#4931)
1 parent 19fec36 commit 0f2b589

File tree

5 files changed

+17
-29
lines changed

5 files changed

+17
-29
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 != nil {
66-
ng.timeout = *r.timeout
65+
if r.timeout > ng.timeout {
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 != nil {
197-
return *timeout
195+
func (ng *engine) checkedTimeout(timeout time.Duration) time.Duration {
196+
if timeout > 0 {
197+
return timeout
198198
}
199199

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

rest/engine_test.go

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

67-
minuteDuration := time.Minute
68-
secondDuration := time.Second
69-
7067
routes := []featuredRoutes{
7168
{
7269
jwt: jwtSetting{},
@@ -76,7 +73,7 @@ Verbose: true
7673
Path: "/",
7774
Handler: func(w http.ResponseWriter, r *http.Request) {},
7875
}},
79-
timeout: &minuteDuration,
76+
timeout: time.Minute,
8077
},
8178
{
8279
priority: true,
@@ -87,7 +84,7 @@ Verbose: true
8784
Path: "/",
8885
Handler: func(w http.ResponseWriter, r *http.Request) {},
8986
}},
90-
timeout: &secondDuration,
87+
timeout: time.Second,
9188
},
9289
{
9390
priority: true,
@@ -230,23 +227,19 @@ Verbose: true
230227
}))
231228

232229
timeout := time.Second * 3
233-
if route.timeout != nil {
234-
timeout = *route.timeout
230+
if route.timeout > timeout {
231+
timeout = route.timeout
235232
}
236233
assert.Equal(t, timeout, ng.timeout)
237234
})
238235
}
239236
}
240237
}
241238

242-
func getPtrTimeDuration(dur time.Duration) *time.Duration {
243-
return &dur
244-
}
245-
246239
func TestEngine_checkedTimeout(t *testing.T) {
247240
tests := []struct {
248241
name string
249-
timeout *time.Duration
242+
timeout time.Duration
250243
expect time.Duration
251244
}{
252245
{
@@ -255,24 +248,19 @@ func TestEngine_checkedTimeout(t *testing.T) {
255248
},
256249
{
257250
name: "less",
258-
timeout: getPtrTimeDuration(time.Millisecond * 500),
251+
timeout: time.Millisecond * 500,
259252
expect: time.Millisecond * 500,
260253
},
261254
{
262255
name: "equal",
263-
timeout: getPtrTimeDuration(time.Second),
256+
timeout: time.Second,
264257
expect: time.Second,
265258
},
266259
{
267260
name: "more",
268-
timeout: getPtrTimeDuration(time.Millisecond * 1500),
261+
timeout: time.Millisecond * 1500,
269262
expect: time.Millisecond * 1500,
270263
},
271-
{
272-
name: "set zero",
273-
timeout: getPtrTimeDuration(0),
274-
expect: 0,
275-
},
276264
}
277265

278266
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 = nil
286+
r.timeout = 0
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)