retry

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2023 License: CC0-1.0 Imports: 4 Imported by: 23

README

retry

An exponentially backing off retry package for Go.

GoDoc

go get github.com/coder/retry@latest

retry promotes control flow using for/goto instead of callbacks.

Examples

Wait for connectivity to google.com, checking at most once every second:

func pingGoogle(ctx context.Context) error {
	var err error

	r := retry.New(time.Second, time.Second*10);

	// Jitter is useful when the majority of clients to a service use
	// the same backoff policy.
	//
	// It is provided as a standard deviation.
	r.Jitter = 0.1

  retry:
	_, err = http.Get("https://google.com")
	if err != nil {
		if r.Wait(ctx) {
			goto retry
		}
		return err
	}

	return nil
}

Wait for connectivity to google.com, checking at most 10 times:

func pingGoogle(ctx context.Context) error {
	var err error
	
	for r := retry.New(time.Second, time.Second*10); n := 0; r.Wait(ctx) && n < 10; n++ {
		_, err = http.Get("https://google.com")
		if err != nil {
			continue
		}
		break
	}
	return err
}

Documentation

Overview

Package retry runs a fallible block of code until it succeeds.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Retrier added in v1.3.0

type Retrier struct {
	// Delay is the current delay between attempts.
	Delay time.Duration

	// Floor and Ceil are the minimum and maximum delays.
	Floor, Ceil time.Duration

	// Rate is the rate at which the delay grows.
	// E.g. 2 means the delay doubles each time.
	Rate float64

	// Jitter determines the level of indeterminism in the delay.
	//
	// It is the standard deviation of the normal distribution of a random variable
	// multiplied by the delay. E.g. 0.1 means the delay is normally distributed
	// with a standard deviation of 10% of the delay. Floor and Ceil are still
	// respected, making outlandish values impossible.
	//
	// Jitter can help avoid thundering herds.
	Jitter float64
}

Retrier implements an exponentially backing off retry instance. Use New instead of creating this object directly.

func New

func New(floor, ceil time.Duration) *Retrier

New creates a retrier that exponentially backs off from floor to ceil pauses.

func (*Retrier) Reset added in v1.4.0

func (r *Retrier) Reset()

Reset resets the retrier to its initial state.

func (*Retrier) Wait added in v1.3.0

func (r *Retrier) Wait(ctx context.Context) bool

Wait returns after min(Delay*Growth, Ceil) or ctx is cancelled. The first call to Wait will return immediately.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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