retry

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2023 License: CC0-1.0 Imports: 2 Imported by: 33

README

retry

An exponentially backing off retry package for Go.

GoDoc

go get github.com/coder/retry

Features

  • A for loop experience instead of closures
  • Only 3 exported methods
  • No external dependencies

Examples

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

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

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 {
	// contains filtered or unexported fields
}

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

Jump to

Keyboard shortcuts

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