diff --git a/engine/calldesc.go b/engine/calldesc.go index ceca357bc..817b3ce86 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -368,6 +368,11 @@ func (cd *CallDescriptor) roundTimeSpansToIncrement(timespans TimeSpans) []*Time return timespans } +// Returns call descripor's total duration +func (cd *CallDescriptor) GetDuration() time.Duration { + return cd.TimeEnd.Sub(cd.TimeStart) +} + /* Creates a CallCost structure with the cost information calculated for the received CallDescriptor. */ diff --git a/sessionmanager/fssessionmanager.go b/sessionmanager/fssessionmanager.go index fcc10e50d..677a0ec3e 100644 --- a/sessionmanager/fssessionmanager.go +++ b/sessionmanager/fssessionmanager.go @@ -310,10 +310,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) (cc *engine.CallCost) { cc = &engine.CallCost{} - cd.LoopIndex = index - cd.CallDuration += sm.debitPeriod err := sm.connector.MaxDebit(*cd, cc) if err != nil { engine.Logger.Err(fmt.Sprintf("Could not complete debit opperation: %v", err)) diff --git a/sessionmanager/session.go b/sessionmanager/session.go index 2af31f6f8..40b57cc42 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -77,6 +77,7 @@ func NewSession(ev Event, sm SessionManager) (s *Session) { func (s *Session) startDebitLoop() { nextCd := *s.callDescriptor index := 0.0 + debitPeriod := s.sessionManager.GetDebitPeriod() for { select { case <-s.stopDebit: @@ -86,9 +87,14 @@ func (s *Session) startDebitLoop() { if index > 0 { // first time use the session start time nextCd.TimeStart = nextCd.TimeEnd } - nextCd.TimeEnd = nextCd.TimeStart.Add(s.sessionManager.GetDebitPeriod()) - cc := s.sessionManager.LoopAction(s, &nextCd, index) - nextCd.TimeEnd = cc.GetEndTime() + nextCd.TimeEnd = nextCd.TimeStart.Add(debitPeriod) + nextCd.LoopIndex = index + nextCd.CallDuration += debitPeriod // first presumed duration + cc := s.sessionManager.LoopAction(s, &nextCd) + nextCd.TimeEnd = cc.GetEndTime() // set debited timeEnd + // update call duration with real debited duration + nextCd.CallDuration -= debitPeriod + nextCd.CallDuration += nextCd.GetDuration() time.Sleep(cc.GetDuration()) index++ } diff --git a/sessionmanager/sessionmanager.go b/sessionmanager/sessionmanager.go index 9eb2f68ae..ea0074786 100644 --- a/sessionmanager/sessionmanager.go +++ b/sessionmanager/sessionmanager.go @@ -19,16 +19,17 @@ along with this program. If not, see package sessionmanager import ( + "time" + "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" - "time" ) type SessionManager interface { Connect(*config.CGRConfig) error DisconnectSession(*Session, string) RemoveSession(*Session) - LoopAction(*Session, *engine.CallDescriptor, float64) *engine.CallCost + LoopAction(*Session, *engine.CallDescriptor) *engine.CallCost GetDebitPeriod() time.Duration GetDbLogger() engine.LogStorage Shutdown() error