retry

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

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

Go to latest
Published: Dec 3, 2015 License: Apache-2.0 Imports: 2 Imported by: 25

README

retry-go

Small helper library to retry operations automatically on certain errors.

Usage

The retry package provides a Do() function which can be used to execute a provided function until it succeds.

op := func() error {
	// Do something that can fail and should be retried here
	return httpClient.CreateUserOnRemoteServer()
}
retry.Do(op, 
         retry.RetryChecker(IsNetOpErr),
         retry.Timeout(15 * time.Second))

Besides the op itself, you can provide a few options:

  • RetryChecker(func(err error) bool) - If this func returns true for the returned error, the operation is tried again (default: nil - no retries)
  • MaxTries(int) - Maximum number of calls to op() before aborting with MaxRetriesReachedErr
  • Timeout(time.Duration) - Maximum number of time to try to perform this op before aborting with TimeoutReachedErr
  • Sleep(time.Duration) - time to sleep after every failed op()

Documentation

Index

Constants

View Source
const (
	DefaultMaxTries = 3
	DefaultTimeout  = time.Duration(15 * time.Second)
)

Variables

View Source
var (
	TimeoutError           = errgo.New("Operation aborted. Timeout occured")
	MaxRetriesReachedError = errgo.New("Operation aborted. Too many errors.")
)

Functions

func Do

func Do(op func() error, retryOptions ...RetryOption) error

Do performs the given operation. Based on the options, it can retry the operation, if it failed.

The following options are supported: * RetryChecker(func(err error) bool) - If this func returns true for the returned error, the operation is tried again * MaxTries(int) - Maximum number of calls to op() before aborting with MaxRetriesReached * Timeout(time.Duration) - Maximum number of time to try to perform this op before aborting with TimeoutReached * Sleep(time.Duration) - time to sleep after error failed op()

Defaults:

Timeout = 15 seconds
MaxRetries = 5
Retryer = errgo.Any
Sleep = No sleep

func IsMaxRetriesReached

func IsMaxRetriesReached(err error) bool

IsMaxRetriesReached returns true if the cause of the given error is a MaxRetriesReachedError.

func IsTimeout

func IsTimeout(err error) bool

IsTimeout returns true if the cause of the given error is a TimeoutError.

func Not

func Not(checker func(err error) bool) func(err error) bool

Not is a helper to invert another .

Types

type RetryOption

type RetryOption func(options *retryOptions)

func AfterRetry

func AfterRetry(afterRetry func(err error)) RetryOption

AfterRetry is called after a retry and can be used e.g. to emit events.

func AfterRetryLimit

func AfterRetryLimit(afterRetryLimit func(err error)) RetryOption

AfterRetryLimit is called after a retry limit is reached and can be used e.g. to emit events.

func MaxTries

func MaxTries(tries int) RetryOption

MaxTries specifies the maximum number of times op will be called by Do().

func RetryChecker

func RetryChecker(checker func(err error) bool) RetryOption

RetryChecker defines whether the given error is an error that can be retried.

func Sleep

func Sleep(d time.Duration) RetryOption

func Timeout

func Timeout(d time.Duration) RetryOption

Timeout specifies the maximum time that should be used before aborting the retry loop. Note that this does not abort the operation in progress.

Jump to

Keyboard shortcuts

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