From 9b406acf71c5b4021d05e64daaed2c2532a727fc Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Wed, 17 Nov 2021 16:29:19 +0200 Subject: [PATCH] Make usageTTL opt a pointer to be able to determine whether or not it has been set in config --- config/config_test.go | 4 ++-- config/resourcescfg.go | 27 +++++++++++++++---------- config/resourcescfg_test.go | 2 +- engine/resources.go | 40 +++++++++++++++---------------------- utils/options.go | 15 ++++++++++++++ 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index 03ae5c1c4..d89766526 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -720,7 +720,7 @@ func TestCgrCfgJSONDefaultsResLimCfg(t *testing.T) { SuffixIndexedFields: &[]string{}, Opts: &ResourcesOpts{ UsageID: utils.EmptyString, - UsageTTL: 72 * time.Hour, + UsageTTL: utils.DurationPointer(72 * time.Hour), Units: 1, }, } @@ -2003,7 +2003,7 @@ func TestResourceSConfig(t *testing.T) { NestedFields: false, Opts: &ResourcesOpts{ UsageID: "", - UsageTTL: 72 * time.Hour, + UsageTTL: utils.DurationPointer(72 * time.Hour), Units: 1, }, } diff --git a/config/resourcescfg.go b/config/resourcescfg.go index ee16cede0..2d05f94a3 100644 --- a/config/resourcescfg.go +++ b/config/resourcescfg.go @@ -26,7 +26,7 @@ import ( type ResourcesOpts struct { UsageID string - UsageTTL time.Duration + UsageTTL *time.Duration Units float64 } @@ -51,9 +51,11 @@ func (resOpts *ResourcesOpts) loadFromJSONCfg(jsnCfg *ResourcesOptsJson) (err er resOpts.UsageID = *jsnCfg.UsageID } if jsnCfg.UsageTTL != nil { - if resOpts.UsageTTL, err = utils.ParseDurationWithNanosecs(*jsnCfg.UsageTTL); err != nil { + var ttl time.Duration + if ttl, err = utils.ParseDurationWithNanosecs(*jsnCfg.UsageTTL); err != nil { return err } + resOpts.UsageTTL = utils.DurationPointer(ttl) } if jsnCfg.Units != nil { resOpts.Units = *jsnCfg.Units @@ -119,9 +121,11 @@ func (rlcfg *ResourceSConfig) loadFromJSONCfg(jsnCfg *ResourceSJsonCfg) (err err // AsMapInterface returns the config as a map[string]interface{} func (rlcfg *ResourceSConfig) AsMapInterface() (initialMP map[string]interface{}) { opts := map[string]interface{}{ - utils.MetaUsageIDCfg: rlcfg.Opts.UsageID, - utils.MetaUsageTTLCfg: rlcfg.Opts.UsageTTL, - utils.MetaUnitsCfg: rlcfg.Opts.Units, + utils.MetaUsageIDCfg: rlcfg.Opts.UsageID, + utils.MetaUnitsCfg: rlcfg.Opts.Units, + } + if rlcfg.Opts.UsageTTL != nil { + opts[utils.MetaUsageTTLCfg] = *rlcfg.Opts.UsageTTL } initialMP = map[string]interface{}{ utils.EnabledCfg: rlcfg.Enabled, @@ -167,12 +171,15 @@ func (rlcfg *ResourceSConfig) AsMapInterface() (initialMP map[string]interface{} return } -func (resOpts *ResourcesOpts) Clone() *ResourcesOpts { - return &ResourcesOpts{ - UsageID: resOpts.UsageID, - UsageTTL: resOpts.UsageTTL, - Units: resOpts.Units, +func (resOpts *ResourcesOpts) Clone() (cln *ResourcesOpts) { + cln = &ResourcesOpts{ + UsageID: resOpts.UsageID, + Units: resOpts.Units, } + if resOpts.UsageTTL != nil { + cln.UsageTTL = utils.DurationPointer(*resOpts.UsageTTL) + } + return } // Clone returns a deep copy of ResourceSConfig diff --git a/config/resourcescfg_test.go b/config/resourcescfg_test.go index 0bd974a79..a9b97bffd 100644 --- a/config/resourcescfg_test.go +++ b/config/resourcescfg_test.go @@ -46,7 +46,7 @@ func TestResourceSConfigloadFromJsonCfgCase1(t *testing.T) { SuffixIndexedFields: &[]string{"*req.index1"}, NestedFields: true, Opts: &ResourcesOpts{ - UsageTTL: 72 * time.Hour, + UsageTTL: utils.DurationPointer(72 * time.Hour), Units: 1, }, } diff --git a/engine/resources.go b/engine/resources.go index e8b725e99..b0d0e5ea1 100644 --- a/engine/resources.go +++ b/engine/resources.go @@ -706,13 +706,11 @@ func (rS *ResourceService) V1ResourcesForEvent(args *utils.CGREvent, reply *Reso } // end of RPC caching - ttl := rS.cgrcfg.ResourceSCfg().Opts.UsageTTL - if opt, has := args.APIOpts[utils.OptsResourcesUsageTTL]; has { - if ttl, err = utils.IfaceAsDuration(opt); err != nil { - return - } + var usageTTL *time.Duration + if usageTTL, err = utils.GetDurationPointerOpts(args, rS.cgrcfg.ResourceSCfg().Opts.UsageTTL, + utils.OptsResourcesUsageTTL); err != nil { + return } - usageTTL := utils.DurationPointer(ttl) var mtcRLs Resources if mtcRLs, err = rS.matchingResourcesForEvent(tnt, args, usageID, usageTTL); err != nil { return err @@ -758,13 +756,11 @@ func (rS *ResourceService) V1AuthorizeResources(args *utils.CGREvent, reply *str } // end of RPC caching - ttl := rS.cgrcfg.ResourceSCfg().Opts.UsageTTL - if opt, has := args.APIOpts[utils.OptsResourcesUsageTTL]; has { - if ttl, err = utils.IfaceAsDuration(opt); err != nil { - return - } + var usageTTL *time.Duration + if usageTTL, err = utils.GetDurationPointerOpts(args, rS.cgrcfg.ResourceSCfg().Opts.UsageTTL, + utils.OptsResourcesUsageTTL); err != nil { + return } - usageTTL := utils.DurationPointer(ttl) var mtcRLs Resources if mtcRLs, err = rS.matchingResourcesForEvent(tnt, args, usageID, usageTTL); err != nil { return err @@ -827,13 +823,11 @@ func (rS *ResourceService) V1AllocateResources(args *utils.CGREvent, reply *stri } // end of RPC caching - ttl := rS.cgrcfg.ResourceSCfg().Opts.UsageTTL - if opt, has := args.APIOpts[utils.OptsResourcesUsageTTL]; has { - if ttl, err = utils.IfaceAsDuration(opt); err != nil { - return - } + var usageTTL *time.Duration + if usageTTL, err = utils.GetDurationPointerOpts(args, rS.cgrcfg.ResourceSCfg().Opts.UsageTTL, + utils.OptsResourcesUsageTTL); err != nil { + return } - usageTTL := utils.DurationPointer(ttl) var mtcRLs Resources if mtcRLs, err = rS.matchingResourcesForEvent(tnt, args, usageID, usageTTL); err != nil { @@ -900,13 +894,11 @@ func (rS *ResourceService) V1ReleaseResources(args *utils.CGREvent, reply *strin } // end of RPC caching - ttl := rS.cgrcfg.ResourceSCfg().Opts.UsageTTL - if opt, has := args.APIOpts[utils.OptsResourcesUsageTTL]; has { - if ttl, err = utils.IfaceAsDuration(opt); err != nil { - return - } + var usageTTL *time.Duration + if usageTTL, err = utils.GetDurationPointerOpts(args, rS.cgrcfg.ResourceSCfg().Opts.UsageTTL, + utils.OptsResourcesUsageTTL); err != nil { + return } - usageTTL := utils.DurationPointer(ttl) var mtcRLs Resources if mtcRLs, err = rS.matchingResourcesForEvent(tnt, args, usageID, usageTTL); err != nil { diff --git a/utils/options.go b/utils/options.go index 195b5ea59..834091e0b 100644 --- a/utils/options.go +++ b/utils/options.go @@ -130,3 +130,18 @@ func GetIntPointerOpts(ev *CGREvent, dftOpt *int, optNames ...string) (cfgOpt *i } return dftOpt, nil } + +// GetDurationPointerOpts checks the specified option names in order among the keys in APIOpts returning the first value it finds as *time.Duration, otherwise it +// returns the default option (usually the value specified in config) +func GetDurationPointerOpts(ev *CGREvent, dftOpt *time.Duration, optNames ...string) (cfgOpt *time.Duration, err error) { + for _, optName := range optNames { + if opt, has := ev.APIOpts[optName]; has { + var value time.Duration + if value, err = IfaceAsDuration(opt); err != nil { + return + } + return DurationPointer(value), nil + } + } + return dftOpt, nil +}