From 9e9ce3bf4860d8605a5203aaca8e9812e7081095 Mon Sep 17 00:00:00 2001 From: shenbaise9527 Date: Wed, 30 Sep 2020 16:50:02 +0800 Subject: [PATCH] GetBreaker need double-check (#112) --- core/breaker/breakers.go | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/core/breaker/breakers.go b/core/breaker/breakers.go index 6e320521..e8f2479e 100644 --- a/core/breaker/breakers.go +++ b/core/breaker/breakers.go @@ -41,10 +41,12 @@ func GetBreaker(name string) Breaker { } lock.Lock() - defer lock.Unlock() - - b = NewBreaker() - breakers[name] = b + b, ok = breakers[name] + if !ok { + b = NewBreaker(WithName(name)) + breakers[name] = b + } + lock.Unlock() return b } @@ -55,20 +57,5 @@ func NoBreakFor(name string) { } func do(name string, execute func(b Breaker) error) error { - lock.RLock() - b, ok := breakers[name] - lock.RUnlock() - if ok { - return execute(b) - } - - lock.Lock() - b, ok = breakers[name] - if !ok { - b = NewBreaker(WithName(name)) - breakers[name] = b - } - lock.Unlock() - - return execute(b) + return execute(GetBreaker(name)) }