trytryagain

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: MIT Imports: 5 Imported by: 1

README

TryTryAgain GoDoc Build Status Coverage Status Go Report Card

This library provides a simple utility for performing an action with retries. TryTryAgain is thread-safe and takes care of handling backoff and expired contexts. The library provides configuration for the backoff strategy, retry counts, and a callback for errors.

How it works

A retrier is created with the specified options for maxAttempts, backoffFunc, and onErrorFunc. Then you simply call Do, providing a function which wraps the action to be performed. This function should return an error and a boolean to indicate whether the error is retriable or not.

Defaults:

  • The default max attempts is 3
  • The default backoff strategy is exponential
  • The default onError callback does nothing

Usage

Here is a trivial example:

r := NewRetrier(
    WithMaxAttempts(4),
    WithOnError(func(err error) { fmt.Println(err.Error()) }),
    WithBackoff(exponentialBackoff),
)

err := r.Do(ctx, func() (error, bool) {
    return fmt.Errorf("something went wrong"), true
})

More detailed examples can be found in examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrContextCanceled is returned if the context is cancelled or the deadline is reached
	ErrContextCanceled = fmt.Errorf("context cancelled")
	// ErrRequestNotRetriable is returned if the request fails and the error is not retriable (a 400, for example)
	ErrRequestNotRetriable = fmt.Errorf("request is not retriable")
	// ErrNotSuccessful is returned if the number of retries is exhausted without any successes
	ErrNotSuccessful = fmt.Errorf("request not successful")
)

Functions

func IsCanceledContextError added in v0.0.2

func IsCanceledContextError(err error) bool

IsCanceledContextError returns true if the error is some variant of "context canceled" or "context deadline exceeded"

func IsContextDone added in v0.0.2

func IsContextDone(ctx context.Context) bool

IsContextDone returns true if the context is done

Types

type ActionFunc

type ActionFunc func() (err error, retriable bool)

ActionFunc is the function which is called and retried in case of a failure

type BackoffFunc

type BackoffFunc func(ctx context.Context, attempts uint) time.Duration

BackoffFunc is the backoff strategy. Given the number of attempts already made, it returns the wait time

type ErrorHandler added in v1.0.1

type ErrorHandler func(retrierErr, actionErr error) error

ErrorHandler is a function which is called before returning an error

type OnErrorFunc

type OnErrorFunc func(err error)

OnErrorFunc is a callback function which is called every time the action returns an error

type Retrier

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

Retrier performs a specified action with retry logic based on its configuration

func NewRetrier

func NewRetrier(options ...RetrierOption) *Retrier

NewRetrier returns a new Retrier with the specified options

func (*Retrier) Do

func (t *Retrier) Do(ctx context.Context, action ActionFunc) error

Do performs the specified action and retries with backoff in case of failures until the request either succeeds or the maximum number of retries has been reached.

type RetrierOption

type RetrierOption func(t *Retrier)

RetrierOption is a callback for specifying configuration options for a Retrier

func WithBackoff

func WithBackoff(backoff BackoffFunc) RetrierOption

WithBackoff allows you to specify the backoff strategy, for example an exponential backoff

func WithIgnoreCtx added in v1.0.0

func WithIgnoreCtx(ignoreCtx bool) RetrierOption

WithIgnoreCtx is an option to specify whether context canncelation/deadline should be ignored (will retry anyway if true)

func WithMaxAttempts

func WithMaxAttempts(maxAttempts uint) RetrierOption

WithMaxAttempts is an option to specify the maximum number of retries which the Retrier should make

func WithOnError

func WithOnError(onError OnErrorFunc) RetrierOption

WithOnError is an option to specify the OnError callback which is called for each failed attempt

func WithPassThroughErrorHandler added in v1.0.1

func WithPassThroughErrorHandler() RetrierOption

WithPassThroughErrorHandler is an option to specify that the Retrier should return original error returned by the action

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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