Skip to content

Commit f155ccb

Browse files
committed
refactor(retrypolicy): fix off-by-one in computeBackoff logging
retry-go's OnRetry callback supplies a 1-based retry attempt number, so computeBackoff(n+1, ...) logged a delay that was one doubling ahead of the actual backoff used by the retry loop. Pass n directly. Also drop the unreachable attempt == 0 branch in computeBackoff (retry-go never supplies 0) and skip that case in the test. Addresses review feedback on PR #468 (gemini). Signed-off-by: Zhao Chen <winters.zc@antgroup.com>
1 parent fcdbcb0 commit f155ccb

2 files changed

Lines changed: 5 additions & 8 deletions

File tree

pkg/retrypolicy/retrypolicy.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func Do(ctx context.Context, fn func(ctx context.Context) error, opts DoOpts) er
130130
return retryable
131131
}),
132132
retry.OnRetry(func(n uint, err error) {
133-
backoff := computeBackoff(n+1, initialDelay, maxBackoff)
133+
backoff := computeBackoff(n, initialDelay, maxBackoff)
134134
elapsed := time.Since(startTime)
135135

136136
log.WithFields(log.Fields{
@@ -173,10 +173,9 @@ func computeDynamicParams(fileSize int64) (time.Duration, time.Duration) {
173173

174174
// computeBackoff estimates the backoff duration for display purposes.
175175
// It mirrors the exponential backoff calculation without jitter.
176+
// The attempt parameter is the 1-based retry number as provided by
177+
// retry-go's OnRetry callback, so it is always >= 1.
176178
func computeBackoff(attempt uint, initial, maxDelay time.Duration) time.Duration {
177-
if attempt == 0 {
178-
return initial
179-
}
180179
backoff := time.Duration(float64(initial) * math.Pow(2, float64(attempt-1)))
181180
if backoff > maxDelay {
182181
backoff = maxDelay

pkg/retrypolicy/retrypolicy_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,8 @@ func TestComputeBackoff(t *testing.T) {
523523
initial := 5 * time.Second
524524
maxDelay := 1 * time.Minute
525525

526-
// attempt 0 => initial
527-
if got := computeBackoff(0, initial, maxDelay); got != initial {
528-
t.Errorf("attempt 0: got %v, want %v", got, initial)
529-
}
526+
// retry-go's OnRetry always supplies a 1-based attempt number, so 0 is
527+
// not a value the function is ever called with in production. Start from 1.
530528

531529
// attempt 1 => 5s * 2^0 = 5s
532530
if got := computeBackoff(1, initial, maxDelay); got != 5*time.Second {

0 commit comments

Comments
 (0)