2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 12:55:18 +00:00

Add function to notify of success after retrying

This commit is contained in:
Fred 2020-03-21 19:05:26 +00:00 committed by Michael Eischer
parent c169e37139
commit d629333efe

View File

@ -32,6 +32,25 @@ func NewRetryBackend(be restic.Backend, maxTries int, report func(string, error,
}
}
// retryNotifyErrorWithSuccess is an extension of backoff.RetryNotify with notification of success after an error
// success is NOT notified on the first run of operation (only after an error)
func retryNotifyErrorWithSuccess(operation backoff.Operation, b backoff.BackOff, notify backoff.Notify, success func()) error {
if success == nil {
return backoff.RetryNotify(operation, b, notify)
}
errorDetected := false
operationWrapper := func() error {
err := operation()
if err != nil {
errorDetected = true
} else if errorDetected {
success()
}
return err
}
return backoff.RetryNotify(operationWrapper, b, notify)
}
func (be *RetryBackend) retry(ctx context.Context, msg string, f func() error) error {
// Don't do anything when called with an already cancelled context. There would be
// no retries in that case either, so be consistent and abort always.