mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
simler lock for account guard
This commit is contained in:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user