Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ _testmain.go
*.exe
*.test
*.prof

# IDEA/WebStorm
/.idea/
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.
26 changes: 9 additions & 17 deletions try.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
}
21 changes: 14 additions & 7 deletions try_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

func TestTryExample(t *testing.T) {
try.MaxRetries = 20
SomeFunction := func() (string, error) {
return "", nil
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}