Simplified locking for SessionS in order to prevent deadlock bugs

This commit is contained in:
DanB
2019-10-07 19:24:57 +02:00
parent 8cddeca1a0
commit 5dbaee2d33
5 changed files with 291 additions and 266 deletions

View File

@@ -128,6 +128,15 @@ func (me MapEvent) GetDurationPtrIgnoreErrors(fldName string) (d *time.Duration)
return
}
// GetDurationPtrOrDefault returns pointer or default if fldName is missing
func (me MapEvent) GetDurationPtrOrDefault(fldName string, dflt *time.Duration) (d *time.Duration, err error) {
if d, err = me.GetDurationPtr(fldName); err == utils.ErrNotFound {
d = dflt
err = nil
}
return
}
// GetTime returns a field as Time
func (me MapEvent) GetTime(fldName string, timezone string) (t time.Time, err error) {
fldIface, has := me[fldName]

View File

@@ -699,3 +699,15 @@ func TestMapEventGetDurationPtrIgnoreErrors(t *testing.T) {
t.Errorf("Expected: %+v, received: %+v", expected, rply)
}
}
func TestMapEventGetDurationPtrOrDefault(t *testing.T) {
dflt := &time.Duration(1)
if rcv, err := mapEv.GetDurationPtrOrDefault("test7", &dflt); !reflect.DeepEqual(rcv, dflt) {
t.Error("received: ", ptr)
}
newVal := time.Duration(2)
mapEv["test7"] = newVal
if ptr, err := mapEv.GetDurationPtrOrDefault("test7", &dflt); !reflect.DeepEqual(rcv, newVal) {
t.Error("received: ", ptr)
}
}

View File

@@ -39,6 +39,11 @@ var unratedReqs = engine.MapEvent{
utils.META_RATED: struct{}{},
}
var authReqs = engine.MapEvent{
utils.META_PREPAID: struct{}{},
utils.META_PSEUDOPREPAID: struct{}{},
}
// SessionSClient is the interface implemented by Agents which are able to
// communicate bidirectionally with SessionS and remote Communication Switch
type SessionSClient interface {
@@ -81,12 +86,12 @@ func getSessionTTL(ev *engine.SafEvent, cfgSessionTTL time.Duration,
return
}
func GetSetCGRID(ev *engine.SafEvent) (cgrID string) {
func GetSetCGRID(ev engine.MapEvent) (cgrID string) {
cgrID = ev.GetStringIgnoreErrors(utils.CGRID)
if cgrID == "" {
cgrID = utils.Sha1(ev.GetStringIgnoreErrors(utils.OriginID),
ev.GetStringIgnoreErrors(utils.OriginHost))
ev.Set(utils.CGRID, cgrID)
ev[utils.CGRID] = cgrID
}
return
}

View File

@@ -68,10 +68,10 @@ type Session struct {
CGRID string
Tenant string
ResourceID string
ClientConnID string // connection ID towards the client so we can recover from passive
EventStart *engine.SafEvent // Event which started the session
DebitInterval time.Duration // execute debits for *prepaid runs
SRuns []*SRun // forked based on ChargerS
ClientConnID string // connection ID towards the client so we can recover from passive
EventStart engine.MapEvent // Event which started the session
DebitInterval time.Duration // execute debits for *prepaid runs
SRuns []*SRun // forked based on ChargerS
debitStop chan struct{}
sTerminator *sTerminator // automatic timeout for the session
@@ -201,7 +201,7 @@ func (s *Session) TotalUsage() (tDur time.Duration) {
// AsCGREvents is not thread safe since it is supposed to run by the time Session is closed
func (s *Session) asCGREvents() (cgrEvs []*utils.CGREvent, err error) {
cgrEvs = make([]*utils.CGREvent, len(s.SRuns)+1) // so we can gather all cdr info while under lock
rawEv := s.EventStart.MapEvent()
rawEv := s.EventStart.Clone()
rawEv[utils.RunID] = utils.MetaRaw
cgrEvs[0] = &utils.CGREvent{
Tenant: s.Tenant,

File diff suppressed because it is too large Load Diff