retro

package module
v0.0.0-...-7769938 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2019 License: MIT Imports: 3 Imported by: 13

README

Retro - Retryable errors in Golang

⚠ Note: This library is no longer maintained ⚠

Codeship Status for codeship/go-retro

Retry allows you to wrap failure prone sections of code in retry blocks, and conditionally retry different types of errors. To retry an error within a block, simply return a custom error type matching the provided interface. By default, wrapping code in a retro block will have no effect.

Usage

// Wrap your code block with retro
finalErr := retro.DoWithRetry(func() error{
    // Hit an external API
    return DoMyFunc(args)
})

Any errors matching the RetryableError interface bubbled up will automatically retry the code block.

For more detailed examples see the examples folder.

Independent sections of code within a chain can be wrapped conditionally in retro blocks as needed. You should always try and keep retry blocks as small as possible to reduce code and requests being re-run.

Retrying

You can use two types of provided error retry patterns, or create your own:

Static retryable errors

These errors retry X times every Y seconds. To create a static retryable error use retro.NewStaticRetryableError(err error, maxAttempts, waitSeconds int). For generic errors you can reference a shared variable, for dynamic errors you can create an error with the relevant base error message each time. Error retrying state is stored in the retro loop, not the RetryableError.

Backoff retryable errors

These errors use quadrilateral equation based on Sidekiq retries to increasingly space out subsequent attempts. To create a backoff retryable error use retro.NewBackoffRetryableError(err error, maxAttempts int). For generic errors you can reference a shared variable, for dynamic errors you can create an error with the relevant base error message each time. Error retrying state is stored in the retro loop, not the RetryableError.

Retry attempts

The retro retry loop will keep track of how many times it has looped. Any time it gets an error it compares the state against the allowed retry conditions for the latest error. This means that should the loop initially retry with an error allowing ten retries, if the second error indicates only two retries or fewer are allowed, then the loop will no longer retry since it has already retried twice.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoWithRetry

func DoWithRetry(f func() error) error

DoWithRetry will execute a function as many times as is dictated by any retryable errors propagated by the function

func WrapRetryableError

func WrapRetryableError(err error, errorList []*regexp.Regexp, errorCreator ErrorCreator) error

WrapRetryableError takes an error, and given a set of retryable error regexes, returns a suitable error type, either a standard error or a retryable error

Types

type ErrorCreator

type ErrorCreator func(error) RetryableError

ErrorCreator is a type of function that creates retryable errors from standard errors

type RetryableError

type RetryableError interface {
	Error() string
	MaxAttempts() int
	Wait(int)
}

RetryableError is an interface for any kind of error allowing retries

func NewBackoffRetryableError

func NewBackoffRetryableError(err error, maxAttempts int) RetryableError

NewBackoffRetryableError creates a RetryableError which will retry up to maxAttempts times using an exponential backoff

func NewLogarithmicRetryableError

func NewLogarithmicRetryableError(err error, maxAttempts int, baseOffset, scale, power float64) RetryableError

NewLogarithmicRetryableError creates a RetryableError which will retry up to maxAttempts times, with a sleep pattern defined by a logarithmic curve

func NewStaticRetryableError

func NewStaticRetryableError(err error, maxAttempts, waitSeconds int) RetryableError

NewStaticRetryableError creates a RetryableError which will retry up to maxAttempts times sleeping waitSeconds in between tries

Directories

Path Synopsis
examples
network command
simple command

Jump to

Keyboard shortcuts

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