retry

package
v1.15.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2023 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Jitter

type Jitter func(baseTime time.Duration) time.Duration

Jitter should return a new wait duration optionally with some time added or removed to create some randomness in wait time.

func NewJitter

func NewJitter(percent int64) Jitter

NewJitter returns a new random Jitter that is up to percent longer than the original wait time.

type Waiter

type Waiter struct {
	// MinFailures before exponential backoff starts. Any failures before
	// MinFailures is reached will wait MinWait time.
	MinFailures uint
	// MinWait time. Returned after the first failure.
	MinWait time.Duration
	// MaxWait time applied before Jitter. Note that the actual maximum wait time
	// is MaxWait + MaxWait * Jitter.
	MaxWait time.Duration
	// Jitter to add to each wait time. The Jitter is applied after MaxWait, which
	// may cause the actual wait time to exceed MaxWait.
	Jitter Jitter
	// Factor is the multiplier to use when calculating the delay. Defaults to
	// 1 second.
	Factor time.Duration
	// contains filtered or unexported fields
}

Waiter records the number of failures and performs exponential backoff when there are consecutive failures.

func (*Waiter) Failures

func (w *Waiter) Failures() int

Failures returns the count of consecutive failures.

func (*Waiter) NextWait added in v1.14.0

func (w *Waiter) NextWait() time.Duration

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 (*Waiter) Reset

func (w *Waiter) Reset()

Reset the failure count to 0. Reset must be called if the operation done after Wait did not fail.

func (*Waiter) RetryLoop added in v1.14.0

func (w *Waiter) RetryLoop(ctx context.Context, operation func() error) error

RetryLoop retries an operation until either operation completes without error or Waiter's context is canceled.

func (*Waiter) Wait

func (w *Waiter) Wait(ctx context.Context) error

Wait increases the number of failures by one, and then blocks until the context is cancelled, or until the wait time is reached.

The wait time increases exponentially as the number of failures increases. Every call to Wait increments the failures count, so Reset must be called after Wait when there wasn't a failure.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL