Skip to content

Commit

Permalink
GetBreaker need double-check (zeromicro#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenbaise9527 authored Sep 30, 2020
1 parent 0ce6549 commit 9e9ce3b
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions core/breaker/breakers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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))
}

0 comments on commit 9e9ce3b

Please sign in to comment.