Fix concurrent access data race

This commit is contained in:
Evan Borgstrom 2019-09-25 09:59:28 +08:00
parent f452bc980a
commit 3e39e52ddf
2 changed files with 21 additions and 17 deletions

View File

@ -4,6 +4,7 @@ package backoff
import ( import (
"math" "math"
"math/rand" "math/rand"
"sync/atomic"
"time" "time"
) )
@ -14,8 +15,9 @@ import (
// Backoff is not generally concurrent-safe, but the ForAttempt method can // Backoff is not generally concurrent-safe, but the ForAttempt method can
// be used concurrently. // be used concurrently.
type Backoff struct { type Backoff struct {
attempt uint64
// Factor is the multiplying factor for each increment step // Factor is the multiplying factor for each increment step
attempt, Factor float64 Factor float64
// Jitter eases contention by randomizing backoff steps // Jitter eases contention by randomizing backoff steps
Jitter bool Jitter bool
// Min and Max are the minimum and maximum values of the counter // Min and Max are the minimum and maximum values of the counter
@ -25,8 +27,8 @@ 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(b.attempt) d := b.ForAttempt(atomic.LoadUint64(&b.attempt))
b.attempt++ atomic.AddUint64(&b.attempt, 1)
return d return d
} }
@ -38,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 float64) time.Duration { func (b *Backoff) ForAttempt(attempt uint64) 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
@ -59,7 +61,7 @@ func (b *Backoff) ForAttempt(attempt float64) time.Duration {
} }
//calculate this duration //calculate this duration
minf := float64(min) minf := float64(min)
durf := minf * math.Pow(factor, attempt) durf := minf * math.Pow(factor, float64(attempt))
if b.Jitter { if b.Jitter {
durf = rand.Float64()*(durf-minf) + minf durf = rand.Float64()*(durf-minf) + minf
} }
@ -80,12 +82,12 @@ func (b *Backoff) ForAttempt(attempt float64) time.Duration {
// Reset restarts the current attempt counter at zero. // Reset restarts the current attempt counter at zero.
func (b *Backoff) Reset() { func (b *Backoff) Reset() {
b.attempt = 0 atomic.StoreUint64(&b.attempt, 0)
} }
// Attempt returns the current attempt counter value. // Attempt returns the current attempt counter value.
func (b *Backoff) Attempt() float64 { func (b *Backoff) Attempt() uint64 {
return b.attempt return 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(), float64(0)) equals(t, b.Attempt(), uint64(0))
equals(t, b.Duration(), 100*time.Millisecond) equals(t, b.Duration(), 100*time.Millisecond)
equals(t, b.Attempt(), float64(1)) equals(t, b.Attempt(), uint64(1))
equals(t, b.Duration(), 200*time.Millisecond) equals(t, b.Duration(), 200*time.Millisecond)
equals(t, b.Attempt(), float64(2)) equals(t, b.Attempt(), uint64(2))
equals(t, b.Duration(), 400*time.Millisecond) equals(t, b.Duration(), 400*time.Millisecond)
equals(t, b.Attempt(), float64(3)) equals(t, b.Attempt(), uint64(3))
b.Reset() b.Reset()
equals(t, b.Attempt(), float64(0)) equals(t, b.Attempt(), uint64(0))
equals(t, b.Duration(), 100*time.Millisecond) equals(t, b.Duration(), 100*time.Millisecond)
equals(t, b.Attempt(), float64(1)) equals(t, b.Attempt(), uint64(1))
} }
func TestJitter(t *testing.T) { func TestJitter(t *testing.T) {
@ -142,6 +142,7 @@ func TestConcurrent(t *testing.T) {
} }
func between(t *testing.T, actual, low, high time.Duration) { func between(t *testing.T, actual, low, high time.Duration) {
t.Helper()
if actual < low { if actual < low {
t.Fatalf("Got %s, Expecting >= %s", actual, low) t.Fatalf("Got %s, Expecting >= %s", actual, low)
} }
@ -151,6 +152,7 @@ func between(t *testing.T, actual, low, high time.Duration) {
} }
func equals(t *testing.T, v1, v2 interface{}) { func equals(t *testing.T, v1, v2 interface{}) {
t.Helper()
if !reflect.DeepEqual(v1, v2) { if !reflect.DeepEqual(v1, v2) {
t.Fatalf("Got %v, Expecting %v", v1, v2) t.Fatalf("Got %v, Expecting %v", v1, v2)
} }