Skip to content

Commit 60056ec

Browse files
committed
check max elapsed time
1 parent 98570a5 commit 60056ec

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

retry.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,17 @@ func Retry[T any](ctx context.Context, operation Operation[T], opts ...RetryOpti
9595
return res, err
9696
}
9797

98-
// Stop retrying if maximum elapsed time exceeded.
99-
// TODO: Stop if next backoff is greater than max elapsed time.
100-
if time.Since(startedAt) > args.MaxElapsedTime {
101-
return res, err
102-
}
103-
10498
// Handle permanent errors without retrying.
10599
var permanent *PermanentError
106100
if errors.As(err, &permanent) {
107101
return res, err
108102
}
109103

104+
// Stop retrying if context is cancelled.
105+
if cerr := ctx.Err(); cerr != nil {
106+
return res, cerr
107+
}
108+
110109
// Calculate next backoff duration.
111110
next := args.BackOff.NextBackOff()
112111
if next == Stop {
@@ -120,9 +119,9 @@ func Retry[T any](ctx context.Context, operation Operation[T], opts ...RetryOpti
120119
args.BackOff.Reset()
121120
}
122121

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

128127
// Notify on error if a notifier function is provided.

0 commit comments

Comments
 (0)