Skip to content

Commit 2ac4067

Browse files
committed
Return context error if that's why attempts are ceasing
1 parent 2c55b7d commit 2ac4067

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

retry.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ func Retry(action Action, strategies ...strategy.Strategy) error {
3333
func RetryWithContext(ctx context.Context, action ActionWithContext, strategies ...strategy.Strategy) error {
3434
var err error
3535

36-
if ctx.Err() != nil {
37-
return ctx.Err()
38-
}
39-
4036
for attempt := uint(0); (0 == attempt || nil != err && nil == ctx.Err()) && shouldAttempt(attempt, sleepFunc(ctx), strategies...); attempt++ {
4137
err = action(ctx, attempt)
4238
}
4339

40+
if ctx.Err() != nil {
41+
return ctx.Err()
42+
}
43+
4444
return err
4545
}
4646

retry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ func TestRetryWithContextSleepIsInterrupted(t *testing.T) {
103103
t.Errorf("expected the action to be tried once, not %d times", numCalls)
104104
}
105105

106-
if expectedErr != err {
107-
t.Error("expected to receive the error returned by the action")
106+
if context.DeadlineExceeded != err {
107+
t.Error("expected a context error")
108108
}
109109
}
110110

0 commit comments

Comments
 (0)