forked from giantswarm/retry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial changes to make it usable a lib
- Loading branch information
Showing
3 changed files
with
77 additions
and
52 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,17 @@ | ||
retry-go | ||
======== | ||
|
||
Small helper library to retry operations automatically on certain errors. | ||
|
||
## Usage | ||
|
||
``` | ||
retry.Do(someOp, retry.Timeout(15 * time.Second)) | ||
``` | ||
|
||
// The following options are supported: | ||
// * RetryChecker(func(err error) bool) - If this func returns true for the returned error, the operation is tried again | ||
// * MaxTries(int) - Maximum number of calls to op() before aborting with MaxRetriesReached | ||
// * Timeout(time.Duration) - Maximum number of time to try to perform this op before aborting with TimeoutReached | ||
// * Sleep(time.Duration) - time to sleep after error failed op() | ||
// |
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,55 @@ | ||
package retry | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type RetryOption func(options *retryOptions) | ||
|
||
// Timeout specifies the maximum time that should be used before aborting the retry loop. | ||
// Note that this does not abort the operation in progress. | ||
func Timeout(d time.Duration) RetryOption { | ||
return func(options *retryOptions) { | ||
options.Timeout = d | ||
} | ||
} | ||
|
||
// MaxTries specifies the maximum number of times op will be called by Do(). | ||
func MaxTries(tries int) RetryOption { | ||
return func(options *retryOptions) { | ||
options.MaxTries = tries | ||
} | ||
} | ||
|
||
// RetryChecker defines whether the given error is an error that can be retried. | ||
func RetryChecker(checker func(err error) bool) RetryOption { | ||
return func(options *retryOptions) { | ||
options.Checker = checker | ||
} | ||
} | ||
|
||
func Sleep(d time.Duration) RetryOption { | ||
return func(options *retryOptions) { | ||
options.Sleep = d | ||
} | ||
} | ||
|
||
type retryOptions struct { | ||
Timeout time.Duration | ||
MaxTries int | ||
Checker func(err error) bool | ||
Sleep time.Duration | ||
} | ||
|
||
func newRetryOptions(options ...RetryOption) retryOptions { | ||
state := retryOptions{ | ||
Timeout: DefaultTimeout, | ||
MaxTries: DefaultMaxTries, | ||
Retryer: errgo.Any, | ||
} | ||
|
||
for _, option := range options { | ||
option(&state) | ||
} | ||
return state | ||
} |
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