Skip to content

Commit

Permalink
check max elapsed time
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Dec 18, 2024
1 parent 98570a5 commit 60056ec
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,17 @@ func Retry[T any](ctx context.Context, operation Operation[T], opts ...RetryOpti
return res, err
}

// Stop retrying if maximum elapsed time exceeded.
// TODO: Stop if next backoff is greater than max elapsed time.
if time.Since(startedAt) > args.MaxElapsedTime {
return res, err
}

// Handle permanent errors without retrying.
var permanent *PermanentError
if errors.As(err, &permanent) {
return res, err
}

// Stop retrying if context is cancelled.
if cerr := ctx.Err(); cerr != nil {
return res, cerr
}

// Calculate next backoff duration.
next := args.BackOff.NextBackOff()
if next == Stop {
Expand All @@ -120,9 +119,9 @@ func Retry[T any](ctx context.Context, operation Operation[T], opts ...RetryOpti
args.BackOff.Reset()
}

// Stop retrying if context is cancelled.
if cerr := ctx.Err(); cerr != nil {
return res, cerr
// Stop retrying if maximum elapsed time exceeded.
if time.Since(startedAt)+next > args.MaxElapsedTime {
return res, err
}

// Notify on error if a notifier function is provided.
Expand Down

0 comments on commit 60056ec

Please sign in to comment.