retry-go: Index | Files

package retry

import ""

Package retry manages the execution of certain pieces of code that must run a specific number of times before being considered as failed.

The execution of a function will be considered failed only if all the tries returned an error, when it happens the 'With*' function will return all the errors in a ErrorGroup.

If the TryFunc return a nil error at any moment it will be considered a successful execution and nil will be returned by the 'With*' function.

To know more about the ErrorGroup go to

How many times TryFunc will execute?

The general rule is 'len(BackoffArray) + 1'

This happens because the BackoffArray is an array of intervals and makes no sense to have an interval after the last execution. Because of this every time the last interval is extracted from the BackoffArray the TryFunc is executed one more time. If you want to run the function only one time you should give a BackoffArray with zero items.


Package Files

doc.go retry.go


var (
    // ErrTryFuncNil is returned when the TryFunc is nil
    ErrTryFuncNil = errors.New("TryFunc can not be nil")

func WithBackoffArray Uses

func WithBackoffArray(backoff BackoffArray, fn TryFunc) error

WithBackoffArray runs the TryFunc with intervals from the given BackoffArray

It is important to notice that the TryFunc will run 'len(BackoffArray) + 1' times

func WithFixedInterval Uses

func WithFixedInterval(interval time.Duration, repeat int, fn TryFunc) error

WithFixedInterval runs the TryFunc with a BackoffArray created with the given 'interval' repeated 'repeat' times

It is important to notice that the TryFunc will run 'repeat + 1' times

type BackoffArray Uses

type BackoffArray []time.Duration

BackoffArray an vector of interval to wait between each retry

type TryFunc Uses

type TryFunc func(attempt, limit int) error

TryFunc is the function to try to execute.

It receives as arguments: - the number of this attempt '[0..len(BackoffArray]' - the limit of executions 'len(BackoffArray) + 1'

Package retry imports 3 packages (graph). Updated 2017-06-06. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).