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
// 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

View File

@ -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) {