Retrieve routes default ratio from global var

to prevent cyclic import (utils > config > utils)
This commit is contained in:
ionutboangiu
2025-04-15 17:23:57 +03:00
committed by Dan Christian Bogos
parent 7b74552c8f
commit 9a42adc716
7 changed files with 47 additions and 38 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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