diff --git a/pkg/net/netutil/breaker/sre_breaker.go b/pkg/net/netutil/breaker/sre_breaker.go index 4627369ff..44ccb4b71 100644 --- a/pkg/net/netutil/breaker/sre_breaker.go +++ b/pkg/net/netutil/breaker/sre_breaker.go @@ -15,15 +15,14 @@ import ( // sreBreaker is a sre CircuitBreaker pattern. type sreBreaker struct { stat metric.RollingCounter + r *rand.Rand + // rand.New(...) returns a non thread safe object + randLock sync.Mutex k float64 request int64 state int32 - - random *rand.Rand - // rand.New(...) returns a non thread safe object - randLock sync.Mutex } func newSRE(c *Config) Breaker { @@ -34,11 +33,11 @@ func newSRE(c *Config) Breaker { stat := metric.NewRollingCounter(counterOpts) return &sreBreaker{ stat: stat, + r: rand.New(rand.NewSource(time.Now().UnixNano())), request: c.Request, k: c.K, state: StateClosed, - random: rand.New(rand.NewSource(time.Now().UnixNano())), } } @@ -95,7 +94,7 @@ func (b *sreBreaker) MarkFailed() { func (b *sreBreaker) trueOnProba(proba float64) (truth bool) { b.randLock.Lock() - truth = b.random.Float64() < proba + truth = b.r.Float64() < proba b.randLock.Unlock() return } diff --git a/pkg/net/netutil/breaker/sre_breaker_test.go b/pkg/net/netutil/breaker/sre_breaker_test.go index bd1849d74..9df374736 100644 --- a/pkg/net/netutil/breaker/sre_breaker_test.go +++ b/pkg/net/netutil/breaker/sre_breaker_test.go @@ -29,6 +29,7 @@ func getSREBreaker() *sreBreaker { stat := metric.NewRollingCounter(counterOpts) return &sreBreaker{ stat: stat, + r: rand.New(rand.NewSource(time.Now().UnixNano())), request: 100, k: 2, @@ -151,8 +152,9 @@ func TestTrueOnProba(t *testing.T) { const total = 100000 const epsilon = 0.05 var count int + b := getSREBreaker() for i := 0; i < total; i++ { - if trueOnProba(proba) { + if b.trueOnProba(proba) { count++ } }