Skip to content

Commit

Permalink
Return context error if that's why attempts are ceasing
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyDWJones committed Mar 13, 2021
1 parent 2c55b7d commit 2ac4067
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func Retry(action Action, strategies ...strategy.Strategy) error {
func RetryWithContext(ctx context.Context, action ActionWithContext, strategies ...strategy.Strategy) error {
var err error

if ctx.Err() != nil {
return ctx.Err()
}

for attempt := uint(0); (0 == attempt || nil != err && nil == ctx.Err()) && shouldAttempt(attempt, sleepFunc(ctx), strategies...); attempt++ {
err = action(ctx, attempt)
}

if ctx.Err() != nil {
return ctx.Err()
}

return err
}

Expand Down
4 changes: 2 additions & 2 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func TestRetryWithContextSleepIsInterrupted(t *testing.T) {
t.Errorf("expected the action to be tried once, not %d times", numCalls)
}

if expectedErr != err {
t.Error("expected to receive the error returned by the action")
if context.DeadlineExceeded != err {
t.Error("expected a context error")
}
}

Expand Down

0 comments on commit 2ac4067

Please sign in to comment.