diff --git a/engine/calldesc.go b/engine/calldesc.go index f8864935b..667f1656b 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -743,22 +743,33 @@ func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) { func (cd *CallDescriptor) RefundIncrements() (left float64, err error) { cd.account = nil // make sure it's not cached - accountsCache := make(map[string]*Account) + // get account list for locking + // all must be locked in order to use cache + var accountIDs []string for _, increment := range cd.Increments { - account, found := accountsCache[increment.BalanceInfo.AccountId] - Guardian.Guard(func() (interface{}, error) { + accountIDs = append(accountIDs, increment.BalanceInfo.AccountId) + } + Guardian.Guard(func() (interface{}, error) { + accountsCache := make(map[string]*Account) + for _, increment := range cd.Increments { + account, found := accountsCache[increment.BalanceInfo.AccountId] if !found { if acc, err := accountingStorage.GetAccount(increment.BalanceInfo.AccountId); err == nil && acc != nil { account = acc accountsCache[increment.BalanceInfo.AccountId] = account + // will save the account only once at the end of the function defer accountingStorage.SetAccount(account) } } - utils.Logger.Info(fmt.Sprintf("Refunding increment %+v", increment)) + if account == nil { + utils.Logger.Warning(fmt.Sprintf("Could not get the account to be refunded: %s", increment.BalanceInfo.AccountId)) + continue + } + //utils.Logger.Info(fmt.Sprintf("Refunding increment %+v", increment)) account.refundIncrement(increment, cd, true) - return 0, err - }, 0, increment.BalanceInfo.AccountId) - } + } + return 0, err + }, 0, accountIDs...) return 0, err } diff --git a/sessionmanager/session.go b/sessionmanager/session.go index f30bf276b..0713956a2 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -201,6 +201,7 @@ func (s *Session) Refund(lastCC *engine.CallCost, hangupTime time.Time) error { Subject: lastCC.Subject, Account: lastCC.Account, Destination: lastCC.Destination, + TOR: lastCC.TOR, Increments: refundIncrements, } var response float64 diff --git a/sessionmanager/smg_it_test.go b/sessionmanager/smg_it_test.go index 0fc3a6196..acf495e1f 100644 --- a/sessionmanager/smg_it_test.go +++ b/sessionmanager/smg_it_test.go @@ -111,6 +111,9 @@ func TestSMGTPFromFolder(t *testing.T) { } func TestSMGMonetaryRefound(t *testing.T) { + if !*testIntegration { + return + } smgEv := SMGenericEvent{ utils.EVENT_NAME: "TEST_EVENT", utils.TOR: utils.VOICE, @@ -171,6 +174,9 @@ func TestSMGMonetaryRefound(t *testing.T) { } func TestSMGVoiceRefound(t *testing.T) { + if !*testIntegration { + return + } smgEv := SMGenericEvent{ utils.EVENT_NAME: "TEST_EVENT", utils.TOR: utils.VOICE, diff --git a/sessionmanager/smg_session.go b/sessionmanager/smg_session.go index 2ee36f58e..2837ccb3b 100644 --- a/sessionmanager/smg_session.go +++ b/sessionmanager/smg_session.go @@ -152,6 +152,7 @@ func (self *SMGSession) refund(refundDuration time.Duration) error { Subject: lastCC.Subject, Account: lastCC.Account, Destination: lastCC.Destination, + TOR: lastCC.TOR, Increments: refundIncrements, } var response float64