Updated CGREngine structure

This commit is contained in:
Trial97
2021-09-07 19:32:59 +03:00
committed by Dan Christian Bogos
parent 5098d3e65f
commit cea082eb7f
23 changed files with 613 additions and 1101 deletions

View File

@@ -35,6 +35,7 @@ func NewConnManager(cfg *config.CGRConfig, rpcInternal map[string]chan birpc.Cli
cM = &ConnManager{
cfg: cfg,
rpcInternal: rpcInternal,
dynIntCh: NewRPCClientSet(rpcInternal),
connCache: ltcache.NewCache(-1, 0, true, nil),
}
SetConnManager(cM)
@@ -45,6 +46,7 @@ func NewConnManager(cfg *config.CGRConfig, rpcInternal map[string]chan birpc.Cli
type ConnManager struct {
cfg *config.CGRConfig
rpcInternal map[string]chan birpc.ClientConnector
dynIntCh RPCClientSet
connCache *ltcache.Cache
}
@@ -71,7 +73,7 @@ func (cM *ConnManager) getConn(ctx *context.Context, connID string) (conn birpc.
connCfg = cM.cfg.RPCConns()[connID]
for _, rpcConn := range connCfg.Conns {
if rpcConn.Address == utils.MetaInternal {
intChan = IntRPC.GetInternalChanel()
intChan = cM.dynIntCh.GetInternalChanel()
break
}
}
@@ -203,3 +205,13 @@ func (cM *ConnManager) Reload() {
Cache.Clear([]string{utils.CacheReplicationHosts})
cM.connCache.Clear()
}
func (cM *ConnManager) GetInternalChan() chan birpc.ClientConnector {
return cM.dynIntCh.GetInternalChanel()
}
func (cM *ConnManager) AddInternalConn(connName, apiPrefix string,
iConnCh chan birpc.ClientConnector) {
cM.rpcInternal[connName] = iConnCh
cM.dynIntCh[apiPrefix] = iConnCh
}

View File

@@ -91,30 +91,38 @@ func NewRPCConnection(ctx *context.Context, cfg *config.RemoteHost, keyPath, cer
return
}
// IntRPC is the global variable that is used to comunicate with all the subsystems internally
var IntRPC RPCClientSet
// NewRPCClientSet initilalizates the map of connections
func NewRPCClientSet() (s RPCClientSet) {
return make(RPCClientSet)
func NewRPCClientSet(m map[string]chan birpc.ClientConnector) (s RPCClientSet) {
s = make(RPCClientSet)
for k, v := range map[string]string{
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAnalyzer): utils.AnalyzerSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAdminS): utils.AdminSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAttributes): utils.AttributeSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches): utils.CacheSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCDRs): utils.CDRsV1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaChargers): utils.ChargerSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaGuardian): utils.GuardianSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaLoaders): utils.LoaderSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaResources): utils.ResourceSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaSessionS): utils.SessionSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaStats): utils.StatSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaRoutes): utils.RouteSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaThresholds): utils.ThresholdSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaServiceManager): utils.ServiceManagerV1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaConfig): utils.ConfigSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCore): utils.CoreSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaEEs): utils.EeSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaRateS): utils.RateSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaDispatchers): utils.DispatcherSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAccounts): utils.AccountSv1,
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaActions): utils.ActionSv1,
} {
s[v] = m[k]
}
return
}
// RPCClientSet is a RPC ClientConnector for the internal subsystems
type RPCClientSet map[string]*rpcclient.RPCClient
// AddInternalRPCClient creates and adds to the set a new rpc client using the provided configuration
func (s RPCClientSet) AddInternalRPCClient(name string, connChan chan birpc.ClientConnector) {
rpc, err := rpcclient.NewRPCClient(context.Background(), utils.EmptyString, utils.EmptyString, false,
utils.EmptyString, utils.EmptyString, utils.EmptyString,
config.CgrConfig().GeneralCfg().ConnectAttempts, config.CgrConfig().GeneralCfg().Reconnects,
config.CgrConfig().GeneralCfg().ConnectTimeout, config.CgrConfig().GeneralCfg().ReplyTimeout,
rpcclient.InternalRPC, connChan, true, nil)
if err != nil {
utils.Logger.Err(fmt.Sprintf("<%s> Error adding %s to the set: %s", utils.InternalRPCSet, name, err.Error()))
return
}
s[name] = rpc
}
type RPCClientSet map[string]chan birpc.ClientConnector
// GetInternalChanel is used when RPCClientSet is passed as internal connection for RPCPool
func (s RPCClientSet) GetInternalChanel() chan birpc.ClientConnector {
@@ -129,9 +137,21 @@ func (s RPCClientSet) Call(ctx *context.Context, method string, args interface{}
if len(methodSplit) != 2 {
return rpcclient.ErrUnsupporteServiceMethod
}
conn, has := s[methodSplit[0]]
connCh, has := s[methodSplit[0]]
if !has {
return rpcclient.ErrUnsupporteServiceMethod
}
var conn birpc.ClientConnector
ctx2, cancel := context.WithTimeout(ctx, config.CgrConfig().GeneralCfg().ConnectTimeout)
select {
case conn = <-connCh:
connCh <- conn
cancel()
if conn == nil {
return rpcclient.ErrDisconnected
}
case <-ctx2.Done():
return ctx2.Err()
}
return conn.Call(ctx, method, args, reply)
}