package errorsext

import "github.com/go-playground/pkg/v5/errors"

Index

Variables

var (
	// ErrMaxAttemptsReached is a placeholder error to use when some retryable even has reached its maximum number of
	// attempts.
	ErrMaxAttemptsReached = errors.New("max attempts reached")
)

Functions

func DoRetryable

func DoRetryable[T, E any](ctx context.Context, isRetryFn IsRetryableFn[E], onRetryFn OnRetryFn[E], fn RetryableFn[T, E]) resultext.Result[T, E]

DoRetryable will execute the provided functions code and automatically retry using the provided retry function.

Deprecated: use `errorsext.Retrier` instead which corrects design issues with the current implementation.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable returns true if the provided error is considered retryable by testing if it complies with an interface implementing `Retryable() bool` or `IsRetryable bool` and calling the function.

func IsRetryableHTTP

func IsRetryableHTTP(err error) (retryType string, isRetryable bool)

IsRetryableHTTP returns if the provided error is considered retryable HTTP error. It also returns the type, in string form, for optional logging and metrics use.

func IsRetryableNetwork

func IsRetryableNetwork(err error) (retryType string, isRetryable bool)

IsRetryableNetwork returns if the provided error is a retryable network related error. It also returns the type, in string form, for optional logging and metrics use.

func IsTemporary

func IsTemporary(err error) bool

IsTemporary returns true if the provided error is considered retryable temporary error by testing if it complies with an interface implementing `Temporary() bool` and calling the function.

func IsTemporaryConnection

func IsTemporaryConnection(err error) (retryType string, isRetryable bool)

IsTemporaryConnection returns if the provided error was a low level retryable connection error. It also returns the type, in string form, for optional logging and metrics use.

func IsTimeout

func IsTimeout(err error) bool

IsTimeout returns true if the provided error is considered a retryable timeout error by testing if it complies with an interface implementing `Timeout() bool` and calling the function.

Types

type BackoffFn

type BackoffFn[E any] func(ctx context.Context, attempt int, e E)

BackoffFn is a function used to apply a backoff strategy to the retryable function.

It accepts `E` in cases where the amount of time to backoff is dynamic, for example when and http request fails with a 429 status code, the `Retry-After` header can be used to determine how long to backoff. It is not required to use or handle `E` and can be ignored if desired.

type EarlyReturnFn

type EarlyReturnFn[E any] func(ctx context.Context, e E) (earlyReturn bool)

EarlyReturnFn is the function that can be used to bypass all retry logic, no matter the MaxAttemptsMode, for when the type of `E` will never succeed and should not be retried.

eg. If retrying an HTTP request and getting 400 Bad Request, it's unlikely to ever succeed and should not be retried.

type IsRetryableFn

type IsRetryableFn[E any] func(err E) (reason string, isRetryable bool)

IsRetryableFn is called to determine if the error is retryable and optionally returns the reason for logging and metrics.

type IsRetryableFn2

type IsRetryableFn2[E any] func(ctx context.Context, e E) (isRetryable bool)

IsRetryableFn2 is called to determine if the type E is retryable.

type MaxAttemptsMode

type MaxAttemptsMode uint8

MaxAttemptsMode is used to set the mode for the maximum number of attempts.

eg. Should the max attempts apply to all errors, just ones not determined to be retryable, reset on retryable errors, etc.

const (
	// MaxAttemptsNonRetryableReset will apply the max attempts to all errors not determined to be retryable, but will
	// reset the attempts if a retryable error is encountered after a non-retryable error.
	MaxAttemptsNonRetryableReset MaxAttemptsMode = iota

	// MaxAttemptsNonRetryable will apply the max attempts to all errors not determined to be retryable.
	MaxAttemptsNonRetryable

	// MaxAttempts will apply the max attempts to all errors, even those determined to be retryable.
	MaxAttempts

	// MaxAttemptsUnlimited will not apply a maximum number of attempts.
	MaxAttemptsUnlimited
)

type OnRetryFn

type OnRetryFn[E any] func(ctx context.Context, originalErr E, reason string, attempt int) optionext.Option[E]

OnRetryFn is called after IsRetryableFn returns true and before the retry is attempted.

this allows for interception, short-circuiting and adding of backoff strategies.

type RetryableFn

type RetryableFn[T, E any] func(ctx context.Context) resultext.Result[T, E]

RetryableFn is a function that can be retried.

type Retryer

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

Retryer is used to retry any fallible operation.

func NewRetryer

func NewRetryer[T, E any]() Retryer[T, E]

NewRetryer returns a new `Retryer` with sane default values.

The default values are: - `MaxAttemptsMode` is `MaxAttemptsNonRetryableReset`. - `MaxAttempts` is 5. - `Timeout` is 0 no context timeout. - `IsRetryableFn` will always return false as `E` is unknown until defined. - `BackoffFn` will sleep for 200ms. It's recommended to use exponential backoff for production. - `EarlyReturnFn` will be None.

func (Retryer[T, E]) Backoff

func (r Retryer[T, E]) Backoff(fn BackoffFn[E]) Retryer[T, E]

Backoff sets the backoff function for the `Retryer`.

func (Retryer[T, E]) Do

func (r Retryer[T, E]) Do(ctx context.Context, fn RetryableFn[T, E]) Result[T, E]

Do will execute the provided functions code and automatically retry using the provided retry function.

func (Retryer[T, E]) IsEarlyReturnFn

func (r Retryer[T, E]) IsEarlyReturnFn(fn EarlyReturnFn[E]) Retryer[T, E]

IsEarlyReturnFn sets the `EarlyReturnFn` for the `Retryer`.

NOTE: If the `EarlyReturnFn` and `IsRetryableFn` are both set and a conflicting `IsRetryableFn` will take precedence.

func (Retryer[T, E]) IsRetryableFn

func (r Retryer[T, E]) IsRetryableFn(fn IsRetryableFn2[E]) Retryer[T, E]

IsRetryableFn sets the `IsRetryableFn` for the `Retryer`.

func (Retryer[T, E]) MaxAttempts

func (r Retryer[T, E]) MaxAttempts(mode MaxAttemptsMode, maxAttempts uint8) Retryer[T, E]

MaxAttempts sets the maximum number of attempts for the `Retryer`.

NOTE: Max attempts is optional and if not set will retry indefinitely on retryable errors.

func (Retryer[T, E]) Timeout

func (r Retryer[T, E]) Timeout(timeout time.Duration) Retryer[T, E]

Timeout sets the timeout for the `Retryer`. This is the timeout per `RetyableFn` attempt and not the entirety of the `Retryer` execution.

A timeout of 0 will disable the timeout and is the default.

Source Files

do.go retrier.go retryable.go

Version
v5.30.0 (latest)
Published
Jun 1, 2024
Platform
linux/amd64
Imports
7 packages
Last checked
11 hours ago

Tools for package owners.