go get -u github.com/sermojohn/go-recovererr
The package name was conceived by merging recover
and error
and can be pronounced as recoverer.
- Wrap recoverable errors with an error value implementing
Recover() bool
:
type customError struct {
recoverable bool
message string
}
func (ce *customError) Recover() bool {
return ce.recoverable
}
func (ce *customError) Error() string {
return fmt.Sprintf("recoverable:%t, message:%s", ce.recoverable, ce.message)
}
- Retry action of recoverable error per 1sec forever:
action := func() error {
return &customError{recoverable: true}
}
backoff := NewConstantBackoff(time.Second, 0)
_ = Retry(context.Background(), action, backoff, RetryRecoverablePolicy)
- Retry action of recoverable error using exponential backoff starting with 1sec until 5sec elapse:
action := func() error {
return &customError{recoverable: true}
}
backoff := NewExponentialBackoff(
WithInitialInterval(time.Second),
WithMaxElapsedTime(5*time.Second),
)
_ = Retry(context.Background(), action, backoff, RetryRecoverablePolicy)
- Retry action of non unrecoverable error:
action := func() error {
return errors.New("any error")
}
backoff := NewConstantBackoff(time.Second, 0)
_ = Retry(context.Background(), action, backoff, RetryNonUnrecoverablePolicy)
The package provides Recoverable
and Unrecoverable
public functions to wrap the given error with recovery context.
Also provides DoRecover
function to check the recovery context of any error.
The package provides function Retry
that receives a function that optionally returns an error.
The RetryPolicy
is provided to Retry
, to check the error recovery context on failure and define if the function should be retried.
The BackoffStrategy
is provided to defind the delay applied before each retry performing either constant
or exponential
backoff.
If context.Context gets cancelled no extra retry will be performed, but the original error will be wrapped to the timeout error.