From 2029e1799d1f71d5bc5c1bbe997f34391e1b8bd9 Mon Sep 17 00:00:00 2001 From: Thomas Stromberg Date: Mon, 28 Jul 2025 12:58:17 -0400 Subject: [PATCH] merge: BackOffDelay multiplies attempts from zero #130 --- options.go | 2 ++ retry_test.go | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/options.go b/options.go index 5577ee7..f79fa98 100644 --- a/options.go +++ b/options.go @@ -124,6 +124,8 @@ func BackOffDelay(n uint, _ error, config *Config) time.Duration { config.maxBackOffN = max - uint(math.Floor(math.Log2(float64(config.delay)))) } + n-- + if n > config.maxBackOffN { n = config.maxBackOffN } diff --git a/retry_test.go b/retry_test.go index 1ee3739..2a1ec11 100644 --- a/retry_test.go +++ b/retry_test.go @@ -294,7 +294,7 @@ func TestBackOffDelay(t *testing.T) { delay: -1, expectedMaxN: 62, n: 2, - expectedDelay: 4, + expectedDelay: 2, }, { label: "zero-delay", @@ -310,6 +310,13 @@ func TestBackOffDelay(t *testing.T) { n: 62, expectedDelay: time.Second << 33, }, + { + label: "one-second-n", + delay: time.Second, + expectedMaxN: 33, + n: 1, + expectedDelay: time.Second, + }, } { t.Run( c.label, @@ -489,7 +496,7 @@ func TestContext(t *testing.T) { }) t.Run("timed out on retry infinte attempts - wraps context error with last retried function error", func(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500) + ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200) defer cancel() retrySum := 0