Centralize RPCClient constructor

This commit is contained in:
ionutboangiu
2024-08-14 17:05:20 +03:00
committed by Dan Christian Bogos
parent 7c5d56c1de
commit 0dfb3b860d
144 changed files with 496 additions and 670 deletions

View File

@@ -19,12 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"errors"
"flag"
"net/rpc"
"net/rpc/jsonrpc"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
@@ -34,14 +30,3 @@ var (
encoding = flag.String("rpc", utils.MetaJSON, "what encoding whould be uused for rpc comunication")
dbType = flag.String("dbtype", utils.MetaInternal, "The type of DataBase (Internal/Mongo/mySql)")
)
func newRPCClient(cfg *config.ListenCfg) (c *rpc.Client, err error) {
switch *encoding {
case utils.MetaJSON:
return jsonrpc.Dial(utils.TCP, cfg.RPCJSONListen)
case utils.MetaGOB:
return rpc.Dial(utils.TCP, cfg.RPCGOBListen)
default:
return nil, errors.New("UNSUPPORTED_RPC")
}
}

View File

@@ -20,9 +20,9 @@ package engine
import (
"bytes"
"errors"
"fmt"
"io"
"net/rpc/jsonrpc"
"os"
"os/exec"
"path"
@@ -30,7 +30,9 @@ import (
"strings"
"time"
"github.com/cgrates/birpc"
"github.com/cgrates/birpc/context"
"github.com/cgrates/birpc/jsonrpc"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
@@ -304,3 +306,14 @@ func GetDefaultEmptyCacheStats() map[string]*ltcache.CacheStats {
utils.CacheReplicationHosts: {},
}
}
func NewRPCClient(cfg *config.ListenCfg, encoding string) (*birpc.Client, error) {
switch encoding {
case utils.MetaJSON:
return jsonrpc.Dial(utils.TCP, cfg.RPCJSONListen)
case utils.MetaGOB:
return birpc.Dial(utils.TCP, cfg.RPCGOBListen)
default:
return nil, errors.New("invalid encoding")
}
}

View File

@@ -92,7 +92,7 @@ func testActionsStartEngine(t *testing.T) {
// Connect rpc client to rater
func testActionsRPCConn(t *testing.T) {
var err error
actsRPC, err = newRPCClient(actsCfg.ListenCfg()) // We connect over JSON so we can also troubleshoot if needed
actsRPC, err = NewRPCClient(actsCfg.ListenCfg(), *encoding) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal(err)
}

View File

@@ -93,7 +93,7 @@ func testActionsitStartEngine(t *testing.T) {
func testActionsitRpcConn(t *testing.T) {
var err error
// time.Sleep(500 * time.Millisecond)
actsLclRpc, err = newRPCClient(actsLclCfg.ListenCfg()) // We connect over JSON so we can also troubleshoot if needed
actsLclRpc, err = NewRPCClient(actsLclCfg.ListenCfg(), *encoding) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal(err)
}