-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88ab330
commit 0cc7026
Showing
2 changed files
with
26 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package outis | ||
|
||
// retry allowes a given method to be retried x amount of times. | ||
type retry struct{ amount, retries int8 } | ||
|
||
// Go returns the settings for using the Attempt method | ||
func (ctx *Context) Retry(retries int8) *retry { | ||
return &retry{retries: retries} | ||
} | ||
|
||
// The attempt attempts the given method for a given amount of retries | ||
// If the method still fails after the set limit is a error returned. | ||
func (r *retry) Attempt(method func() error) (err error) { | ||
if err = method(); err == nil { | ||
return nil | ||
} | ||
|
||
if r.retries >= r.amount { | ||
return err | ||
} | ||
|
||
r.retries++ | ||
return r.Attempt(method) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters