retry

package module
v4.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2019 License: MIT Imports: 3 Imported by: 3

README

♻️ retry

Functional mechanism to perform actions repetitively until successful.

Awesome Patreon GoDoc Research License

Important news

The master is a feature frozen branch for versions 3.3.x and no longer maintained.

$ dep ensure -add github.com/kamilsk/retry@3.3.3

The v3 branch is a continuation of the master branch for versions v3.4.x to better integration with Go Modules.

$ go get -u github.com/kamilsk/retry/v3@v3.4.4

The v4 branch is an actual development branch.

$ go get -u github.com/kamilsk/retry/v4

$ dep ensure -add github.com/kamilsk/retry@v4.0.0-rc5

Version v4.x.y focused on integration with the 🚧 breaker and the 🧰 platform packages.

Usage

Quick start
retry.Retry
var response *http.Response

action := func(uint) error {
	var err error
	response, err = http.Get("https://github.com/kamilsk/retry")
	return err
}

if err := retry.Retry(breaker.BreakByTimeout(time.Minute), action, strategy.Limit(3)); err != nil {
	// handle error
}
// work with response
retry.Try
var response *http.Response

action := func(uint) error {
	var err error
	response, err = http.Get("https://github.com/kamilsk/retry")
	return err
}
interrupter := breaker.MultiplexTwo(
	breaker.BreakByTimeout(time.Minute),
	breaker.BreakBySignal(os.Interrupt),
)
defer interrupter.Close()

if err := retry.Try(interrupter, action, strategy.Limit(3)); err != nil {
	// handle error
}
// work with response

Or use Context

ctx, cancel := context.WithTimeout(request.Context(), time.Minute)
defer cancel()

if err := retry.Try(ctx, action, strategy.Limit(3)); err != nil {
	// handle error
}
// work with response
retry.TryContext
var response *http.Response

action := func(ctx context.Context, _ uint) error {
	req, err := http.NewRequest(http.MethodGet, "https://github.com/kamilsk/retry", nil)
	if err != nil {
		return err
	}
	req = req.WithContext(ctx)
	response, err = http.DefaultClient.Do(req)
	return err
}
ctx, cancel := context.WithTimeout(request.Context(), time.Minute)
br, ctx := breaker.WithContext(ctx)
defer func() {
	// they do the same thing
	br.Close()
	close()
}()

if err := retry.TryContext(ctx, action, strategy.Limit(3)); err != nil {
	// handle error
}
// work with response
Console tool for command execution with retries

This example shows how to repeat console command until successful.

$ retry --infinite -timeout 10m -backoff=lin:500ms -- /bin/sh -c 'echo "trying..."; exit $((1 + RANDOM % 10 > 5))'

asciicast

See more details here.

Installation

$ go get github.com/kamilsk/retry
$ # or use mirror
$ egg bitbucket.org/kamilsk/retry

egg1 is an extended go get.

Update

This library is using SemVer for versioning, and it is not BC-safe.

1 The project is still in prototyping.


Gitter

made with ❤️ by OctoLab

Documentation

Overview

Package retry provides functional mechanism to perform actions repetitively until successful.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsInterrupted

func IsInterrupted(err error) bool

IsInterrupted checks that the error is related to the Breaker interruption on Retry call.

func IsRecovered

func IsRecovered(err error) (interface{}, bool)

IsRecovered checks that the error is related to unhandled Action's panic and returns an original cause of panic.

func Retry

func Retry(
	breaker BreakCloser,
	action func(attempt uint) error,
	strategies ...func(attempt uint, err error) bool,
) error

Retry takes action and performs it, repetitively, until successful. When it is done it releases resources associated with the Breaker.

Optionally, strategies may be passed that assess whether or not an attempt should be made.

func Try

func Try(
	breaker Breaker,
	action func(attempt uint) error,
	strategies ...func(attempt uint, err error) bool,
) error

Try takes action and performs it, repetitively, until successful.

Optionally, strategies may be passed that assess whether or not an attempt should be made.

func TryContext

func TryContext(
	ctx context.Context,
	action func(ctx context.Context, attempt uint) error,
	strategies ...func(attempt uint, err error) bool,
) error

TryContext takes action and performs it, repetitively, until successful. It uses the Context as a Breaker to prevent unnecessary action execution.

Optionally, strategies may be passed that assess whether or not an attempt should be made.

Types

type BreakCloser

type BreakCloser interface {
	Breaker
	// Close closes the Done channel and releases resources associated with it.
	Close()
}

A BreakCloser carries a cancellation signal to break an action execution and can release resources associated with it.

It is a subset of github.com/kamilsk/breaker.Breaker.

type Breaker

type Breaker interface {
	// Done returns a channel that's closed when a cancellation signal occurred.
	Done() <-chan struct{}
}

A Breaker carries a cancellation signal to break an action execution.

It is a subset of context.Context and github.com/kamilsk/breaker.Breaker.

Directories

Path Synopsis
Package backoff provides stateless methods of calculating durations based on a number of attempts made.
Package backoff provides stateless methods of calculating durations based on a number of attempts made.
Package jitter provides methods of transforming durations.
Package jitter provides methods of transforming durations.
Package strategy provides a way to change the way that retry is performed.
Package strategy provides a way to change the way that retry is performed.

Jump to

Keyboard shortcuts

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