Skip to content
Simple golang library for retry mechanism
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples add retryIf option Nov 24, 2017
.godocdown.tmpl makefile and readme template Oct 3, 2017
.travis.yml * travis build 1.6+ Nov 10, 2017
Gopkg.toml dep support Oct 3, 2017
LICENSE Initial commit Jul 24, 2017
options.go new line and comment fix Jan 18, 2019
retry.go lint Jan 19, 2019


Release Software License Travis AppVeyor Go Report Card GoDoc Sourcegraph

Simple library for retry mechanism

slightly inspired by Try::Tiny::Retry


http get with retry:

url := ""
var body []byte

err := retry.Do(
	func() error {
		resp, err := http.Get(url)
		if err != nil {
			return err
		defer resp.Body.Close()
		body, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			return err

		return nil


next examples


  • giantswarm/retry-go - slightly complicated interface.

  • sethgrid/pester - only http retry for http calls with retries and backoff

  • cenkalti/backoff - Go port of the exponential backoff algorithm from Google's HTTP Client Library for Java. Really complicated interface.

  • rafaeljesus/retry-go - looks good, slightly similar as this package, don't have 'simple' Retry method

  • matryer/try - very popular package, nonintuitive interface (for me)


1.0.2 -> 2.0.0

0.3.0 -> 1.0.0

  • retry.Retry function are changed to retry.Do function

  • retry.RetryCustom (OnRetry) and retry.RetryCustomWithOpts functions are now implement via functions produces Options (aka retry.OnRetry)


func BackOffDelay

func BackOffDelay(n uint, config *config) time.Duration

BackOffDelay is a DelayType which increases delay between consecutive retries

func Do

func Do(retryableFunc RetryableFunc, opts ...Option) error

func FixedDelay

func FixedDelay(_ uint, config *config) time.Duration

FixedDelay is a DelayType which keeps delay the same through all iterations

type DelayTypeFunc

type DelayTypeFunc func(n uint, config *config) time.Duration

type Error

type Error []error

Error type represents list of errors in retry

func (Error) Error

func (e Error) Error() string

Error method return string representation of Error It is an implementation of error interface

func (Error) WrappedErrors

func (e Error) WrappedErrors() []error

WrappedErrors returns the list of errors that this Error is wrapping. It is an implementation of the errwrap.Wrapper interface in package errwrap so that retry.Error can be used with that library.

type OnRetryFunc

type OnRetryFunc func(n uint, err error)

Function signature of OnRetry function n = count of attempts

type Option

type Option func(*config)

Option represents an option for retry.

func Attempts

func Attempts(attempts uint) Option

Attempts set count of retry default is 10

func Delay

func Delay(delay time.Duration) Option

Delay set delay between retry default is 100ms

func DelayType

func DelayType(delayType DelayTypeFunc) Option

DelayType set type of the delay between retries default is BackOff

func OnRetry

func OnRetry(onRetry OnRetryFunc) Option

OnRetry function callback are called each retry

log each retry example:

	func() error {
		return errors.New("some error")
	retry.OnRetry(func(n uint, err error) {
		log.Printf("#%d: %s\n", n, err)

func RetryIf

func RetryIf(retryIf RetryIfFunc) Option

RetryIf controls whether a retry should be attempted after an error (assuming there are any retry attempts remaining)

skip retry if special error example:

	func() error {
		return errors.New("special error")
	retry.RetryIf(func(err error) bool {
		if err.Error() == "special error" {
			return false
		return true

type RetryIfFunc

type RetryIfFunc func(error) bool

Function signature of retry if function

type RetryableFunc

type RetryableFunc func() error

Function signature of retryable function


Contributions are very much welcome.


Makefile provides several handy rules, like generator , setup for prepare build/dev environment, test, cover, etc...

Try make help for more information.

Before pull request

please try:

  • run tests (make test)
  • run linter (make lint)
  • if your IDE don't automaticaly do go fmt, run go fmt (make fmt)

README are generate from template .godocdown.tmpl and code documentation via godocdown.

Never edit direct, because your change will be lost.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.