Documentation ¶
Index ¶
- Constants
- Variables
- func Retry(o Operation, b BackOff) error
- func RetryNotify(operation Operation, b BackOff, notify Notify) error
- type BackOff
- type BackOffContext
- type Clock
- type ConstantBackOff
- type ExponentialBackOff
- type Notify
- type Operation
- type PermanentError
- type StopBackOff
- type Ticker
- type ZeroBackOff
Constants ¶
const ( DefaultInitialInterval = 500 * time.Millisecond DefaultRandomizationFactor = 0.5 DefaultMultiplier = 1.5 DefaultMaxInterval = 60 * time.Second DefaultMaxElapsedTime = 15 * time.Minute )
Default values for ExponentialBackOff.
const Stop time.Duration = -1
Stop indicates that no more retries should be made for use in NextBackOff().
Variables ¶
var SystemClock = systemClock{}
SystemClock implements Clock interface that uses time.Now().
Functions ¶
func Retry ¶
Retry the operation o until it does not return error or BackOff stops. o is guaranteed to be run at least once. It is the caller's responsibility to reset b after Retry returns.
If o returns a *PermanentError, the operation is not retried, and the wrapped error is returned.
Retry sleeps the goroutine for the duration returned by BackOff after a failed operation returns.
Types ¶
type BackOff ¶
type BackOff interface { // returns duration NextBackOff() time.Duration // Reset to initial state. Reset() }
BackOff is a backoff policy for retrying an operation.
func WithMaxRetries ¶
WithMaxRetries creates a wrapper around another BackOff, which will return Stop if NextBackOff() has been called too many times since the last time Reset() was called Note: Implementation is not thread-safe.
type BackOffContext ¶
BackOffContext is a backoff policy that stops retrying after the context is canceled.
func WithContext ¶
func WithContext(b BackOff, ctx context.Context) BackOffContext
WithContext returns a BackOffContext with context ctx
ctx must not be nil
type ConstantBackOff ¶
ConstantBackOff is a backoff policy that always returns the same backoff delay. This is in contrast to an exponential backoff policy, which returns a delay that grows longer as you call NextBackOff() over and over again.
func NewConstantBackOff ¶
func NewConstantBackOff(d time.Duration) *ConstantBackOff
func (*ConstantBackOff) NextBackOff ¶
func (b *ConstantBackOff) NextBackOff() time.Duration
func (*ConstantBackOff) Reset ¶
func (b *ConstantBackOff) Reset()
type ExponentialBackOff ¶
type ExponentialBackOff struct { InitialInterval time.Duration RandomizationFactor float64 Multiplier float64 MaxInterval time.Duration // After MaxElapsedTime the ExponentialBackOff stops. // It never stops if MaxElapsedTime == 0. MaxElapsedTime time.Duration Clock Clock // contains filtered or unexported fields }
func NewExponentialBackOff ¶
func NewExponentialBackOff() *ExponentialBackOff
NewExponentialBackOff creates an instance of ExponentialBackOff using default values.
func (*ExponentialBackOff) GetElapsedTime ¶
func (b *ExponentialBackOff) GetElapsedTime() time.Duration
GetElapsedTime returns the elapsed time since an ExponentialBackOff instance is created and is reset when Reset() is called.
The elapsed time is computed using time.Now().UnixNano(). It is safe to call even while the backoff policy is used by a running ticker.
func (*ExponentialBackOff) NextBackOff ¶
func (b *ExponentialBackOff) NextBackOff() time.Duration
NextBackOff calculates the next backoff interval using the formula:
Randomized interval = RetryInterval +/- (RandomizationFactor * RetryInterval)
func (*ExponentialBackOff) Reset ¶
func (b *ExponentialBackOff) Reset()
Reset the interval back to the initial retry interval and restarts the timer.
type Notify ¶
Notify is a notify-on-error function. It receives an operation error and backoff delay if the operation failed (with an error).
NOTE that if the backoff policy stated to stop retrying, the notify function isn't called.
type Operation ¶
type Operation func() error
An Operation is executing by Retry() or RetryNotify(). The operation will be retried using a backoff policy if it returns an error.
type PermanentError ¶
type PermanentError struct {
Err error
}
PermanentError signals that the operation should not be retried.
func Permanent ¶
func Permanent(err error) *PermanentError
Permanent wraps the given err in a *PermanentError.
func (*PermanentError) Error ¶
func (e *PermanentError) Error() string
type StopBackOff ¶
type StopBackOff struct{}
StopBackOff is a fixed backoff policy that always returns backoff.Stop for NextBackOff(), meaning that the operation should never be retried.
func (*StopBackOff) NextBackOff ¶
func (b *StopBackOff) NextBackOff() time.Duration
func (*StopBackOff) Reset ¶
func (b *StopBackOff) Reset()
type Ticker ¶
Ticker holds a channel that delivers `ticks' of a clock at times reported by a BackOff.
Ticks will continue to arrive when the previous operation is still running, so operations that take a while to fail could run in quick succession.
func NewTicker ¶
NewTicker returns a new Ticker containing a channel that will send the time at times specified by the BackOff argument. Ticker is guaranteed to tick at least once. The channel is closed when Stop method is called or BackOff stops. It is not safe to manipulate the provided backoff policy (notably calling NextBackOff or Reset) while the ticker is running.
type ZeroBackOff ¶
type ZeroBackOff struct{}
ZeroBackOff is a fixed backoff policy whose backoff time is always zero, meaning that the operation is retried immediately without waiting, indefinitely.
func (*ZeroBackOff) NextBackOff ¶
func (b *ZeroBackOff) NextBackOff() time.Duration
func (*ZeroBackOff) Reset ¶
func (b *ZeroBackOff) Reset()