retry

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: MIT Imports: 6 Imported by: 2

README

Package retry v1.2.2

This library implements a retryer: a generic way to execute some code at regular interval.

Usage

Creating a new retryer is done with:

retry.New(retry.WithWaitDuration(10*time.Second), retry.WithMaxAttempts(5))

Possible options for the constructor are:

  • WithWaitDuration: time interval between each execution of the code (default to 10 seconds).
  • WithMaxDuration: the retryer will stop executing after the specified amount of time (disabled by default).
  • WithMaxAttempts: the retryer will execute the at most N times. N being this max attempts parameter (default to 5).
  • WithoutMaxAttempts: disable the max attempts parameter.

Then execute the retryer with:

err = retryer.Do(ctx, func(ctx context.Context) error {
    // ...
	})

The function given as parameter only returns an error. If this error has the type RetryCancelError, it stops the execution of the retryer. You can create such error with NewRetryCancelError.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorCallback

type ErrorCallback func(ctx context.Context, err error, currentAttempt, maxAttempts int)

type Retry

type Retry interface {
	Do(ctx context.Context, method Retryable) error
}

type RetryCancelError

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

RetryCancelError is a error wrapping type that the user of a Retry should use to cancel retry operations before the end of maxAttempts/maxDuration conditions

func NewRetryCancelError

func NewRetryCancelError(err error) RetryCancelError

func (RetryCancelError) Error

func (err RetryCancelError) Error() string

type RetryError

type RetryError struct {
	Scope   RetryErrorScope
	Err     error
	LastErr error
}

func (RetryError) Error

func (err RetryError) Error() string

type RetryErrorScope

type RetryErrorScope string
const (
	MaxDurationScope RetryErrorScope = "max-duration"
	ContextScope     RetryErrorScope = "context"
)

type Retryable

type Retryable func(ctx context.Context) error

type Retryer

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

func New

func New(opts ...RetryerOptsFunc) Retryer

func (Retryer) Do

func (r Retryer) Do(ctx context.Context, method Retryable) error

Do execute method following rules of the Retry struct Two timeouts co-exist: * The one given as param of 'method': can be the scope of the current http.Request for instance * The one defined with the option WithMaxDuration, which would cancel the retry loop if it has expired.

type RetryerOptsFunc

type RetryerOptsFunc func(r *Retryer)

func WithErrorCallback

func WithErrorCallback(c ErrorCallback) RetryerOptsFunc

func WithLoggingOnAttemptError added in v1.2.2

func WithLoggingOnAttemptError(severity logrus.Level) RetryerOptsFunc

WithLoggingOnAttemptError allows emitting a log message on each attempt which failed. The capacity to specify the severity of the log message is useful to avoid flooding the logs with too many messages in case of a retry loop. Most of the time it will be Debug or Info according to the type of operation. Error should be chosen carefully if logger was configured to send Errors to a tool like Rollbar/Sentry/...

func WithMaxAttempts

func WithMaxAttempts(maxAttempts int) RetryerOptsFunc

func WithMaxDuration

func WithMaxDuration(duration time.Duration) RetryerOptsFunc

func WithWaitDuration

func WithWaitDuration(duration time.Duration) RetryerOptsFunc

func WithoutMaxAttempts

func WithoutMaxAttempts() RetryerOptsFunc

Jump to

Keyboard shortcuts

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