retry

package
v0.5.10 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 4 Imported by: 4

Documentation

Overview

retry is used to keep trying something until it works.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Operation

type Operation func() error

Operation is passed to Do() and is the code you would like to retry.

type Reason

type Reason string

Reason is the type of our Because* constants.

const (
	BecauseLimitReached  Reason = "limit reached"
	BecauseErrorNil      Reason = "there was no error"
	BecauseContextClosed Reason = "context closed"
)

Because* constants are returned by Until.ShouldStop().

type Status

type Status struct {
	// Retried is the number of retries done (which can be 0 if the Operation
	// only needed to be run once).
	Retried int

	// StoppedBecause is the Reason retries were stopped.
	StoppedBecause Reason

	// Err is the last return value of the Operation.
	Err error
}

Status is returned by Do() to explain what happened when retrying your Operation. It can be stringified or used as an error that wraps Err.

func Do

func Do(ctx context.Context, op Operation, until Until, bo *backoff.Backoff, activity string) *Status

Do will run op at least once, and then will keep retrying it unless the until returns a Reason to stop, or the context has been cancelled. The amount of time between retries is determined by bo.

The context is also used to end bo's sleep early, if cancelled during a sleep.

If any retries were required, the returned Status is logged using the global context logger at debug level. Any Backoff sleeps will have been logged sharing a unique retryset id, and a retrynum. All logs will include the given activity.

Note that bo is NOT Reset() during this function.

func (*Status) Error

func (s *Status) Error() string

Error implements the error interface, returning the same as String().

func (*Status) String

func (s *Status) String() string

String returns a string representation of the Status.

func (*Status) Unwrap

func (s *Status) Unwrap() error

Unwrap implements the error interface, returning our wrapped error.

type Until

type Until interface {
	// ShouldStop takes the number of retries so far along with the error from
	// the last attempt, and returns a non-blank Reason if no more attempts
	// should be made.
	ShouldStop(retries int, err error) Reason
}

Until is used by Retry to determine when to stop retrying.

type UntilLimit

type UntilLimit struct {
	Max int
}

UntilLimit implements Until, stopping retries after Max retries. A Max of 0 means "don't retry". A Max of 1 means up to 1 retry will be attempted, and so on.

func (*UntilLimit) ShouldStop

func (u *UntilLimit) ShouldStop(retries int, err error) Reason

ShouldStop returns BecauseLimitReached when retries is greater than or equal to Max. err is not considered.

type UntilNoError

type UntilNoError struct{}

UntilNoError implements Until, stopping retries when the error passed to ShouldStop is nil.

func (*UntilNoError) ShouldStop

func (u *UntilNoError) ShouldStop(retries int, err error) Reason

ShouldStop returns BecauseErrorNil when err is nil. retries is not considered.

type Untils

type Untils []Until

Untils is a slice of Until which itself implements Until, letting you combine multiple Untils.

func (Untils) ShouldStop

func (u Untils) ShouldStop(retries int, err error) Reason

ShouldStop returns a non-blank Reason when any of the elements of this slice return one.

Jump to

Keyboard shortcuts

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