2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-10-01 23:02:32 +00:00
|
|
|
package retry
|
2019-04-26 17:38:39 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-01 05:14:21 +00:00
|
|
|
"context"
|
2022-10-17 21:57:48 +00:00
|
|
|
"fmt"
|
2020-10-01 05:14:21 +00:00
|
|
|
"math/rand"
|
2019-04-26 17:38:39 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
// Jitter should return a new wait duration optionally with some time added or
|
|
|
|
// removed to create some randomness in wait time.
|
|
|
|
type Jitter func(baseTime time.Duration) time.Duration
|
2019-04-26 17:38:39 +00:00
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
// NewJitter returns a new random Jitter that is up to percent longer than the
|
|
|
|
// original wait time.
|
|
|
|
func NewJitter(percent int64) Jitter {
|
2019-04-26 17:38:39 +00:00
|
|
|
if percent < 0 {
|
|
|
|
percent = 0
|
|
|
|
}
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
return func(baseTime time.Duration) time.Duration {
|
|
|
|
if percent == 0 {
|
|
|
|
return baseTime
|
|
|
|
}
|
|
|
|
max := (int64(baseTime) * percent) / 100
|
|
|
|
if max < 0 { // overflow
|
|
|
|
return baseTime
|
|
|
|
}
|
|
|
|
return baseTime + time.Duration(rand.Int63n(max))
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
// Waiter records the number of failures and performs exponential backoff when
|
2022-10-17 21:57:48 +00:00
|
|
|
// there are consecutive failures.
|
2020-10-01 23:02:32 +00:00
|
|
|
type Waiter struct {
|
2020-10-01 05:14:21 +00:00
|
|
|
// MinFailures before exponential backoff starts. Any failures before
|
|
|
|
// MinFailures is reached will wait MinWait time.
|
2020-10-01 23:03:44 +00:00
|
|
|
MinFailures uint
|
2020-10-01 05:14:21 +00:00
|
|
|
// MinWait time. Returned after the first failure.
|
|
|
|
MinWait time.Duration
|
2021-04-07 22:33:11 +00:00
|
|
|
// MaxWait time applied before Jitter. Note that the actual maximum wait time
|
|
|
|
// is MaxWait + MaxWait * Jitter.
|
2020-10-01 05:14:21 +00:00
|
|
|
MaxWait time.Duration
|
2021-04-07 22:33:11 +00:00
|
|
|
// Jitter to add to each wait time. The Jitter is applied after MaxWait, which
|
|
|
|
// may cause the actual wait time to exceed MaxWait.
|
2020-10-01 05:14:21 +00:00
|
|
|
Jitter Jitter
|
|
|
|
// Factor is the multiplier to use when calculating the delay. Defaults to
|
|
|
|
// 1 second.
|
|
|
|
Factor time.Duration
|
|
|
|
failures uint
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
// delay calculates the time to wait based on the number of failures
|
|
|
|
func (w *Waiter) delay() time.Duration {
|
|
|
|
if w.failures <= w.MinFailures {
|
|
|
|
return w.MinWait
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
2020-10-01 05:14:21 +00:00
|
|
|
factor := w.Factor
|
|
|
|
if factor == 0 {
|
|
|
|
factor = time.Second
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
shift := w.failures - w.MinFailures - 1
|
|
|
|
waitTime := w.MaxWait
|
|
|
|
if shift < 31 {
|
|
|
|
waitTime = (1 << shift) * factor
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
2021-04-07 22:33:11 +00:00
|
|
|
// apply MaxWait before jitter so that multiple waiters with the same MaxWait
|
|
|
|
// do not converge when they hit their max.
|
|
|
|
if w.MaxWait != 0 && waitTime > w.MaxWait {
|
|
|
|
waitTime = w.MaxWait
|
|
|
|
}
|
2020-10-01 05:14:21 +00:00
|
|
|
if w.Jitter != nil {
|
|
|
|
waitTime = w.Jitter(waitTime)
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
2020-10-01 05:14:21 +00:00
|
|
|
if waitTime < w.MinWait {
|
|
|
|
return w.MinWait
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
return waitTime
|
|
|
|
}
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
// Reset the failure count to 0.
|
2022-09-09 19:06:48 +00:00
|
|
|
// Reset must be called if the operation done after Wait did not fail.
|
2020-10-01 05:14:21 +00:00
|
|
|
func (w *Waiter) Reset() {
|
|
|
|
w.failures = 0
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
// Failures returns the count of consecutive failures.
|
|
|
|
func (w *Waiter) Failures() int {
|
|
|
|
return int(w.failures)
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 19:06:48 +00:00
|
|
|
// Wait increases the number of failures by one, and then blocks until the context
|
2020-10-01 05:14:21 +00:00
|
|
|
// is cancelled, or until the wait time is reached.
|
2022-09-09 19:06:48 +00:00
|
|
|
//
|
2020-10-01 05:14:21 +00:00
|
|
|
// The wait time increases exponentially as the number of failures increases.
|
2022-09-09 19:06:48 +00:00
|
|
|
// Every call to Wait increments the failures count, so Reset must be called
|
|
|
|
// after Wait when there wasn't a failure.
|
|
|
|
//
|
2022-09-21 15:55:19 +00:00
|
|
|
// The only non-nil error that Wait returns will come from ctx.Err(),
|
|
|
|
// such as when the context is canceled. This makes it suitable for
|
|
|
|
// long-running routines that do not get re-initialized, such as replication.
|
2020-10-01 05:14:21 +00:00
|
|
|
func (w *Waiter) Wait(ctx context.Context) error {
|
|
|
|
w.failures++
|
|
|
|
timer := time.NewTimer(w.delay())
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
timer.Stop()
|
|
|
|
return ctx.Err()
|
|
|
|
case <-timer.C:
|
|
|
|
return nil
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-26 18:58:15 +00:00
|
|
|
|
|
|
|
// NextWait returns the period the next call to Wait with block for assuming
|
|
|
|
// it's context is not cancelled. It's useful for informing a user how long
|
|
|
|
// it will be before the next attempt is made.
|
|
|
|
func (w *Waiter) NextWait() time.Duration {
|
|
|
|
return w.delay()
|
|
|
|
}
|
2022-10-17 21:57:48 +00:00
|
|
|
|
|
|
|
// RetryLoop retries an operation until either operation completes without error
|
|
|
|
// or Waiter's context is canceled.
|
|
|
|
func (w *Waiter) RetryLoop(ctx context.Context, operation func() error) error {
|
|
|
|
var lastError error
|
|
|
|
for {
|
|
|
|
if err := w.Wait(ctx); err != nil {
|
|
|
|
// The error will only be non-nil if the context is canceled.
|
|
|
|
return fmt.Errorf("could not retry operation: %w", lastError)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := operation(); err == nil {
|
|
|
|
// Reset the failure count seen by the waiter if there was no error.
|
|
|
|
w.Reset()
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
lastError = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|