From 8b2b5dece0bc128d840ffd181d0b823feae3885a Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 28 Mar 2019 15:33:28 +0100 Subject: [PATCH] DispatcherHost.GetRPCConnection --- engine/dispatcherprfl.go | 35 ++++++++++++++++++++++++++++++----- engine/model_helpers.go | 4 ++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/engine/dispatcherprfl.go b/engine/dispatcherprfl.go index c1a055a54..4ec8bf62a 100644 --- a/engine/dispatcherprfl.go +++ b/engine/dispatcherprfl.go @@ -21,8 +21,11 @@ package engine import ( "math/rand" "sort" + "time" + "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" + "github.com/cgrates/rpcclient" ) type DispatcherConn struct { @@ -123,17 +126,39 @@ func (dps DispatcherProfiles) Sort() { sort.Slice(dps, func(i, j int) bool { return dps[i].Weight > dps[j].Weight }) } +<<<<<<< HEAD type DispatcherHostConn struct { Address string Transport string TLS bool } +======= +// DispatcherHost represents one virtual host with po +>>>>>>> DispatcherHost.GetRPCConnection type DispatcherHost struct { - Tenant string - ID string - Conns []*DispatcherHostConn + Tenant string + ID string + Conns []*config.HaPoolConfig + rpcConn rpcclient.RpcClientConnection } -func (dP *DispatcherHost) TenantID() string { - return utils.ConcatenatedKey(dP.Tenant, dP.ID) +func (dH *DispatcherHost) TenantID() string { + return utils.ConcatenatedKey(dH.Tenant, dH.ID) +} + +// GetRPCConnection builds or returns the cached connection +func (dH *DispatcherHost) GetRPCConnection() (rpcConn rpcclient.RpcClientConnection, err error) { + if dH.rpcConn == nil { + cfg := config.CgrConfig() + if dH.rpcConn, err = NewRPCPool( + rpcclient.POOL_FIRST, + cfg.TlsCfg().ClientKey, + cfg.TlsCfg().ClientCerificate, cfg.TlsCfg().CaCertificate, + cfg.GeneralCfg().ConnectAttempts, cfg.GeneralCfg().Reconnects, + cfg.GeneralCfg().ConnectTimeout, cfg.GeneralCfg().ReplyTimeout, + dH.Conns, nil, time.Duration(0), false); err != nil { + return + } + } + return dH.rpcConn, nil } diff --git a/engine/model_helpers.go b/engine/model_helpers.go index 0bc4de3dc..d4cc57a17 100644 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -2661,10 +2661,10 @@ func APItoDispatcherHost(tpDPH *utils.TPDispatcherHost) (dpp *DispatcherHost) { dpp = &DispatcherHost{ Tenant: tpDPH.Tenant, ID: tpDPH.ID, - Conns: make([]*DispatcherHostConn, len(tpDPH.Conns)), + Conns: make([]*config.HaPoolConfig, len(tpDPH.Conns)), } for i, conn := range tpDPH.Conns { - dpp.Conns[i] = &DispatcherHostConn{ + dpp.Conns[i] = &config.HaPoolConfig{ Address: conn.Address, Transport: conn.Transport, TLS: conn.TLS,