From e89c06952ef4e9ac045e2e92b860786b7bd697c1 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 10 Jul 2015 23:28:53 +0300 Subject: [PATCH] renamed AccLock to Guardian --- engine/accountlock.go | 12 ++++++------ engine/accountlock_test.go | 6 +++--- engine/action_plan.go | 2 +- engine/calldesc.go | 6 +++--- engine/cdrs.go | 4 ++-- engine/responder.go | 10 +++++----- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/engine/accountlock.go b/engine/accountlock.go index 5184ce8f2..ac1bb2675 100644 --- a/engine/accountlock.go +++ b/engine/accountlock.go @@ -23,27 +23,27 @@ import ( ) // global package variable -var AccLock = &AccountLock{queue: make(map[string]chan bool)} +var Guardian = &GuardianLock{queue: make(map[string]chan bool)} -type AccountLock struct { +type GuardianLock struct { queue map[string]chan bool mu sync.Mutex } -func (cm *AccountLock) Guard(handler func() (interface{}, error), names ...string) (reply interface{}, err error) { +func (cm *GuardianLock) Guard(handler func() (interface{}, error), names ...string) (reply interface{}, err error) { cm.mu.Lock() for _, name := range names { - lock, exists := AccLock.queue[name] + lock, exists := Guardian.queue[name] if !exists { lock = make(chan bool, 1) - AccLock.queue[name] = lock + Guardian.queue[name] = lock } lock <- true } cm.mu.Unlock() reply, err = handler() for _, name := range names { - lock := AccLock.queue[name] + lock := Guardian.queue[name] <-lock } return diff --git a/engine/accountlock_test.go b/engine/accountlock_test.go index 3c1635d01..4a7266478 100644 --- a/engine/accountlock_test.go +++ b/engine/accountlock_test.go @@ -25,19 +25,19 @@ import ( ) func ATestAccountLock(t *testing.T) { - go AccLock.Guard(func() (interface{}, error) { + go Guardian.Guard(func() (interface{}, error) { log.Print("first 1") time.Sleep(1 * time.Second) log.Print("end first 1") return 0, nil }, "1") - go AccLock.Guard(func() (interface{}, error) { + go Guardian.Guard(func() (interface{}, error) { log.Print("first 2") time.Sleep(1 * time.Second) log.Print("end first 2") return 0, nil }, "2") - go AccLock.Guard(func() (interface{}, error) { + go Guardian.Guard(func() (interface{}, error) { log.Print("second 1") time.Sleep(1 * time.Second) log.Print("end second 1") diff --git a/engine/action_plan.go b/engine/action_plan.go index e0675712b..b1c694eab 100644 --- a/engine/action_plan.go +++ b/engine/action_plan.go @@ -256,7 +256,7 @@ func (at *ActionPlan) Execute() (err error) { return } for _, ubId := range at.AccountIds { - _, err := AccLock.Guard(func() (interface{}, error) { + _, err := Guardian.Guard(func() (interface{}, error) { ub, err := accountingStorage.GetAccount(ubId) if err != nil { Logger.Warning(fmt.Sprintf("Could not get user balances for this id: %s. Skipping!", ubId)) diff --git a/engine/calldesc.go b/engine/calldesc.go index 0a102533f..45d3c4289 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -563,7 +563,7 @@ func (cd *CallDescriptor) GetMaxSessionDuration() (duration time.Duration, err e return 0, err } else { if memberIds, err := account.GetUniqueSharedGroupMembers(cd); err == nil { - if _, err := AccLock.Guard(func() (interface{}, error) { + if _, err := Guardian.Guard(func() (interface{}, error) { duration, err = cd.getMaxSessionDuration(account) return 0, err }, memberIds...); err != nil { @@ -617,7 +617,7 @@ func (cd *CallDescriptor) Debit() (cc *CallCost, err error) { return nil, err } else { if memberIds, err := account.GetUniqueSharedGroupMembers(cd); err == nil { - AccLock.Guard(func() (interface{}, error) { + Guardian.Guard(func() (interface{}, error) { cc, err = cd.debit(account, false, true) return 0, err }, memberIds...) @@ -639,7 +639,7 @@ func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) { } else { //log.Printf("ACC: %+v", account) if memberIds, err := account.GetUniqueSharedGroupMembers(cd); err == nil { - AccLock.Guard(func() (interface{}, error) { + Guardian.Guard(func() (interface{}, error) { remainingDuration, err := cd.getMaxSessionDuration(account) //log.Print("AFTER MAX SESSION: ", cd) if err != nil || remainingDuration == 0 { diff --git a/engine/cdrs.go b/engine/cdrs.go index 7a1034460..dd7280be7 100644 --- a/engine/cdrs.go +++ b/engine/cdrs.go @@ -65,7 +65,7 @@ func fsCdrHandler(w http.ResponseWriter, r *http.Request) { } func NewCdrServer(cgrCfg *config.CGRConfig, cdrDb CdrStorage, rater Connector, stats StatsInterface) (*CdrServer, error) { - return &CdrServer{cgrCfg: cgrCfg, cdrDb: cdrDb, rater: rater, stats: stats, guard: &AccountLock{queue: make(map[string]chan bool)}}, nil + return &CdrServer{cgrCfg: cgrCfg, cdrDb: cdrDb, rater: rater, stats: stats, guard: &GuardianLock{queue: make(map[string]chan bool)}}, nil } type CdrServer struct { @@ -73,7 +73,7 @@ type CdrServer struct { cdrDb CdrStorage rater Connector stats StatsInterface - guard *AccountLock + guard *GuardianLock } func (self *CdrServer) RegisterHanlersToServer(server *Server) { diff --git a/engine/responder.go b/engine/responder.go index cbdcce645..6cee095d1 100644 --- a/engine/responder.go +++ b/engine/responder.go @@ -58,7 +58,7 @@ func (rs *Responder) GetCost(arg *CallDescriptor, reply *CallCost) (err error) { r, e := rs.getCallCost(arg, "Responder.GetCost") *reply, err = *r, e } else { - r, e := AccLock.Guard(func() (interface{}, error) { + r, e := Guardian.Guard(func() (interface{}, error) { return arg.GetCost() }, arg.GetAccountKey()) if e != nil { @@ -113,7 +113,7 @@ func (rs *Responder) RefundIncrements(arg *CallDescriptor, reply *float64) (err if rs.Bal != nil { *reply, err = rs.callMethod(arg, "Responder.RefundIncrements") } else { - r, e := AccLock.Guard(func() (interface{}, error) { + r, e := Guardian.Guard(func() (interface{}, error) { return arg.RefundIncrements() }, arg.GetAccountKey()) *reply, err = r.(float64), e @@ -291,7 +291,7 @@ func (rs *Responder) FlushCache(arg *CallDescriptor, reply *float64) (err error) if rs.Bal != nil { *reply, err = rs.callMethod(arg, "Responder.FlushCache") } else { - r, e := AccLock.Guard(func() (interface{}, error) { + r, e := Guardian.Guard(func() (interface{}, error) { return 0, arg.FlushCache() }, arg.GetAccountKey()) *reply, err = r.(float64), e @@ -336,7 +336,7 @@ func (rs *Responder) getCallCost(key *CallDescriptor, method string) (reply *Cal Logger.Info(" Waiting for raters to register...") time.Sleep(1 * time.Second) // wait one second and retry } else { - _, err = AccLock.Guard(func() (interface{}, error) { + _, err = Guardian.Guard(func() (interface{}, error) { err = client.Call(method, *key, reply) return reply, err }, key.GetAccountKey()) @@ -359,7 +359,7 @@ func (rs *Responder) callMethod(key *CallDescriptor, method string) (reply float Logger.Info("Waiting for raters to register...") time.Sleep(1 * time.Second) // wait one second and retry } else { - _, err = AccLock.Guard(func() (interface{}, error) { + _, err = Guardian.Guard(func() (interface{}, error) { err = client.Call(method, *key, &reply) return reply, err }, key.GetAccountKey())