Skip to content

Commit

Permalink
fix: Backoff retry: Don't multiply on first attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
minitauros committed Feb 15, 2022
1 parent e9a383c commit 0fcb5b1
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions backoff_retrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func (r *BackOffRetrier) Retry(numTimes int, cb func() error) error {
}
err := cb()
if err != nil {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
if sleepDur == 0 {
// First attempt. Don't multiply yet.
sleepDur = delay
} else {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
}
}
return err
})
Expand All @@ -57,7 +62,12 @@ func (r *BackOffRetrier) RetryCtx(ctx context.Context, numTimes int, cb func() e
}
err = cb()
if err != nil {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
if sleepDur == 0 {
// First attempt. Don't multiply yet.
sleepDur = delay
} else {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
}
}
return err
})
Expand All @@ -74,7 +84,12 @@ func (r *BackOffRetrier) RetryWithStop(numTimes int, cb func(stop func()) error)
}
err := cb(stop)
if err != nil {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
if sleepDur == 0 {
// First attempt. Don't multiply yet.
sleepDur = delay
} else {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
}
}
return err
})
Expand All @@ -96,7 +111,12 @@ func (r *BackOffRetrier) RetryWithStopCtx(ctx context.Context, numTimes int, cb
}
err = cb(stop)
if err != nil {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
if sleepDur == 0 {
// First attempt. Don't multiply yet.
sleepDur = delay
} else {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
}
}
return err
})
Expand Down

0 comments on commit 0fcb5b1

Please sign in to comment.