retry

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Overview

Package retry contains code to perform retries with exponential backoff.

Example: loop until doSomething() returns true or context hits deadline or is canceled.

for r := retry.Begin(); r.Continue(ctx); {
  if doSomething() {
    break
  }
}

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	BackoffMultiplier:  1.3,
	BackoffMinDuration: 10 * time.Millisecond,
}

DefaultOptions is the default set of Options.

Functions

This section is empty.

Types

type Options

type Options struct {
	BackoffMultiplier  float64 // If specified, must be at least 1.
	BackoffMinDuration time.Duration
}

Options are the options that configure a retry loop. Before the ith iteration of a retry loop, retry.Continue() sleeps for a duration of BackoffMinDuration * BackoffMultiplier^i, with added jitter.

type Retry

type Retry struct {
	// contains filtered or unexported fields
}

Retry holds state for managing retry loops with exponential backoff and jitter.

func Begin

func Begin() *Retry

Begin initiates a new retry loop.

func BeginWithOptions

func BeginWithOptions(options Options) *Retry

BeginWithOptions returns a new retry loop configured with the provided options.

Example: Sleep 1 second, then 2 seconds, then 4 seconds, and so on.

opts := retry.Options{
  BackoffMultiplier: 2.0,
  BackoffMinDuration: time.Second,
}
for r := retry.Begin(); r.Continue(ctx); {
  // Do nothing.
}

func (*Retry) Continue

func (r *Retry) Continue(ctx context.Context) bool

Continue sleeps for an exponentially increasing interval (with jitter). It stops its sleep early and returns false if context becomes done. If the return value is false, ctx.Err() is guaranteed to be non-nil. The first call does not sleep.

func (*Retry) Reset

func (r *Retry) Reset()

Reset resets a Retry to its initial state. Reset is useful if you want to retry an operation with exponential backoff, but only if it is failing. For example:

for r := retry.Begin(); r.Continue(ctx); {
    if err := doSomething(); err != nil {
        // Retry with backoff if we fail.
        continue
    }
    // Retry immediately if we succeed.
    r.Reset()
}

Jump to

Keyboard shortcuts

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