From 7827bdbad4645cdef088f7ea18d90d05cc8e1587 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Mon, 29 Apr 2024 21:07:17 +0200 Subject: [PATCH] retry: explicitly log failed requests This simplifies finding the request in the log output that cause an operation to fail. --- cmd/restic/global.go | 6 ++++- internal/backend/retry/backend_retry.go | 30 ++++++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 6920caa8d..a06e6b4d3 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -416,7 +416,11 @@ func OpenRepository(ctx context.Context, opts GlobalOptions) (*repository.Reposi } report := func(msg string, err error, d time.Duration) { - Warnf("%v returned error, retrying after %v: %v\n", msg, d, err) + if d < 0 { + Warnf("%v returned error, retrying after %v: %v\n", msg, d, err) + } else { + Warnf("%v failed: %v\n", msg, err) + } } success := func(msg string, retries int) { Warnf("%v operation successful after %d retries\n", msg, retries) diff --git a/internal/backend/retry/backend_retry.go b/internal/backend/retry/backend_retry.go index c63338fb6..3c51a3497 100644 --- a/internal/backend/retry/backend_retry.go +++ b/internal/backend/retry/backend_retry.go @@ -39,20 +39,28 @@ func New(be backend.Backend, maxTries int, report func(string, error, time.Durat // 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(retries int)) error { + var operationWrapper backoff.Operation if success == nil { - return backoff.RetryNotify(operation, b, notify) - } - retries := 0 - operationWrapper := func() error { - err := operation() - if err != nil { - retries++ - } else if retries > 0 { - success(retries) + operationWrapper = operation + } else { + retries := 0 + operationWrapper = func() error { + err := operation() + if err != nil { + retries++ + } else if retries > 0 { + success(retries) + } + return err } - return err } - return backoff.RetryNotify(operationWrapper, b, notify) + err := backoff.RetryNotify(operationWrapper, b, notify) + + if err != nil && notify != nil { + // log final error + notify(err, -1) + } + return err } var fastRetries = false