From acf2b44fe3aa7743e82133dbe297deafe642f1a5 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 3 Apr 2015 21:21:14 +0300 Subject: [PATCH] simler lock for account guard --- engine/accountlock.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/engine/accountlock.go b/engine/accountlock.go index 5309f75b8..6a2a20ee8 100644 --- a/engine/accountlock.go +++ b/engine/accountlock.go @@ -30,7 +30,7 @@ func init() { type AccountLock struct { queue map[string]chan bool - mu sync.RWMutex + mu sync.Mutex } func NewAccountLock() *AccountLock { @@ -38,34 +38,30 @@ func NewAccountLock() *AccountLock { } func (cm *AccountLock) GuardCallCost(handler func() (*CallCost, error), name string) (reply *CallCost, err error) { - cm.mu.RLock() + cm.mu.Lock() lock, exists := AccLock.queue[name] - cm.mu.RUnlock() if !exists { - cm.mu.Lock() lock = make(chan bool, 1) AccLock.queue[name] = lock - cm.mu.Unlock() } lock <- true + cm.mu.Unlock() reply, err = handler() <-lock return } func (cm *AccountLock) Guard(handler func() (float64, error), names ...string) (reply float64, err error) { + cm.mu.Lock() for _, name := range names { - cm.mu.RLock() lock, exists := AccLock.queue[name] - cm.mu.RUnlock() if !exists { - cm.mu.Lock() lock = make(chan bool, 1) AccLock.queue[name] = lock - cm.mu.Unlock() } lock <- true } + cm.mu.Unlock() reply, err = handler() for _, name := range names { lock := AccLock.queue[name]