Undo breaking API change

This commit is contained in:
Evan Borgstrom 2019-09-26 10:00:18 +08:00
parent 3e39e52ddf
commit dccb0d859c
2 changed files with 11 additions and 11 deletions

View File

@ -27,7 +27,7 @@ type Backoff struct {
// Duration returns the duration for the current attempt before incrementing // Duration returns the duration for the current attempt before incrementing
// the attempt counter. See ForAttempt. // the attempt counter. See ForAttempt.
func (b *Backoff) Duration() time.Duration { 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) atomic.AddUint64(&b.attempt, 1)
return d return d
} }
@ -40,7 +40,7 @@ const maxInt64 = float64(math.MaxInt64 - 512)
// attempt should be 0. // attempt should be 0.
// //
// ForAttempt is concurrent-safe. // 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 // Zero-values are nonsensical, so we use
// them to apply defaults // them to apply defaults
min := b.Min min := b.Min
@ -61,7 +61,7 @@ func (b *Backoff) ForAttempt(attempt uint64) time.Duration {
} }
//calculate this duration //calculate this duration
minf := float64(min) minf := float64(min)
durf := minf * math.Pow(factor, float64(attempt)) durf := minf * math.Pow(factor, attempt)
if b.Jitter { if b.Jitter {
durf = rand.Float64()*(durf-minf) + minf durf = rand.Float64()*(durf-minf) + minf
} }
@ -86,8 +86,8 @@ func (b *Backoff) Reset() {
} }
// Attempt returns the current attempt counter value. // Attempt returns the current attempt counter value.
func (b *Backoff) Attempt() uint64 { func (b *Backoff) Attempt() float64 {
return atomic.LoadUint64(&b.attempt) return float64(atomic.LoadUint64(&b.attempt))
} }
// Copy returns a backoff with equals constraints as the original // Copy returns a backoff with equals constraints as the original

View File

@ -83,17 +83,17 @@ func TestGetAttempt(t *testing.T) {
Max: 10 * time.Second, Max: 10 * time.Second,
Factor: 2, Factor: 2,
} }
equals(t, b.Attempt(), uint64(0)) equals(t, b.Attempt(), float64(0))
equals(t, b.Duration(), 100*time.Millisecond) 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.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.Duration(), 400*time.Millisecond)
equals(t, b.Attempt(), uint64(3)) equals(t, b.Attempt(), float64(3))
b.Reset() b.Reset()
equals(t, b.Attempt(), uint64(0)) equals(t, b.Attempt(), float64(0))
equals(t, b.Duration(), 100*time.Millisecond) equals(t, b.Duration(), 100*time.Millisecond)
equals(t, b.Attempt(), uint64(1)) equals(t, b.Attempt(), float64(1))
} }
func TestJitter(t *testing.T) { func TestJitter(t *testing.T) {