retry

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: BSD-3-Clause Imports: 4 Imported by: 4

Documentation

Overview

Package retry provides functionality for controlling retries.

Exponential Backoff

Backoff duration after $retryAttempt (first attempt is 0) is defined as:

backoff =
  min(initialBackoff * pow(multiplier, $retryAttempt), maxBackoff == 0 ? +Inf : maxBackoff) *
    (1.0 - randomizationFactor + 2 * rand(0, randomizationFactor))

Retrying Failures

Example 1: Opening connection.

retry.Do(ctx, func() error {
	return openConnection(&handle)
})

Retry Loops

Example 1: Event pulling and dispatching.

for r := retry.Start(ctx, WithMaxBackoff(200 * time.Millisecond)); r.Next(); {
	events := pull();
	if len(events) > 0 {
		dispatch(events)
		r.Reset()
	}
}
return ctx.Err()

Example 2: Retrying CAS operations.

for r := retry.Start(ctx); r.Next(); {
	success, err := kv.CompareAndSwap(key, value)
	switch {
	case err != nil:
		return err
	case success:
		return nil
	default:
		continue
	}
}
return ctx.Err()

Example 3: Waiting for status.

for r := retry.Start(ctx); r.Next(); {
	if serverStatus() == StatusRunning {
		return true
	}
}
return false

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, action func() error, options ...Option) error

Do retries action until action returns nil, context is done or max attempts limit is reached.

Returns nil if action eventually succeeded, otherwise returns last action error or ctx.Err() if action was never executed.

Types

type Option

type Option func(r *options)

Option configures retry strategy such as backoff duration or maximal number of attempts.

func WithInitialBackoff

func WithInitialBackoff(initialBackoff time.Duration) Option

WithInitialBackoff sets initial backoff.

If initial backoff option is not used, then default value of 50 milliseconds is used. If initial backoff is larger than max backoff and the max backoff is nonzero, the initial backoff will be used as the max.

func WithMaxAttempts

func WithMaxAttempts(maxAttempts int) Option

WithMaxAttempts sets upper limit on a number of attempts.

Max attempts of 0 indicates no limit.

If max attempts option is not used, then default value of 0 is used.

func WithMaxBackoff

func WithMaxBackoff(maxBackoff time.Duration) Option

WithMaxBackoff sets upper limit on backoff duration.

Max backoff of 0 indicates no limit.

If max backoff option is not used, then default value of 2 seconds is used.

func WithMultiplier

func WithMultiplier(multiplier float64) Option

WithMultiplier sets backoff multiplier controlling how fast backoff duration grows with each retry attempt.

If multiplier option is not used, then default value of 2 is used.

func WithRandomizationFactor

func WithRandomizationFactor(randomizationFactor float64) Option

WithRandomizationFactor sets randomization factor.

If randomization factor option is not used, then default value of 0.15 is used.

type Retrier

type Retrier interface {
	// Reset the retrier to its initial state, meaning that the next call to
	// Next will return immediately and subsequent calls will behave as if
	// they had followed the very first attempt.
	Reset()

	// Next returns whether the retry loop should continue, and blocks for the
	// appropriate length of time before yielding back to the caller.
	//
	// If a context is present, Next will eagerly return false if the context is done.
	Next() bool

	// CurrentAttempt returns current retry attempt.
	//
	// First attempt number is 0.
	//
	// Resetting retrier resets attempts counter.
	CurrentAttempt() int
}

Retrier allows controlling a retry loop.

Note that an explict loop using a Retrier can be often replaced with simpler and less error-prone Do() function.

func Start

func Start(ctx context.Context, opts ...Option) Retrier

Start returns a new initialized retrier.

If the provided context is canceled (see Context.Done), then Next() will eagerly return false and the retry loop will do no iterations.

Jump to

Keyboard shortcuts

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