From 48ddd773eba4a0cfdfec0081c53249dd5f3868ee Mon Sep 17 00:00:00 2001 From: fengxsong Date: Tue, 13 Jun 2023 09:29:22 +0800 Subject: [PATCH] fix: add a local variable 'isRetryable' to avoid modifying the caller's Options' Signed-off-by: fengxsong --- pkg/retry/retry.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go index d517c8f40..5cf311b43 100644 --- a/pkg/retry/retry.go +++ b/pkg/retry/retry.go @@ -32,11 +32,14 @@ func RetryIfNecessary(ctx context.Context, operation func() error, options *Opti // IfNecessary retries the operation in exponential backoff with the retry Options. func IfNecessary(ctx context.Context, operation func() error, options *Options) error { - if options.IsErrorRetryable == nil { - options.IsErrorRetryable = IsErrorRetryable + var isRetryable func(error) bool + if options.IsErrorRetryable != nil { + isRetryable = options.IsErrorRetryable + } else { + isRetryable = IsErrorRetryable } err := operation() - for attempt := 0; err != nil && options.IsErrorRetryable(err) && attempt < options.MaxRetry; attempt++ { + for attempt := 0; err != nil && isRetryable(err) && attempt < options.MaxRetry; attempt++ { delay := time.Duration(int(math.Pow(2, float64(attempt)))) * time.Second if options.Delay != 0 { delay = options.Delay