retry

package
v3.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Package retry exposes a 'Retryer' allowing conditionally retrying (with back-off) of functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRetriesAborted

func IsRetriesAborted(err error) bool

IsRetriesAborted returns a boolean indicating whether the given error is a 'RetriesAbortedError'.

func IsRetriesExhausted

func IsRetriesExhausted(err error) bool

IsRetriesExhausted returns a boolean indicating whether the given error is a 'RetriesExhaustedError'.

func NewAbortRetriesError

func NewAbortRetriesError(err error) error

NewAbortRetriesError returns a wrapped error type, which allows aborting retries.

Types

type AbortRetriesError

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

AbortRetriesError allows aborting retries mid-way in the event of a fatal error.

NOTE: This error is not returned by the APIs, it's only used as a sentinel error to abort retries; expect a 'RetriesAbortedError' error.

func (*AbortRetriesError) Error

func (a *AbortRetriesError) Error() string

func (*AbortRetriesError) Unwrap

func (a *AbortRetriesError) Unwrap() error

type Algorithm

type Algorithm int

Algorithm represents a retry algorithm used to determine backoff before retrying function execution.

const (
	// AlgorithmFibonacci backs off using the fibonacci sequence e.g. 50ms, 50ms, 100ms ... 128h9m33s
	AlgorithmFibonacci Algorithm = iota

	// AlgorithmExponential backs off exponentially e.g. 100ms, 200ms, 400ms ... 477218h35m18s
	AlgorithmExponential

	// AlgorithmLinear backs off linearly e.g. 50ms, 100ms, 150ms ... 1.75s
	AlgorithmLinear
)

type CleanupFunc

type CleanupFunc[T any] func(payload T)

CleanupFunc is a function which is run with the payload for all, but the last retry attempt.

NOTE: The final attempt is not cleaned up because the payload may want to be used/read to enhance returned errors.

type Context

type Context struct {
	context.Context
	// contains filtered or unexported fields
}

Context wraps the 'context.Context' interface whilst allowing access to useful attributes such as the number of attempts made so far.

func NewContext

func NewContext(ctx context.Context) *Context

NewContext wraps the given context with a retry context.

func (*Context) Attempt

func (c *Context) Attempt() int

Attempt returns the current attempt number.

type LogFunc

type LogFunc[T any] func(ctx *Context, payload T, err error)

LogFunc is a function which is run before each retry attempt after failing to run the given 'RetryableFunc'.

type RetriesAbortedError

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

RetriesAbortedError is returned when retries have been aborted either due to context cancellation, or through use of the 'AbortRetriesError' sentinel error.

func (*RetriesAbortedError) Error

func (r *RetriesAbortedError) Error() string

func (*RetriesAbortedError) Unwrap

func (r *RetriesAbortedError) Unwrap() error

type RetriesExhaustedError

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

RetriesExhaustedError is returned after exhausting the max number of retries, unwrapping the error will return the error from the last failure.

func (*RetriesExhaustedError) Error

func (r *RetriesExhaustedError) Error() string

func (*RetriesExhaustedError) Unwrap

func (r *RetriesExhaustedError) Unwrap() error

type RetryableFunc

type RetryableFunc[T any] func(ctx *Context) (T, error)

RetryableFunc represents a function which is retryable.

type Retryer

type Retryer[T any] struct {
	// contains filtered or unexported fields
}

Retryer is a function retryer, which supports executing a given function a number of times until successful.

func NewRetryer

func NewRetryer[T any](options RetryerOptions[T]) Retryer[T]

NewRetryer returns a new retryer with the given options.

func (Retryer[T]) Do

func (r Retryer[T]) Do(fn RetryableFunc[T]) (T, error)

Do executes the given function until it's successful.

func (Retryer[T]) DoWithContext

func (r Retryer[T]) DoWithContext(ctx context.Context, fn RetryableFunc[T]) (T, error)

DoWithContext executes the given function until it's successful, the provided context may be used for cancellation.

func (Retryer[T]) Duration added in v3.0.1

func (r Retryer[T]) Duration(attempt int) time.Duration

Duration returns the duration to sleep for, this may be calculated using one of a number of different algorithms.

NOTE: After fifty attempts, a constant duration is returned (the max available, or the chosen max delay).

func (Retryer[T]) Jitter added in v3.1.0

func (r Retryer[T]) Jitter() (time.Duration, error)

Jitter returns the jitter, between the users provided min/max values.

type RetryerOptions

type RetryerOptions[T any] struct {
	// Algorithm is the algorithm to use when calculating backoff.
	Algorithm Algorithm

	// MaxRetries is the maximum number of times to retry the function.
	MaxRetries int

	// MinDelay is the minimum delay to use for backoff.
	MinDelay time.Duration

	// MaxDelay is the maximum delay to use for backoff.
	MaxDelay time.Duration

	// MinJitter is the minimum amount of jitter to apply before backing off.
	MinJitter time.Duration

	// MaxJitter is the maximum amount of jitter to apply before backing off.
	MaxJitter time.Duration

	// ShouldRetry is a custom retry function, when not supplied, this will be defaulted to 'err != nil'.
	ShouldRetry ShouldRetryFunc[T]

	// Log is a function which is run before each retry, when not supplied logging will be skipped.
	Log LogFunc[T]

	// Cleanup is a cleanup function run for all but the last payloads prior to performing a retry.
	Cleanup CleanupFunc[T]
}

RetryerOptions encapsulates the options available when creating a retryer.

type ShouldRetryFunc

type ShouldRetryFunc[T any] func(ctx *Context, payload T, err error) bool

ShouldRetryFunc is a function which may be supplied to the retry options which allows more control over which types of errors are retried.

NOTE: If not supplied, retries will take place if the given 'RetryableFunc' returns an error.

Jump to

Keyboard shortcuts

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