From 2c076c1abb2a379ebd45c844f6ee564e3235e449 Mon Sep 17 00:00:00 2001 From: DanB Date: Mon, 9 Jan 2017 20:36:23 +0100 Subject: [PATCH] Moving Guardian into it's own package so we can use it from other independent packages --- apier/v1/accounts.go | 11 ++++++----- apier/v1/apier.go | 9 +++++---- apier/v1/triggers.go | 11 ++++++----- apier/v2/accounts.go | 5 +++-- apier/v2/apier.go | 3 ++- apier/v2/triggers.go | 3 ++- cdrc/partial_cdr.go | 5 +++-- cdrc/unpairedrecords.go | 6 +++--- engine/account.go | 5 +++-- engine/action.go | 3 ++- engine/action_plan.go | 3 ++- engine/aliases.go | 2 +- engine/calldesc.go | 11 ++++++----- engine/cdrs.go | 5 +++-- engine/responder.go | 7 ++++--- engine/storage_mongo_datadb.go | 3 ++- engine/storage_redis.go | 3 ++- engine/tp_reader.go | 12 +++++++++--- {engine => guardian}/guardian.go | 2 +- {engine => guardian}/guardian_test.go | 2 +- sessionmanager/sessions.go | 7 ++++--- sessionmanager/smgeneric.go | 7 ++++--- 22 files changed, 74 insertions(+), 51 deletions(-) rename {engine => guardian}/guardian.go (99%) rename {engine => guardian}/guardian_test.go (99%) diff --git a/apier/v1/accounts.go b/apier/v1/accounts.go index bb7a08165..3532c9125 100644 --- a/apier/v1/accounts.go +++ b/apier/v1/accounts.go @@ -26,6 +26,7 @@ import ( "github.com/cgrates/cgrates/cache" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" ) @@ -89,7 +90,7 @@ func (self *ApierV1) RemActionTiming(attrs AttrRemActionTiming, reply *string) ( return utils.NewErrMandatoryIeMissing(missing...) } } - _, err = engine.Guardian.Guard(func() (interface{}, error) { + _, err = guardian.Guardian.Guard(func() (interface{}, error) { ap, err := self.RatingDb.GetActionPlan(attrs.ActionPlanId, false, utils.NonTransactional) if err != nil { return 0, err @@ -153,7 +154,7 @@ func (self *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) error var schedulerReloadNeeded = false accID := utils.AccountKey(attr.Tenant, attr.Account) var ub *engine.Account - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { if bal, _ := self.AccountDb.GetAccount(accID); bal != nil { ub = bal } else { // Not found in db, create it here @@ -163,7 +164,7 @@ func (self *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) error } if len(attr.ActionPlanId) != 0 { - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { var ap *engine.ActionPlan ap, err := self.RatingDb.GetActionPlan(attr.ActionPlanId, false, utils.NonTransactional) if err != nil { @@ -259,9 +260,9 @@ func (self *ApierV1) RemoveAccount(attr utils.AttrRemoveAccount, reply *string) } dirtyActionPlans := make(map[string]*engine.ActionPlan) accID := utils.AccountKey(attr.Tenant, attr.Account) - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { // remove it from all action plans - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { actionPlansMap, err := self.RatingDb.GetAllActionPlans() if err == utils.ErrNotFound { // no action plans diff --git a/apier/v1/apier.go b/apier/v1/apier.go index a4fd1d188..96f50acd1 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -29,6 +29,7 @@ import ( "github.com/cgrates/cgrates/cache" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/servmanager" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" @@ -696,7 +697,7 @@ func (self *ApierV1) LoadAccountActions(attrs utils.TPAccountActions, reply *str return utils.NewErrMandatoryIeMissing("TPid") } dbReader := engine.NewTpReader(self.RatingDb, self.AccountDb, self.StorDb, attrs.TPid, self.Config.DefaultTimezone) - if _, err := engine.Guardian.Guard(func() (interface{}, error) { + if _, err := guardian.Guardian.Guard(func() (interface{}, error) { aas := engine.APItoModelAccountAction(&attrs) if err := dbReader.LoadAccountActionsFiltered(aas); err != nil { return 0, err @@ -1516,7 +1517,7 @@ func (self *ApierV1) RemoveRatingProfile(attr AttrRemoveRatingProfile, reply *st attr.Tenant != "" && attr.Direction == "" { return utils.ErrMandatoryIeMissing } - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { err := self.RatingDb.RemoveRatingProfile(attr.GetId(), utils.NonTransactional) if err != nil { return 0, err @@ -1624,13 +1625,13 @@ type AttrRemoteLock struct { } func (self *ApierV1) RemoteLock(attr AttrRemoteLock, reply *string) error { - engine.Guardian.GuardIDs(attr.Timeout, attr.LockIDs...) + guardian.Guardian.GuardIDs(attr.Timeout, attr.LockIDs...) *reply = utils.OK return nil } func (self *ApierV1) RemoteUnlock(lockIDs []string, reply *string) error { - engine.Guardian.UnguardIDs(lockIDs...) + guardian.Guardian.UnguardIDs(lockIDs...) *reply = utils.OK return nil } diff --git a/apier/v1/triggers.go b/apier/v1/triggers.go index f50cd9b4e..7cc8de34c 100644 --- a/apier/v1/triggers.go +++ b/apier/v1/triggers.go @@ -21,6 +21,7 @@ import ( "strings" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" ) @@ -61,7 +62,7 @@ func (self *ApierV1) AddAccountActionTriggers(attr AttrAddAccountActionTriggers, } accID := utils.AccountKey(attr.Tenant, attr.Account) var account *engine.Account - _, err = engine.Guardian.Guard(func() (interface{}, error) { + _, err = guardian.Guardian.Guard(func() (interface{}, error) { if acc, err := self.AccountDb.GetAccount(accID); err == nil { account = acc } else { @@ -118,7 +119,7 @@ func (self *ApierV1) RemoveAccountActionTriggers(attr AttrRemoveAccountActionTri return utils.NewErrMandatoryIeMissing(missing...) } accID := utils.AccountKey(attr.Tenant, attr.Account) - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { var account *engine.Account if acc, err := self.AccountDb.GetAccount(accID); err == nil { account = acc @@ -164,7 +165,7 @@ func (self *ApierV1) ResetAccountActionTriggers(attr AttrResetAccountActionTrigg } accID := utils.AccountKey(attr.Tenant, attr.Account) var account *engine.Account - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { if acc, err := self.AccountDb.GetAccount(accID); err == nil { account = acc } else { @@ -229,7 +230,7 @@ func (self *ApierV1) SetAccountActionTriggers(attr AttrSetAccountActionTriggers, } accID := utils.AccountKey(attr.Tenant, attr.Account) var account *engine.Account - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { if acc, err := self.AccountDb.GetAccount(accID); err == nil { account = acc } else { @@ -614,7 +615,7 @@ func (self *ApierV1) AddTriggeredAction(attr AttrAddActionTrigger, reply *string at.Balance.SharedGroups = &utils.StringMap{attr.BalanceSharedGroup: true} } acntID := utils.AccountKey(attr.Tenant, attr.Account) - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { acnt, err := self.AccountDb.GetAccount(acntID) if err != nil { return 0, err diff --git a/apier/v2/accounts.go b/apier/v2/accounts.go index 069c34a1f..ed6ea522f 100644 --- a/apier/v2/accounts.go +++ b/apier/v2/accounts.go @@ -23,6 +23,7 @@ import ( "math" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" ) @@ -103,7 +104,7 @@ func (self *ApierV2) SetAccount(attr AttrSetAccount, reply *string) error { accID := utils.AccountKey(attr.Tenant, attr.Account) dirtyActionPlans := make(map[string]*engine.ActionPlan) var ub *engine.Account - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { if bal, _ := self.AccountDb.GetAccount(accID); bal != nil { ub = bal } else { // Not found in db, create it here @@ -112,7 +113,7 @@ func (self *ApierV2) SetAccount(attr AttrSetAccount, reply *string) error { } } if attr.ActionPlanIDs != nil { - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { actionPlansMap, err := self.RatingDb.GetAllActionPlans() if err != nil { if err == utils.ErrNotFound { // if no action plans just continue diff --git a/apier/v2/apier.go b/apier/v2/apier.go index 1f0b37c35..95666617e 100644 --- a/apier/v2/apier.go +++ b/apier/v2/apier.go @@ -28,6 +28,7 @@ import ( "github.com/cgrates/cgrates/apier/v1" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" ) @@ -73,7 +74,7 @@ func (self *ApierV2) LoadAccountActions(attrs AttrLoadAccountActions, reply *str tpAa := &utils.TPAccountActions{TPid: attrs.TPid} tpAa.SetAccountActionsId(attrs.AccountActionsId) aa := engine.APItoModelAccountAction(tpAa) - if _, err := engine.Guardian.Guard(func() (interface{}, error) { + if _, err := guardian.Guardian.Guard(func() (interface{}, error) { if err := dbReader.LoadAccountActionsFiltered(aa); err != nil { return 0, err } diff --git a/apier/v2/triggers.go b/apier/v2/triggers.go index b4b9fdb94..2c0b37b0a 100644 --- a/apier/v2/triggers.go +++ b/apier/v2/triggers.go @@ -21,6 +21,7 @@ import ( "errors" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" ) @@ -159,7 +160,7 @@ func (self *ApierV2) SetAccountActionTriggers(attr AttrSetAccountActionTriggers, } accID := utils.AccountKey(attr.Tenant, attr.Account) var account *engine.Account - _, err := engine.Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { if acc, err := self.AccountDb.GetAccount(accID); err == nil { account = acc } else { diff --git a/cdrc/partial_cdr.go b/cdrc/partial_cdr.go index b330cd237..363cbc3a5 100644 --- a/cdrc/partial_cdr.go +++ b/cdrc/partial_cdr.go @@ -28,6 +28,7 @@ import ( "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" ) @@ -38,7 +39,7 @@ const ( func NewPartialRecordsCache(ttl time.Duration, expiryAction string, cdrOutDir string, csvSep rune, roundDecimals int, timezone string, httpSkipTlsCheck bool, cdrs rpcclient.RpcClientConnection) (*PartialRecordsCache, error) { return &PartialRecordsCache{ttl: ttl, expiryAction: expiryAction, cdrOutDir: cdrOutDir, csvSep: csvSep, roundDecimals: roundDecimals, timezone: timezone, httpSkipTlsCheck: httpSkipTlsCheck, cdrs: cdrs, - partialRecords: make(map[string]*PartialCDRRecord), dumpTimers: make(map[string]*time.Timer), guard: engine.Guardian}, nil + partialRecords: make(map[string]*PartialCDRRecord), dumpTimers: make(map[string]*time.Timer), guard: guardian.Guardian}, nil } type PartialRecordsCache struct { @@ -52,7 +53,7 @@ type PartialRecordsCache struct { cdrs rpcclient.RpcClientConnection partialRecords map[string]*PartialCDRRecord // [OriginID]*PartialRecord dumpTimers map[string]*time.Timer // [OriginID]*time.Timer which can be canceled or reset - guard *engine.GuardianLock + guard *guardian.GuardianLock } // Dumps the cache into a .unpaired file in the outdir and cleans cache after diff --git a/cdrc/unpairedrecords.go b/cdrc/unpairedrecords.go index cf8a77c79..c2ce6fd1e 100644 --- a/cdrc/unpairedrecords.go +++ b/cdrc/unpairedrecords.go @@ -26,13 +26,13 @@ import ( "strconv" "time" - "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" ) func NewUnpairedRecordsCache(ttl time.Duration, cdrOutDir string, csvSep rune) (*UnpairedRecordsCache, error) { return &UnpairedRecordsCache{ttl: ttl, cdrOutDir: cdrOutDir, csvSep: csvSep, - partialRecords: make(map[string]map[string]*UnpairedRecord), guard: engine.Guardian}, nil + partialRecords: make(map[string]map[string]*UnpairedRecord), guard: guardian.Guardian}, nil } type UnpairedRecordsCache struct { @@ -40,7 +40,7 @@ type UnpairedRecordsCache struct { cdrOutDir string csvSep rune partialRecords map[string]map[string]*UnpairedRecord // [FileName"][OriginID]*PartialRecord - guard *engine.GuardianLock + guard *guardian.GuardianLock } // Dumps the cache into a .unpaired file in the outdir and cleans cache after diff --git a/engine/account.go b/engine/account.go index 776933300..b8cca9198 100644 --- a/engine/account.go +++ b/engine/account.go @@ -23,6 +23,7 @@ import ( "fmt" "time" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/structmatcher" "github.com/cgrates/cgrates/utils" @@ -151,7 +152,7 @@ func (acc *Account) setBalanceAction(a *Action) error { } // modify if necessary the shared groups here if !found || !previousSharedGroups.Equal(balance.SharedGroups) { - _, err := Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { sgs := make([]string, len(balance.SharedGroups)) i := 0 for sgID := range balance.SharedGroups { @@ -243,7 +244,7 @@ func (ub *Account) debitBalanceAction(a *Action, reset bool) error { } } ub.BalanceMap[balanceType] = append(ub.BalanceMap[balanceType], bClone) - _, err := Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { sgs := make([]string, len(bClone.SharedGroups)) i := 0 for sgId := range bClone.SharedGroups { diff --git a/engine/action.go b/engine/action.go index 8067aa141..fcabe7f4b 100644 --- a/engine/action.go +++ b/engine/action.go @@ -32,6 +32,7 @@ import ( "time" "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" "github.com/mitchellh/mapstructure" @@ -559,7 +560,7 @@ func removeAccountAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Ac return err } - _, err := Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { // clean the account id from all action plans allAPs, err := ratingStorage.GetAllActionPlans() if err != nil && err != utils.ErrNotFound { diff --git a/engine/action_plan.go b/engine/action_plan.go index 3696cf69f..36b215952 100644 --- a/engine/action_plan.go +++ b/engine/action_plan.go @@ -22,6 +22,7 @@ import ( "sort" "time" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "github.com/gorhill/cronexpr" ) @@ -289,7 +290,7 @@ func (at *ActionTiming) Execute() (err error) { return } for accID, _ := range at.accountIDs { - _, err = Guardian.Guard(func() (interface{}, error) { + _, err = guardian.Guardian.Guard(func() (interface{}, error) { acc, err := accountingStorage.GetAccount(accID) if err != nil { utils.Logger.Warning(fmt.Sprintf("Could not get account id: %s. Skipping!", accID)) diff --git a/engine/aliases.go b/engine/aliases.go index 11df84507..96529aec9 100644 --- a/engine/aliases.go +++ b/engine/aliases.go @@ -386,7 +386,7 @@ func (am *AliasHandler) Call(serviceMethod string, args interface{}, reply inter } func LoadAlias(attr *AttrMatchingAlias, in interface{}, extraFields string) error { - if aliasService == nil { // no alias service => no fun + if aliasService == nil { return nil } response := Alias{} diff --git a/engine/calldesc.go b/engine/calldesc.go index 3b0d1492f..a5a403b97 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -28,6 +28,7 @@ import ( "github.com/cgrates/cgrates/cache" "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" ) @@ -636,7 +637,7 @@ func (cd *CallDescriptor) GetMaxSessionDuration() (duration time.Duration, err e return 0, err } else { if memberIds, err := account.GetUniqueSharedGroupMembers(cd); err == nil { - if _, err := Guardian.Guard(func() (interface{}, error) { + if _, err := guardian.Guardian.Guard(func() (interface{}, error) { duration, err = cd.getMaxSessionDuration(account) return 0, err }, 0, memberIds.Slice()...); err != nil { @@ -702,7 +703,7 @@ func (cd *CallDescriptor) Debit() (cc *CallCost, err error) { return nil, err } else { if memberIds, sgerr := account.GetUniqueSharedGroupMembers(cd); sgerr == nil { - _, err = Guardian.Guard(func() (interface{}, error) { + _, err = guardian.Guardian.Guard(func() (interface{}, error) { cc, err = cd.debit(account, cd.DryRun, !cd.DenyNegativeAccount) return 0, err }, 0, memberIds.Slice()...) @@ -724,7 +725,7 @@ func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) { } else { //log.Printf("ACC: %+v", account) if memberIDs, err := account.GetUniqueSharedGroupMembers(cd); err == nil { - _, err = Guardian.Guard(func() (interface{}, error) { + _, err = guardian.Guardian.Guard(func() (interface{}, error) { remainingDuration, err := cd.getMaxSessionDuration(account) if err != nil && cd.GetDuration() > 0 { return 0, err @@ -783,7 +784,7 @@ func (cd *CallDescriptor) RefundIncrements() error { } } // start increment refunding loop - _, err := Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { accountsCache := make(map[string]*Account) for _, increment := range cd.Increments { account, found := accountsCache[increment.BalanceInfo.AccountID] @@ -832,7 +833,7 @@ func (cd *CallDescriptor) RefundRounding() error { accMap[inc.BalanceInfo.AccountID] = true } // start increment refunding loop - _, err := Guardian.Guard(func() (interface{}, error) { + _, err := guardian.Guardian.Guard(func() (interface{}, error) { accountsCache := make(map[string]*Account) for _, increment := range cd.Increments { account, found := accountsCache[increment.BalanceInfo.AccountID] diff --git a/engine/cdrs.go b/engine/cdrs.go index 8b845cff7..f2bdaaff1 100644 --- a/engine/cdrs.go +++ b/engine/cdrs.go @@ -30,6 +30,7 @@ import ( "github.com/cgrates/cgrates/cache" "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" ) @@ -87,7 +88,7 @@ func NewCdrServer(cgrCfg *config.CGRConfig, cdrDb CdrStorage, dataDB AccountingS stats = nil } return &CdrServer{cgrCfg: cgrCfg, cdrDb: cdrDb, dataDB: dataDB, - rals: rater, pubsub: pubsub, users: users, aliases: aliases, stats: stats, guard: Guardian, + rals: rater, pubsub: pubsub, users: users, aliases: aliases, stats: stats, guard: guardian.Guardian, httpPoster: utils.NewHTTPPoster(cgrCfg.HttpSkipTlsVerify, cgrCfg.ReplyTimeout)}, nil } @@ -100,7 +101,7 @@ type CdrServer struct { users rpcclient.RpcClientConnection aliases rpcclient.RpcClientConnection stats rpcclient.RpcClientConnection - guard *GuardianLock + guard *guardian.GuardianLock responseCache *cache.ResponseCache httpPoster *utils.HTTPPoster // used for replication } diff --git a/engine/responder.go b/engine/responder.go index 24fe43ee8..2a72acae9 100644 --- a/engine/responder.go +++ b/engine/responder.go @@ -29,6 +29,7 @@ import ( "github.com/cgrates/cgrates/balancer2go" "github.com/cgrates/cgrates/cache" "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" ) @@ -97,7 +98,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 := Guardian.Guard(func() (interface{}, error) { + r, e := guardian.Guardian.Guard(func() (interface{}, error) { return arg.GetCost() }, 0, arg.GetAccountKey()) if r != nil { @@ -616,7 +617,7 @@ func (rs *Responder) getCallCost(key *CallDescriptor, method string) (reply *Cal utils.Logger.Info(" Waiting for raters to register...") time.Sleep(1 * time.Second) // wait one second and retry } else { - _, err = Guardian.Guard(func() (interface{}, error) { + _, err = guardian.Guardian.Guard(func() (interface{}, error) { err = client.Call(method, *key, reply) return reply, err }, 0, key.GetAccountKey()) @@ -639,7 +640,7 @@ func (rs *Responder) callMethod(key *CallDescriptor, method string) (reply float utils.Logger.Info("Waiting for raters to register...") time.Sleep(1 * time.Second) // wait one second and retry } else { - _, err = Guardian.Guard(func() (interface{}, error) { + _, err = guardian.Guardian.Guard(func() (interface{}, error) { err = client.Call(method, *key, &reply) return reply, err }, 0, key.GetAccountKey()) diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 482161137..110cdf57f 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -27,6 +27,7 @@ import ( "github.com/cgrates/cgrates/cache" "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" @@ -1415,7 +1416,7 @@ func (ms *MongoStorage) AddLoadHistory(ldInst *utils.LoadInstance, loadHistSize } } - _, err = Guardian.Guard(func() (interface{}, error) { // Make sure we do it locked since other instance can modify history while we read it + _, err = guardian.Guardian.Guard(func() (interface{}, error) { // Make sure we do it locked since other instance can modify history while we read it // insert on first position existingLoadHistory = append(existingLoadHistory, nil) copy(existingLoadHistory[1:], existingLoadHistory[0:]) diff --git a/engine/storage_redis.go b/engine/storage_redis.go index ab3b75b0c..12ad9edb3 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -27,6 +27,7 @@ import ( "github.com/cgrates/cgrates/cache" "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "github.com/mediocregopher/radix.v2/pool" "github.com/mediocregopher/radix.v2/redis" @@ -1053,7 +1054,7 @@ func (rs *RedisStorage) AddLoadHistory(ldInst *utils.LoadInstance, loadHistSize if err != nil { return err } - _, err = Guardian.Guard(func() (interface{}, error) { // Make sure we do it locked since other instance can modify history while we read it + _, err = guardian.Guardian.Guard(func() (interface{}, error) { // Make sure we do it locked since other instance can modify history while we read it histLen, err := rs.Cmd("LLEN", utils.LOADINST_KEY).Int() if err != nil { return nil, err diff --git a/engine/tp_reader.go b/engine/tp_reader.go index c1a015e27..9acce3488 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -1916,19 +1916,25 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose, disable_reverse bool) (err } if !disable_reverse { if len(tpr.destinations) > 0 { - log.Print("Rebuilding Reverse Destinations") + if verbose { + log.Print("Rebuilding Reverse Destinations") + } if err = tpr.ratingStorage.RebuildReverseForPrefix(utils.REVERSE_DESTINATION_PREFIX); err != nil { return err } } if len(tpr.acntActionPlans) > 0 { - log.Print("Rebuilding Account Action Plans") + if verbose { + log.Print("Rebuilding Account Action Plans") + } if err = tpr.ratingStorage.RebuildReverseForPrefix(utils.AccountActionPlansPrefix); err != nil { return err } } if len(tpr.aliases) > 0 { - log.Print("Rebuilding Reverse Aliases") + if verbose { + log.Print("Rebuilding Reverse Aliases") + } if err = tpr.accountingStorage.RebuildReverseForPrefix(utils.REVERSE_ALIASES_PREFIX); err != nil { return err } diff --git a/engine/guardian.go b/guardian/guardian.go similarity index 99% rename from engine/guardian.go rename to guardian/guardian.go index 716d3aaec..99d4dcde7 100644 --- a/engine/guardian.go +++ b/guardian/guardian.go @@ -15,7 +15,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see */ -package engine +package guardian import ( "sync" diff --git a/engine/guardian_test.go b/guardian/guardian_test.go similarity index 99% rename from engine/guardian_test.go rename to guardian/guardian_test.go index 5f78dcab2..98b7ba8b8 100644 --- a/engine/guardian_test.go +++ b/guardian/guardian_test.go @@ -15,7 +15,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see */ -package engine +package guardian import ( "sync" diff --git a/sessionmanager/sessions.go b/sessionmanager/sessions.go index 6694f2126..3f984ed72 100644 --- a/sessionmanager/sessions.go +++ b/sessionmanager/sessions.go @@ -22,19 +22,20 @@ import ( "time" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" ) func NewSessions() *Sessions { return &Sessions{ sessionsMux: new(sync.Mutex), - guard: engine.Guardian, + guard: guardian.Guardian, } } type Sessions struct { sessions []*Session - sessionsMux *sync.Mutex // Lock the list operations - guard *engine.GuardianLock // Used to lock on uuid + sessionsMux *sync.Mutex // Lock the list operations + guard *guardian.GuardianLock // Used to lock on uuid } func (self *Sessions) indexSession(s *Session) { diff --git a/sessionmanager/smgeneric.go b/sessionmanager/smgeneric.go index ca68dea20..f16b18765 100644 --- a/sessionmanager/smgeneric.go +++ b/sessionmanager/smgeneric.go @@ -28,6 +28,7 @@ import ( "github.com/cgrates/cgrates/cache" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" ) @@ -347,7 +348,7 @@ func (smg *SMGeneric) getSessionIDsForPrefix(prefix string, passiveSessions bool // sessionStart will handle a new session, pass the connectionId so we can communicate on disconnect request func (smg *SMGeneric) sessionStart(evStart SMGenericEvent, clntConn rpcclient.RpcClientConnection) error { cgrID := evStart.GetCGRID(utils.META_DEFAULT) - processed, err := engine.Guardian.Guard(func() (interface{}, error) { // Lock it on CGRID level + processed, err := guardian.Guardian.Guard(func() (interface{}, error) { // Lock it on CGRID level if pSS := smg.passiveToActive(cgrID); len(pSS) != 0 { return true, nil // ToDo: handle here also debits } @@ -379,7 +380,7 @@ func (smg *SMGeneric) sessionStart(evStart SMGenericEvent, clntConn rpcclient.Rp // sessionEnd will end a session from outside func (smg *SMGeneric) sessionEnd(cgrID string, usage time.Duration) error { - _, err := engine.Guardian.Guard(func() (interface{}, error) { // Lock it on UUID level + _, err := guardian.Guardian.Guard(func() (interface{}, error) { // Lock it on UUID level ss := smg.getSessions(cgrID, false) if len(ss) == 0 { if ss = smg.passiveToActive(cgrID); len(ss) == 0 { @@ -414,7 +415,7 @@ func (smg *SMGeneric) sessionEnd(cgrID string, usage time.Duration) error { // sessionRelocate is used when an update will relocate an initial session (eg multiple data streams) func (smg *SMGeneric) sessionRelocate(initialID, cgrID, newOriginID string) error { - _, err := engine.Guardian.Guard(func() (interface{}, error) { // Lock it on initialID level + _, err := guardian.Guardian.Guard(func() (interface{}, error) { // Lock it on initialID level if utils.IsSliceMember([]string{initialID, cgrID, newOriginID}, "") { // Not allowed empty params here return nil, utils.ErrMandatoryIeMissing }