renamed AccLock to Guardian

This commit is contained in:
Radu Ioan Fericean
2015-07-10 23:28:53 +03:00
parent c359516996
commit e89c06952e
6 changed files with 20 additions and 20 deletions

View File

@@ -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

View File

@@ -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")

View File

@@ -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))

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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("<Balancer> 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())