2017-02-26 21:53:19 +00:00
|
|
|
package backoff
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
// An Operation is executing by Retry() or RetryNotify().
|
|
|
|
// The operation will be retried using a backoff policy if it returns an error.
|
|
|
|
type Operation func() error
|
|
|
|
|
|
|
|
// Notify is a notify-on-error function. It receives an operation error and
|
|
|
|
// backoff delay if the operation failed (with an error).
|
|
|
|
//
|
|
|
|
// NOTE that if the backoff policy stated to stop retrying,
|
|
|
|
// the notify function isn't called.
|
|
|
|
type Notify func(error, time.Duration)
|
|
|
|
|
|
|
|
// Retry the operation o until it does not return error or BackOff stops.
|
|
|
|
// o is guaranteed to be run at least once.
|
|
|
|
// It is the caller's responsibility to reset b after Retry returns.
|
|
|
|
//
|
2017-03-31 00:03:13 +00:00
|
|
|
// If o returns a *PermanentError, the operation is not retried, and the
|
|
|
|
// wrapped error is returned.
|
|
|
|
//
|
2017-02-26 21:53:19 +00:00
|
|
|
// Retry sleeps the goroutine for the duration returned by BackOff after a
|
|
|
|
// failed operation returns.
|
|
|
|
func Retry(o Operation, b BackOff) error { return RetryNotify(o, b, nil) }
|
|
|
|
|
|
|
|
// RetryNotify calls notify function with the error and wait duration
|
|
|
|
// for each failed attempt before sleep.
|
|
|
|
func RetryNotify(operation Operation, b BackOff, notify Notify) error {
|
|
|
|
var err error
|
|
|
|
var next time.Duration
|
|
|
|
|
2017-03-31 00:03:13 +00:00
|
|
|
cb := ensureContext(b)
|
|
|
|
|
2017-02-26 21:53:19 +00:00
|
|
|
b.Reset()
|
|
|
|
for {
|
|
|
|
if err = operation(); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:03:13 +00:00
|
|
|
if permanent, ok := err.(*PermanentError); ok {
|
|
|
|
return permanent.Err
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:53:19 +00:00
|
|
|
if next = b.NextBackOff(); next == Stop {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if notify != nil {
|
|
|
|
notify(err, next)
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:03:13 +00:00
|
|
|
t := time.NewTimer(next)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-cb.Context().Done():
|
|
|
|
t.Stop()
|
|
|
|
return err
|
|
|
|
case <-t.C:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PermanentError signals that the operation should not be retried.
|
|
|
|
type PermanentError struct {
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PermanentError) Error() string {
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permanent wraps the given err in a *PermanentError.
|
|
|
|
func Permanent(err error) *PermanentError {
|
|
|
|
return &PermanentError{
|
|
|
|
Err: err,
|
2017-02-26 21:53:19 +00:00
|
|
|
}
|
|
|
|
}
|