fix for *voice balances refund

This commit is contained in:
Radu Ioan Fericean
2016-01-06 14:16:10 +02:00
parent bbb44cb02e
commit 382f4c308d
4 changed files with 26 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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