mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-23 08:08:45 +05:00
Dynamic usage_ttl for resources
This commit is contained in:
committed by
Dan Christian Bogos
parent
ed0929e533
commit
8811fd36e2
@@ -436,7 +436,7 @@ func (rS *ResourceService) cachedResourcesForEvent(evUUID string) (rs Resources)
|
||||
}
|
||||
|
||||
// matchingResourcesForEvent returns ordered list of matching resources which are active by the time of the call
|
||||
func (rS *ResourceService) matchingResourcesForEvent(ev *utils.CGREvent) (rs Resources, err error) {
|
||||
func (rS *ResourceService) matchingResourcesForEvent(ev *utils.CGREvent, usageTTL *time.Duration) (rs Resources, err error) {
|
||||
matchingResources := make(map[string]*Resource)
|
||||
rIDs, err := matchingItemIDsForEvent(ev.Event, rS.stringIndexedFields, rS.prefixIndexedFields,
|
||||
rS.dm, utils.ResourceFilterIndexes+ev.Tenant)
|
||||
@@ -470,7 +470,11 @@ func (rS *ResourceService) matchingResourcesForEvent(ev *utils.CGREvent) (rs Res
|
||||
if rPrf.Stored && r.dirty == nil {
|
||||
r.dirty = utils.BoolPointer(false)
|
||||
}
|
||||
if rPrf.UsageTTL >= 0 {
|
||||
if usageTTL != nil {
|
||||
if *usageTTL != 0 {
|
||||
r.ttl = usageTTL
|
||||
}
|
||||
} else if rPrf.UsageTTL >= 0 {
|
||||
r.ttl = utils.DurationPointer(rPrf.UsageTTL)
|
||||
}
|
||||
r.rPrf = rPrf
|
||||
@@ -529,7 +533,7 @@ func (rS *ResourceService) V1ResourcesForEvent(args utils.ArgRSv1ResourceUsage,
|
||||
mtcRLs = rS.cachedResourcesForEvent(args.TenantID())
|
||||
}
|
||||
if mtcRLs == nil {
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(&args.CGREvent); err != nil {
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(&args.CGREvent, args.UsageTTL); err != nil {
|
||||
return err
|
||||
}
|
||||
cache.Set(utils.EventResourcesPrefix+args.TenantID(), mtcRLs.tenantIDs(), true, "")
|
||||
@@ -552,7 +556,7 @@ func (rS *ResourceService) V1AuthorizeResources(args utils.ArgRSv1ResourceUsage,
|
||||
}
|
||||
mtcRLs := rS.cachedResourcesForEvent(args.TenantID())
|
||||
if mtcRLs == nil {
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(&args.CGREvent); err != nil {
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(&args.CGREvent, args.UsageTTL); err != nil {
|
||||
return err
|
||||
}
|
||||
cache.Set(utils.EventResourcesPrefix+args.TenantID(), mtcRLs.tenantIDs(), true, "")
|
||||
@@ -583,7 +587,7 @@ func (rS *ResourceService) V1AllocateResource(args utils.ArgRSv1ResourceUsage, r
|
||||
var wasCached bool
|
||||
mtcRLs := rS.cachedResourcesForEvent(args.UsageID)
|
||||
if mtcRLs == nil {
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(&args.CGREvent); err != nil {
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(&args.CGREvent, args.UsageTTL); err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@@ -638,7 +642,7 @@ func (rS *ResourceService) V1ReleaseResource(args utils.ArgRSv1ResourceUsage, re
|
||||
}
|
||||
mtcRLs := rS.cachedResourcesForEvent(args.UsageID)
|
||||
if mtcRLs == nil {
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(&args.CGREvent); err != nil {
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(&args.CGREvent, args.UsageTTL); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ var (
|
||||
rs Resources
|
||||
cloneExpTimeResource time.Time
|
||||
expTimeResource = time.Now().Add(time.Duration(20 * time.Minute))
|
||||
timeDurationExample = time.Duration(10) * time.Second
|
||||
resserv ResourceService
|
||||
dmRES *DataManager
|
||||
resprf = []*ResourceProfile{
|
||||
@@ -588,7 +589,7 @@ func TestCachedResourcesForEvent(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRSmatchingResourcesForEvent(t *testing.T) {
|
||||
mres, err := resserv.matchingResourcesForEvent(resEvs[0])
|
||||
mres, err := resserv.matchingResourcesForEvent(resEvs[0], &timeDurationExample)
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
@@ -599,7 +600,7 @@ func TestRSmatchingResourcesForEvent(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(resourceTest[0].rPrf, mres[0].rPrf) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].rPrf, mres[0].rPrf)
|
||||
}
|
||||
mres, err = resserv.matchingResourcesForEvent(resEvs[1])
|
||||
mres, err = resserv.matchingResourcesForEvent(resEvs[1], &timeDurationExample)
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
@@ -610,7 +611,7 @@ func TestRSmatchingResourcesForEvent(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(resourceTest[1].rPrf, mres[0].rPrf) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", resourceTest[1].rPrf, mres[0].rPrf)
|
||||
}
|
||||
mres, err = resserv.matchingResourcesForEvent(resEvs[2])
|
||||
mres, err = resserv.matchingResourcesForEvent(resEvs[2], &timeDurationExample)
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
@@ -621,7 +622,7 @@ func TestRSmatchingResourcesForEvent(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(resourceTest[2].rPrf, mres[0].rPrf) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", resourceTest[2].rPrf, mres[0].rPrf)
|
||||
}
|
||||
mres, err = resserv.matchingResourcesForEvent(resEvs[3])
|
||||
mres, err = resserv.matchingResourcesForEvent(resEvs[3], &timeDurationExample)
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
|
||||
@@ -1284,8 +1284,9 @@ type AttrRLsCache struct {
|
||||
|
||||
type ArgRSv1ResourceUsage struct {
|
||||
CGREvent
|
||||
UsageID string // ResourceUsage Identifier
|
||||
Units float64
|
||||
UsageID string // ResourceUsage Identifier
|
||||
UsageTTL *time.Duration
|
||||
Units float64
|
||||
}
|
||||
|
||||
func (args *ArgRSv1ResourceUsage) TenantID() string {
|
||||
|
||||
Reference in New Issue
Block a user