Skip to content

Commit 7a49f1b

Browse files
committed
fix(retry): prioritize cancellation in Next() method and update test expectations
1 parent b735014 commit 7a49f1b

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

pkg/util/retry/retry.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ func (r *Retry) Next() bool {
172172
return false
173173
}
174174

175+
// Check for cancellation first to prioritize over timer.
176+
select {
177+
case <-r.opts.Closer:
178+
return false
179+
case <-r.ctx.Done():
180+
return false
181+
default:
182+
}
183+
175184
backoff, actualWait, shouldAttempt := r.calcDurationScopedBackoff()
176185

177186
if !shouldAttempt && r.opts.PreemptivelyCancel {
@@ -192,15 +201,6 @@ func (r *Retry) Next() bool {
192201
r.backingOffHook(actualWait)
193202
}
194203

195-
// Check for cancellation first to prioritize over timer.
196-
select {
197-
case <-r.opts.Closer:
198-
return false
199-
case <-r.ctx.Done():
200-
return false
201-
default:
202-
}
203-
204204
select {
205205
case <-r.opts.Closer:
206206
return false

pkg/util/retry/retry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ func TestRetryWithMaxDuration(t *testing.T) {
469469
preRetryFunc: func() {
470470
cancelCtxFunc()
471471
},
472-
expectedTimeSpent: time.Millisecond,
472+
expectedTimeSpent: 0,
473473
expectedErr: true,
474474
},
475475
{
@@ -486,7 +486,7 @@ func TestRetryWithMaxDuration(t *testing.T) {
486486
preRetryFunc: func() {
487487
close(closeCh)
488488
},
489-
expectedTimeSpent: time.Millisecond,
489+
expectedTimeSpent: 0,
490490
expectedErr: true,
491491
},
492492
}

0 commit comments

Comments
 (0)