From 321db0eecf59e98cd28ed81fcdfe4ffab1476993 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 22 Sep 2021 15:35:08 +0200 Subject: [PATCH] Introduce Settings#OnSuccess refs #351 --- pkg/retry/retry.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go index c3e0c70b..b8c1c92a 100644 --- a/pkg/retry/retry.go +++ b/pkg/retry/retry.go @@ -19,6 +19,8 @@ type Settings struct { Timeout time.Duration // OnError is called if an error occurs. OnError func(elapsed time.Duration, attempt uint64, err, lastErr error) + // OnSuccess is called once the operation succeeds. + OnSuccess func(elapsed time.Duration, attempt uint64, lastErr error) } // WithBackoff retries the passed function if it fails and the error allows it to retry. @@ -37,6 +39,10 @@ func WithBackoff( prevErr := err if err = retryableFunc(ctx); err == nil { + if settings.OnSuccess != nil { + settings.OnSuccess(time.Since(start), attempt, prevErr) + } + return }