♻️ Functional mechanism based on channels to perform actions repetitively until successful.
Branch: v4
Clone or download
Latest commit c80cde8 Feb 17, 2019

README.md

♻️ retry

Functional mechanism based on channels 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.2

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.3

The v4 branch is an actual development branch.

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

Version v4.x.y focus on integration with the 🚧 breaker package.

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

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 := breaker.WithContext(
	context.Background(),
	breaker.BreakBySignal(os.Interrupt),
)

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. Therefore, do not use go get -u to update it, use dep, glide or something similar for this purpose.

1 The project is still in prototyping.


Gitter @kamilsk @octolab

made with ❤️ by OctoLab