Functional mechanism based on channels to perform actions repetitively until successful.
The master is a feature frozen branch for versions 3.3.x and no longer maintained.
$ dep ensure -add github.com/kamilsk/[email protected]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/[email protected]The v4 branch is an actual development branch.
$ go get -u github.com/kamilsk/retry/v4Version v4.x.y focused on integration with the 🚧 breaker package.
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 responsevar 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 responseOr 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 responsevar 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, _ := context.WithTimeout(request.Context(), time.Minute)
br, ctx := breaker.WithContext(ctx)
defer br.Close()
if err := retry.TryContext(ctx, action, strategy.Limit(3)); err != nil {
// handle error
}
// work with responseThis 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))'See more details here.
$ go get github.com/kamilsk/retry
$ # or use mirror
$ egg bitbucket.org/kamilsk/retryThis library is using SemVer for versioning, and it is not BC-safe.
1 The project is still in prototyping. ↩
made with ❤️ by OctoLab