diff --git a/engine/callcost.go b/engine/callcost.go index 367789cd4..499e1c09b 100644 --- a/engine/callcost.go +++ b/engine/callcost.go @@ -66,3 +66,10 @@ func (cc *CallCost) GetStartTime() time.Time { } return cc.Timespans[0].TimeStart } + +func (cc *CallCost) GetTotalDuration() (td time.Duration) { + for _, ts := range cc.Timespans { + td += ts.GetDuration() + } + return +} diff --git a/sessionmanager/fssessionmanager.go b/sessionmanager/fssessionmanager.go index fe989f0a9..130971b66 100644 --- a/sessionmanager/fssessionmanager.go +++ b/sessionmanager/fssessionmanager.go @@ -315,8 +315,8 @@ func (sm *FSSessionManager) OnChannelHangupComplete(ev Event) { } -func (sm *FSSessionManager) LoopAction(s *Session, cd *engine.CallDescriptor, index float64) { - cc := &engine.CallCost{} +func (sm *FSSessionManager) LoopAction(s *Session, cd *engine.CallDescriptor, index float64) (cc *engine.CallCost) { + cc = &engine.CallCost{} cd.LoopIndex = index cd.Amount = sm.debitPeriod.Seconds() cd.CallDuration += time.Duration(cd.Amount) * time.Second @@ -326,18 +326,14 @@ func (sm *FSSessionManager) LoopAction(s *Session, cd *engine.CallDescriptor, in // disconnect session s.sessionManager.DisconnectSession(s, SYSTEM_ERROR) } - nbts := len(cc.Timespans) - remainingSeconds := 0.0 engine.Logger.Debug(fmt.Sprintf("Result of MaxDebit call: %v", cc)) - if nbts > 0 { - remainingSeconds = cc.Timespans[nbts-1].TimeEnd.Sub(cc.Timespans[0].TimeStart).Seconds() - } - if remainingSeconds == 0 || err != nil { + if cc.GetTotalDuration() == 0 || err != nil { engine.Logger.Info(fmt.Sprintf("No credit left: Disconnect %v", s)) sm.DisconnectSession(s, INSUFFICIENT_FUNDS) return } s.CallCosts = append(s.CallCosts, cc) + return } func (sm *FSSessionManager) GetDebitPeriod() time.Duration { diff --git a/sessionmanager/session.go b/sessionmanager/session.go index efa703124..3e5cadb20 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -86,8 +86,8 @@ func (s *Session) startDebitLoop() { nextCd.TimeStart = nextCd.TimeEnd } nextCd.TimeEnd = nextCd.TimeStart.Add(s.sessionManager.GetDebitPeriod()) - s.sessionManager.LoopAction(s, &nextCd, index) - time.Sleep(s.sessionManager.GetDebitPeriod()) + cc := s.sessionManager.LoopAction(s, &nextCd, index) + time.Sleep(cc.GetTotalDuration()) index++ } } diff --git a/sessionmanager/sessionmanager.go b/sessionmanager/sessionmanager.go index ef7311e75..9eb2f68ae 100644 --- a/sessionmanager/sessionmanager.go +++ b/sessionmanager/sessionmanager.go @@ -28,7 +28,7 @@ type SessionManager interface { Connect(*config.CGRConfig) error DisconnectSession(*Session, string) RemoveSession(*Session) - LoopAction(*Session, *engine.CallDescriptor, float64) + LoopAction(*Session, *engine.CallDescriptor, float64) *engine.CallCost GetDebitPeriod() time.Duration GetDbLogger() engine.LogStorage Shutdown() error