From 88a4c8d53a4b17713ff0c58cf3f6bb2b0d7ceeed Mon Sep 17 00:00:00 2001 From: Renato Lima Date: Sun, 14 Apr 2024 16:40:24 -0200 Subject: [PATCH] Introduce UntilSucceeded option to improve semantic --- README.md | 25 +++++++++++++++++-------- options.go | 10 ++++++++-- retry.go | 11 ++++++----- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c7736c0..d423e8d 100644 --- a/README.md +++ b/README.md @@ -10,12 +10,12 @@ Simple library for retry mechanism -slightly inspired by +Slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry) # SYNOPSIS -http get with retry: +HTTP GET with retry: url := "http://example.com" var body []byte @@ -31,17 +31,17 @@ http get with retry: if err != nil { return err } - return nil }, ) + if err != nil { // handle error } fmt.Println(string(body)) -http get with retry with data: +HTTP GET with retry with data: url := "http://example.com" @@ -60,13 +60,14 @@ http get with retry with data: return body, nil }, ) + if err != nil { // handle error } fmt.Println(string(body)) -[next examples](https://github.com/avast/retry-go/tree/master/examples) +[More examples](https://github.com/avast/retry-go/tree/master/examples) # SEE ALSO @@ -241,10 +242,10 @@ used with that library. #### type OnRetryFunc ```go -type OnRetryFunc func(n uint, err error) +type OnRetryFunc func(attempt uint, err error) ``` -Function signature of OnRetry function n = count of attempts +Function signature of OnRetry function #### type Option @@ -366,7 +367,7 @@ skip retry if special error example: return false } return true - }), + }) ) By default RetryIf stops execution if the error is wrapped using @@ -378,6 +379,14 @@ By default RetryIf stops execution if the error is wrapped using } ) +#### func UntilSucceeded + +```go +func UntilSucceeded() Option +``` +UntilSucceeded will retry until the retried function succeeds. Equivalent to +setting Attempts(0). + #### func WithTimer ```go diff --git a/options.go b/options.go index 9d98a1d..5577ee7 100644 --- a/options.go +++ b/options.go @@ -11,8 +11,7 @@ import ( type RetryIfFunc func(error) bool // Function signature of OnRetry function -// n = count of attempts -type OnRetryFunc func(n uint, err error) +type OnRetryFunc func(attempt uint, err error) // DelayTypeFunc is called to return the next delay to wait after the retriable function fails on `err` after `n` attempts. type DelayTypeFunc func(n uint, err error, config *Config) time.Duration @@ -60,6 +59,13 @@ func Attempts(attempts uint) Option { } } +// UntilSucceeded will retry until the retried function succeeds. Equivalent to setting Attempts(0). +func UntilSucceeded() Option { + return func(c *Config) { + c.attempts = 0 + } +} + // AttemptsForError sets count of retry in case execution results in given `err` // Retries for the given `err` are also counted against total retries. // The retry will stop if any of given retries is exhausted. diff --git a/retry.go b/retry.go index 56bc9dd..af40b10 100644 --- a/retry.go +++ b/retry.go @@ -1,11 +1,11 @@ /* Simple library for retry mechanism -slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry) +Slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry) # SYNOPSIS -http get with retry: +HTTP GET with retry: url := "http://example.com" var body []byte @@ -21,17 +21,17 @@ http get with retry: if err != nil { return err } - return nil }, ) + if err != nil { // handle error } fmt.Println(string(body)) -http get with retry with data: +HTTP GET with retry with data: url := "http://example.com" @@ -50,13 +50,14 @@ http get with retry with data: return body, nil }, ) + if err != nil { // handle error } fmt.Println(string(body)) -[next examples](https://github.com/avast/retry-go/tree/master/examples) +[More examples](https://github.com/avast/retry-go/tree/master/examples) # SEE ALSO