From 9a42adc71644e6c0b9c5a84d380bd04618ea68b6 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Tue, 15 Apr 2025 17:23:57 +0300 Subject: [PATCH] Retrieve routes default ratio from global var to prevent cyclic import (utils > config > utils) --- ees/httpjsonmap.go | 2 +- ees/httppost.go | 2 +- engine/globalvars.go | 14 ++++----- engine/globalvars_test.go | 2 +- engine/routes.go | 61 +++++++++++++++++++++++---------------- loaders/csvreader.go | 2 +- services/globalvars.go | 2 ++ 7 files changed, 47 insertions(+), 38 deletions(-) diff --git a/ees/httpjsonmap.go b/ees/httpjsonmap.go index 5fb55f148..d1c492d4e 100644 --- a/ees/httpjsonmap.go +++ b/ees/httpjsonmap.go @@ -38,7 +38,7 @@ func NewHTTPjsonMapEE(cfg *config.EventExporterCfg, cgrCfg *config.CGRConfig, fi pstrJSON = &HTTPjsonMapEE{ cfg: cfg, dc: dc, - client: &http.Client{Transport: engine.GetHTTPPstrTransport(), Timeout: cgrCfg.GeneralCfg().ReplyTimeout}, + client: &http.Client{Transport: engine.HTTPPstrTransport(), Timeout: cgrCfg.GeneralCfg().ReplyTimeout}, reqs: newConcReq(cfg.ConcurrentRequests), } pstrJSON.hdr, err = pstrJSON.composeHeader(cgrCfg, filterS) diff --git a/ees/httppost.go b/ees/httppost.go index fdedb05ea..866f23ae1 100644 --- a/ees/httppost.go +++ b/ees/httppost.go @@ -34,7 +34,7 @@ func NewHTTPPostEE(cfg *config.EventExporterCfg, cgrCfg *config.CGRConfig, filte httpPost = &HTTPPostEE{ cfg: cfg, dc: dc, - client: &http.Client{Transport: engine.GetHTTPPstrTransport(), Timeout: cgrCfg.GeneralCfg().ReplyTimeout}, + client: &http.Client{Transport: engine.HTTPPstrTransport(), Timeout: cgrCfg.GeneralCfg().ReplyTimeout}, reqs: newConcReq(cfg.ConcurrentRequests), } httpPost.hdr, err = httpPost.composeHeader(cgrCfg, filterS) diff --git a/engine/globalvars.go b/engine/globalvars.go index c1f02073d..70fcabe5d 100644 --- a/engine/globalvars.go +++ b/engine/globalvars.go @@ -27,17 +27,13 @@ import ( // this file will contain all the global variable that are used by other subsystems var ( - httpPstrTransport *http.Transport connMgr *ConnManager + httpPstrTransport = config.CgrConfig().HTTPCfg().ClientOpts ) -func init() { - httpPstrTransport = config.CgrConfig().HTTPCfg().ClientOpts -} - // SetConnManager is the exported method to set the connectionManager used when operate on an account. -func SetConnManager(conMgr *ConnManager) { - connMgr = conMgr +func SetConnManager(cm *ConnManager) { + connMgr = cm } // SetHTTPPstrTransport sets the http transport to be used by the HTTP Poster @@ -45,7 +41,7 @@ func SetHTTPPstrTransport(pstrTransport *http.Transport) { httpPstrTransport = pstrTransport } -// GetHTTPPstrTransport gets the http transport to be used by the HTTP Poster -func GetHTTPPstrTransport() *http.Transport { +// HTTPPstrTransport gets the http transport to be used by the HTTP Poster +func HTTPPstrTransport() *http.Transport { return httpPstrTransport } diff --git a/engine/globalvars_test.go b/engine/globalvars_test.go index cdcc49175..86268a3a6 100644 --- a/engine/globalvars_test.go +++ b/engine/globalvars_test.go @@ -45,7 +45,7 @@ func TestGlobalvarsGetHTTPPstrTransport(t *testing.T) { DisableKeepAlives: true, } SetHTTPPstrTransport(newTransport) - getTransport := GetHTTPPstrTransport() + getTransport := HTTPPstrTransport() if getTransport != newTransport { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", newTransport, getTransport) } diff --git a/engine/routes.go b/engine/routes.go index 447322b31..2d7826189 100644 --- a/engine/routes.go +++ b/engine/routes.go @@ -30,6 +30,10 @@ import ( "github.com/cgrates/cgrates/utils" ) +// RoutesDefaultRatio is the fallback value for route ratios when none are +// specified. Used to avoid circular dependencies with the config package. +var RoutesDefaultRatio = 1 + // Route defines routes related information used within a RouteProfile type Route struct { ID string // RouteID @@ -63,33 +67,40 @@ type RouteProfileWithAPIOpts struct { APIOpts map[string]any } +// compileCacheParameters prepares route ratios for MetaLoad sorting by parsing the +// SortingParameters and applying appropriate ratio values to each route. func (rp *RouteProfile) compileCacheParameters() error { - if rp.Sorting == utils.MetaLoad { - // construct the map for ratio - ratioMap := make(map[string]int) - // []string{"routeID:Ratio"} - for _, splIDWithRatio := range rp.SortingParameters { - splitted := strings.Split(splIDWithRatio, utils.ConcatenatedKeySep) - ratioVal, err := strconv.Atoi(splitted[1]) - if err != nil { - return err - } - ratioMap[splitted[0]] = ratioVal - } - // add the ratio for each route - for _, route := range rp.Routes { - route.cacheRoute = make(map[string]any) - if ratioRoute, has := ratioMap[route.ID]; !has { // in case that ratio isn't defined for specific routes check for default - if ratioDefault, has := ratioMap[utils.MetaDefault]; !has { // in case that *default ratio isn't defined take it from config - route.cacheRoute[utils.MetaRatio] = config.CgrConfig().RouteSCfg().DefaultRatio - } else { - route.cacheRoute[utils.MetaRatio] = ratioDefault - } - } else { - route.cacheRoute[utils.MetaRatio] = ratioRoute - } - } + if rp.Sorting != utils.MetaLoad { + return nil } + + // Parse route ID to ratio mappings. + ratioMap := make(map[string]int) + for _, param := range rp.SortingParameters { + parts := strings.Split(param, utils.ConcatenatedKeySep) + ratio, err := strconv.Atoi(parts[1]) + if err != nil { + return err + } + ratioMap[parts[0]] = ratio + } + + // Get default ratio (from map or config). + defaultRatio := RoutesDefaultRatio + if mapDefault, exists := ratioMap[utils.MetaDefault]; exists { + defaultRatio = mapDefault + } + + // Apply appropriate ratio to each route. + for _, route := range rp.Routes { + route.cacheRoute = make(map[string]any) + ratio, exists := ratioMap[route.ID] + if !exists { + ratio = defaultRatio + } + route.cacheRoute[utils.MetaRatio] = ratio + } + return nil } diff --git a/loaders/csvreader.go b/loaders/csvreader.go index 6aaa02d00..04f885d16 100644 --- a/loaders/csvreader.go +++ b/loaders/csvreader.go @@ -89,7 +89,7 @@ func (urlProvider) Open(dPath, fn string) (_ io.ReadCloser, err error) { } var req *http.Response if req, err = (&http.Client{ - Transport: engine.GetHTTPPstrTransport(), + Transport: engine.HTTPPstrTransport(), Timeout: config.CgrConfig().GeneralCfg().ReplyTimeout, }).Get(path); err != nil { err = utils.ErrPathNotReachable(path) diff --git a/services/globalvars.go b/services/globalvars.go index 50060801d..0cfca2e6b 100644 --- a/services/globalvars.go +++ b/services/globalvars.go @@ -43,6 +43,7 @@ type GlobalVarS struct { // Start should handle the sercive start func (gv *GlobalVarS) Start(_ *utils.SyncedChan, _ *servmanager.ServiceRegistry) error { engine.SetHTTPPstrTransport(gv.cfg.HTTPCfg().ClientOpts) + engine.RoutesDefaultRatio = gv.cfg.RouteSCfg().DefaultRatio utils.DecimalContext.MaxScale = gv.cfg.GeneralCfg().DecimalMaxScale utils.DecimalContext.MinScale = gv.cfg.GeneralCfg().DecimalMinScale utils.DecimalContext.Precision = gv.cfg.GeneralCfg().DecimalPrecision @@ -53,6 +54,7 @@ func (gv *GlobalVarS) Start(_ *utils.SyncedChan, _ *servmanager.ServiceRegistry) // Reload handles the change of config func (gv *GlobalVarS) Reload(_ *utils.SyncedChan, _ *servmanager.ServiceRegistry) error { engine.SetHTTPPstrTransport(gv.cfg.HTTPCfg().ClientOpts) + engine.RoutesDefaultRatio = gv.cfg.RouteSCfg().DefaultRatio utils.DecimalContext.MaxScale = gv.cfg.GeneralCfg().DecimalMaxScale utils.DecimalContext.MinScale = gv.cfg.GeneralCfg().DecimalMinScale utils.DecimalContext.Precision = gv.cfg.GeneralCfg().DecimalPrecision