package module
Version: v0.0.0-...-4f428e8 Latest Latest

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

Go to latest
Published: Jan 24, 2017 License: MIT Imports: 3 Imported by: 0


GoDoc Codeship Codecov Go Report Card


Retry a function execution with specific intervals with panic recovery

Make sure to read the docs to understand how this package works and what do expected from it.


go get -u


package main

import (


func main() {
  toTry := func(attempt, limit int) error {
    // Do you stuff and return an error if there is any
    return nil

  err := retry.WithFixedInterval(1 * time.Second, 5, toTry)
  if err != nil {
    log.Fatal(err) // It should log the errors if there were any


go get -u -t
cd $GOPATH/src/
go test ./...





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.



This section is empty.


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


func WithBackoffArray

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

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

type BackoffArray []time.Duration

BackoffArray an vector of interval to wait between each retry

type TryFunc

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'

Source Files

Jump to

Keyboard shortcuts

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