retry

package
v0.0.0-...-ae8e89f Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxAttempts = 4
)

Variables

This section is empty.

Functions

func RateLimit

func RateLimit(err error) error

RateLimit returns an error that indicates that the operation should be retried after a delay.

func Retryable

func Retryable(err error) error

Retryable returns an error that indicates that the operation should be retried.

func Wrap

func Wrap(ctx context.Context, operation OperationFn, opts ...Option) error

Wrap wraps the given operation with retry functionalities.

func WrapWithResult

func WrapWithResult[T any](ctx context.Context, operation OperationWithResultFn[T], opts ...Option) (T, error)

WrapWithResult wraps the given operation with retry functionalities.

Types

type Backoff

type Backoff backoff.BackOff

type BackoffFactory

type BackoffFactory func() Backoff

BackoffFactory returns a new instance of backoff policy.

type OperationFn

type OperationFn func(ctx context.Context) error

type OperationWithResultFn

type OperationWithResultFn[T any] func(ctx context.Context) (T, error)

type Option

type Option func(r *retryOptions)

func WithBackoffFactory

func WithBackoffFactory(backoffFactory BackoffFactory) Option

WithBackoffFactory sets the backoff factory. The default backoff factory creates an exponential backoff policy.

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger sets the logger. The default logger is zap.NewNop().

func WithMaxAttempts

func WithMaxAttempts(maxAttempts int) Option

WithMaxAttempts sets the maximum number of attempts. The default value is 3. Note that when maxAttempts is 1, the operation is executed only once without any retry.

type RateLimitError

type RateLimitError struct {
	Err error
}

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

func (*RateLimitError) Unwrap

func (e *RateLimitError) Unwrap() error

type Retry

type Retry interface {
	Retry(ctx context.Context, operation OperationFn) error
}

Retry is a simple wrapper on top of "cenkalti/backoff" to provide retry functionalities. Main differences with "cenkalti/backoff": * By default, only RetryableError and RateLimitError are retried. In "cenkalti/backoff", all errors except for PermanentError are retried. * It is compatible with xerrors, i.e. you may wrap a RetryableError and the default Filter uses xerrors.As to determine if the error is a RetryableError. * Retry is aborted if either MaxElapsedTime or MaxAttempts is exceeded.

func New

func New(opts ...Option) Retry

New creates a new instance of Retry that works with `func(ctx context.Context) error`.

type RetryWithResult

type RetryWithResult[T any] interface {
	Retry(ctx context.Context, operation OperationWithResultFn[T]) (T, error)
}

RetryWithResult is similar to Retry except that the operation returns a generic result.

func NewWithResult

func NewWithResult[T any](opts ...Option) RetryWithResult[T]

NewWithResult creates a new instance of RetryWithResult that works with `func(ctx context.Context) (T, error)`.

func ToRetryWithResult

func ToRetryWithResult(r Retry) RetryWithResult[struct{}]

ToRetryWithResult converts Retry to RetryWithResult[struct{}].

type RetryableError

type RetryableError struct {
	Err error
}

func (*RetryableError) Error

func (e *RetryableError) Error() string

func (*RetryableError) Unwrap

func (e *RetryableError) Unwrap() error

Jump to

Keyboard shortcuts

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