From 5ae325ca3d6f9793ded26f9903d0f9f2b157cc04 Mon Sep 17 00:00:00 2001 From: Trong Huu Nguyen Date: Fri, 20 Sep 2024 11:32:16 +0200 Subject: [PATCH] fix(retry): correct usage of MaxDuration, remove unused code --- pkg/retry/retry.go | 48 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go index 879729b..c4bb6a5 100644 --- a/pkg/retry/retry.go +++ b/pkg/retry/retry.go @@ -7,52 +7,26 @@ import ( "github.com/sethvargo/go-retry" ) -var ( - DefaultBackoff = Fibonacci() - RetryableError = retry.RetryableError +var RetryableError = retry.RetryableError + +const ( + baseDuration = 50 * time.Millisecond + maxDuration = 1 * time.Second ) -type FibonacciBackoff struct { - Base time.Duration - Max time.Duration -} - -func WithBaseDuration(base time.Duration) func(*FibonacciBackoff) { - return func(f *FibonacciBackoff) { - f.Base = base - } -} - -func WithMaxDuration(max time.Duration) func(*FibonacciBackoff) { - return func(f *FibonacciBackoff) { - f.Max = max - } -} - -func Fibonacci(opts ...func(f *FibonacciBackoff)) retry.Backoff { - const DefaultBaseDuration = 50 * time.Millisecond - const DefaultMaxDuration = 1 * time.Second - - fb := &FibonacciBackoff{ - Base: DefaultBaseDuration, - Max: DefaultMaxDuration, - } - - for _, opt := range opts { - opt(fb) - } - - b := retry.NewFibonacci(fb.Base) - b = retry.WithMaxDuration(fb.Max, b) +func fibonacci() retry.Backoff { + b := retry.NewFibonacci(baseDuration) + // beware: this starts a timer when invoked, on which the max duration is evaluated against + b = retry.WithMaxDuration(maxDuration, b) return b } // Do retries the given function using the DefaultBackoff strategy. func Do(ctx context.Context, f retry.RetryFunc) error { - return retry.Do(ctx, DefaultBackoff, f) + return retry.Do(ctx, fibonacci(), f) } // DoValue is like Do, but returns a value of type T. func DoValue[T any](ctx context.Context, f retry.RetryFuncValue[T]) (T, error) { - return retry.DoValue(ctx, DefaultBackoff, f) + return retry.DoValue(ctx, fibonacci(), f) }