From dccb0d859c50f64688566ffff3fb67a2038e73b7 Mon Sep 17 00:00:00 2001 From: Evan Borgstrom Date: Thu, 26 Sep 2019 10:00:18 +0800 Subject: [PATCH] Undo breaking API change --- backoff.go | 10 +++++----- backoff_test.go | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/backoff.go b/backoff.go index e4fb068..c3cc5ee 100644 --- a/backoff.go +++ b/backoff.go @@ -27,7 +27,7 @@ type Backoff struct { // Duration returns the duration for the current attempt before incrementing // the attempt counter. See ForAttempt. func (b *Backoff) Duration() time.Duration { - d := b.ForAttempt(atomic.LoadUint64(&b.attempt)) + d := b.ForAttempt(float64(atomic.LoadUint64(&b.attempt))) atomic.AddUint64(&b.attempt, 1) return d } @@ -40,7 +40,7 @@ const maxInt64 = float64(math.MaxInt64 - 512) // attempt should be 0. // // ForAttempt is concurrent-safe. -func (b *Backoff) ForAttempt(attempt uint64) time.Duration { +func (b *Backoff) ForAttempt(attempt float64) time.Duration { // Zero-values are nonsensical, so we use // them to apply defaults min := b.Min @@ -61,7 +61,7 @@ func (b *Backoff) ForAttempt(attempt uint64) time.Duration { } //calculate this duration minf := float64(min) - durf := minf * math.Pow(factor, float64(attempt)) + durf := minf * math.Pow(factor, attempt) if b.Jitter { durf = rand.Float64()*(durf-minf) + minf } @@ -86,8 +86,8 @@ func (b *Backoff) Reset() { } // Attempt returns the current attempt counter value. -func (b *Backoff) Attempt() uint64 { - return atomic.LoadUint64(&b.attempt) +func (b *Backoff) Attempt() float64 { + return float64(atomic.LoadUint64(&b.attempt)) } // Copy returns a backoff with equals constraints as the original diff --git a/backoff_test.go b/backoff_test.go index 7b6a6a9..90b68c2 100644 --- a/backoff_test.go +++ b/backoff_test.go @@ -83,17 +83,17 @@ func TestGetAttempt(t *testing.T) { Max: 10 * time.Second, Factor: 2, } - equals(t, b.Attempt(), uint64(0)) + equals(t, b.Attempt(), float64(0)) equals(t, b.Duration(), 100*time.Millisecond) - equals(t, b.Attempt(), uint64(1)) + equals(t, b.Attempt(), float64(1)) equals(t, b.Duration(), 200*time.Millisecond) - equals(t, b.Attempt(), uint64(2)) + equals(t, b.Attempt(), float64(2)) equals(t, b.Duration(), 400*time.Millisecond) - equals(t, b.Attempt(), uint64(3)) + equals(t, b.Attempt(), float64(3)) b.Reset() - equals(t, b.Attempt(), uint64(0)) + equals(t, b.Attempt(), float64(0)) equals(t, b.Duration(), 100*time.Millisecond) - equals(t, b.Attempt(), uint64(1)) + equals(t, b.Attempt(), float64(1)) } func TestJitter(t *testing.T) {