diff --git a/.gitignore b/.gitignore index daf913b..9f9e0f8 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ _testmain.go *.exe *.test *.prof + +# IDEA/WebStorm +/.idea/ diff --git a/README.md b/README.md index 4bcb029..85bdabe 100644 --- a/README.md +++ b/README.md @@ -77,13 +77,3 @@ if err != nil { log.Fatalln("error:", err) } ``` - -#### Maximum retry limit - -To avoid infinite loops, Try will ensure it only makes `try.MaxRetries` attempts. By default, this value is `10`, but you can change it: - -``` -try.MaxRetries = 20 -``` - -To see if a `Do` operation failed due to reaching the limit, you can check the `error` with `try.IsMaxRetries(err)`. diff --git a/try.go b/try.go index 69c791d..a950347 100644 --- a/try.go +++ b/try.go @@ -1,11 +1,9 @@ package try -import "errors" - -// MaxRetries is the maximum number of retries before bailing. -var MaxRetries = 10 - -var errMaxRetriesReached = errors.New("exceeded retry limit") +import ( + "errors" + "fmt" +) // Func represents functions that can be retried. type Func func(attempt int) (retry bool, err error) @@ -14,23 +12,17 @@ type Func func(attempt int) (retry bool, err error) // returns false, or no error is returned. func Do(fn Func) error { var err error - var cont bool + var retry bool attempt := 1 for { - cont, err = fn(attempt) - if !cont || err == nil { + retry, err = fn(attempt) + if err == nil { break } attempt++ - if attempt > MaxRetries { - return errMaxRetriesReached + if !retry { + return errors.New(fmt.Sprintf("exceeded retry limit - %v", err)) } } return err } - -// IsMaxRetries checks whether the error is due to hitting the -// maximum number of retries or not. -func IsMaxRetries(err error) bool { - return err == errMaxRetriesReached -} diff --git a/try_test.go b/try_test.go index 4b00ac1..7922599 100644 --- a/try_test.go +++ b/try_test.go @@ -11,7 +11,6 @@ import ( ) func TestTryExample(t *testing.T) { - try.MaxRetries = 20 SomeFunction := func() (string, error) { return "", nil } @@ -65,7 +64,7 @@ func TestTryDoFailed(t *testing.T) { callCount++ return attempt < 5, theErr }) - is.Equal(err, theErr) + is.Equal(err.Error(), "exceeded retry limit - something went wrong") is.Equal(callCount, 5) } @@ -87,15 +86,23 @@ func TestTryPanics(t *testing.T) { err = theErr return }) - is.Equal(err.Error(), "panic: I don't like three") + is.Equal(err.Error(), "exceeded retry limit - panic: I don't like three") is.Equal(callCount, 5) } func TestRetryLimit(t *testing.T) { is := is.New(t) - err := try.Do(func(attempt int) (bool, error) { - return true, errors.New("nope") + SomeFunction := func() (string, error) { + return "", errors.New("something went wrong") + } + var lastAttempt int + err := try.Do(func(attempt int) (retry bool, err error) { + _, err = SomeFunction() + retry = attempt < 15 // try 15 times + lastAttempt = attempt + return retry, err }) - is.OK(err) - is.Equal(try.IsMaxRetries(err), true) + + is.Equal(err.Error(), "exceeded retry limit - something went wrong") + is.Equal(lastAttempt, 15) }