retry

package
v0.2.15 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, fn func(ctx context.Context) error, options ...Option) error

Do retries the given function until it succeeds or the retry limit is reached. It takes a context and a function as parameters and optionally accepts additional options. The function is retried based on the specified options, which include the maximum number of retries and the backoff strategy. If the context is canceled, Do returns the context error. If the function returns an error that is not of type RetryableError, Do returns the error. If the retry limit is reached, Do returns ErrRetryLimitReached.

func MarkRetryable

func MarkRetryable(err error) error

MarkRetryable marks error as retryable by wrapping it in RetryableError.

Types

type Backoff

type Backoff interface {
	// Next returns the timeout before next retry.
	Next(retry uint) time.Duration
}

Backoff represents backoff logic.

type ConstantBackoff

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

ConstantBackoff implements Backoff interface where the backoff is constant.

func NewConstantBackoff

func NewConstantBackoff(minv, maxv, jitter time.Duration) *ConstantBackoff

NewConstantBackoff returns a pointer to a new instance of ConstantBackoff struct, which implements Backoff interface.

func (*ConstantBackoff) Next

func (b *ConstantBackoff) Next(retry uint) time.Duration

type Error

type Error string

Error represents package level errors.

const (
	// ErrRetryLimitReached is an error indicating that the retry limit has been reached.
	ErrRetryLimitReached Error = "retry limit reached"
)

func (Error) Error

func (e Error) Error() string

type ExponentialBackoff

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

ExponentialBackoff implements Backoff interface where the backoff grows exponentially based on retry count.

func NewExponentialBackoff

func NewExponentialBackoff(f uint, minv, maxv, jitter time.Duration) *ExponentialBackoff

NewExponentialBackoff returns a pointer to a new instance of ExponentialBackoff struct, which implements Backoff interface.

func (*ExponentialBackoff) Next

func (b *ExponentialBackoff) Next(retry uint) time.Duration

type LinearBackoff

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

LinearBackoff implements Backoff where the backoff grows linearly based on retry count.

func NewLinearBackoff

func NewLinearBackoff(minv, maxv, jitter time.Duration) *LinearBackoff

NewLinearBackoff returns a pointer to a new instance of ConstantBackoff struct, which implements Backoff interface.

func (*LinearBackoff) Next

func (b *LinearBackoff) Next(retry uint) time.Duration

type Option

type Option func(*Options)

Option is a function type that modifies the Options struct.

func WithBackoff

func WithBackoff(backoff Backoff) Option

WithBackoff returns an Option function that sets the backoff strategy for retry logic. The Backoff implementation determines the timeout before the next retry based on the number of retries.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger specifies the logger to be used for logging within the retry logic.

func WithMaxAttempts

func WithMaxAttempts(maxAttempts uint) Option

WithMaxAttempts is an Option function that sets the maximum number of attempts for a given operation. It takes a `maxAttempts` parameter of type `uint64` and updates the `maxRetries` field of the Options struct.

type Options

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

Options represents the configuration options for retry logic.

func (*Options) Backoff added in v0.2.8

func (o *Options) Backoff() Backoff

Backoff returns the current implementation of Backoff interface.

func (*Options) MaxRetries added in v0.2.8

func (o *Options) MaxRetries() uint

MaxRetries returns number a max retry attempts.

type RetryableError

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

RetryableError represents an error that can be retried. It encapsulates another error type and can be used to distinguish between errors that can be retried from those that cannot.

func (RetryableError) Error

func (e RetryableError) Error() string

func (RetryableError) Unwrap

func (e RetryableError) Unwrap() error

type StaticBackoff

type StaticBackoff time.Duration

StaticBackoff represents a fixed duration as a backoff strategy.

func (StaticBackoff) Next

func (b StaticBackoff) Next(uint) time.Duration

Jump to

Keyboard shortcuts

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