Skip to content

Commit 9c96c3d

Browse files
authored
Merge pull request #337 from cschleiden/stop-retries-sooner
Stop retries sooner
2 parents b70a356 + 91fd689 commit 9c96c3d

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

workflow/retries.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ func WithRetries[T any](ctx Context, retryOptions RetryOptions, fn func(ctx Cont
6161
break
6262
}
6363

64+
attempt++
65+
66+
if attempt >= retryOptions.MaxAttempts {
67+
// Reached maximum number of attempts, abort retries
68+
break
69+
}
70+
6471
if err == Canceled {
6572
break
6673
}
@@ -75,19 +82,14 @@ func WithRetries[T any](ctx Context, retryOptions RetryOptions, fn func(ctx Cont
7582
backoffDuration = time.Duration(math.Min(float64(backoffDuration), float64(retryOptions.MaxRetryInterval)))
7683
}
7784

78-
if err := Sleep(ctx, backoffDuration); err != nil {
79-
r.Set(*new(T), err)
80-
return
81-
}
82-
83-
if !retryExpiration.IsZero() && Now(ctx).After(retryExpiration) {
84-
// Reached maximum retry time, abort retries
85+
if !retryExpiration.IsZero() && Now(ctx).Add(backoffDuration).After(retryExpiration) {
86+
// Waiting would reach maximum retry time, abort retries
8587
break
8688
}
8789

88-
attempt++
89-
if attempt >= retryOptions.MaxAttempts {
90-
break
90+
if err := Sleep(ctx, backoffDuration); err != nil {
91+
r.Set(*new(T), err)
92+
return
9193
}
9294

9395
f = fn(ctx, attempt)

0 commit comments

Comments
 (0)