From d3fb55fedb0b50175903e6cc1a847d706691a474 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Wed, 25 May 2022 16:47:11 +0300 Subject: [PATCH] Add max_reconnect_interval option for rpc conns --- config/config_defaults.go | 1 + config/libconfig_json.go | 25 ++++++++--------- config/rpcconn.go | 56 ++++++++++++++++++++++++--------------- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/config/config_defaults.go b/config/config_defaults.go index 20b4c6b30..9313562f6 100644 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -72,6 +72,7 @@ const CGRATES_CFG_JSON = ` //"transport":"*json", //"connect_attempts": 5, //"reconnects": -1, + //"max_reconnect_interval": "" //"connect_timeout":"1s", //"reply_timeout":"2s", //"tls":false, diff --git a/config/libconfig_json.go b/config/libconfig_json.go index 9075b0de6..abbd7726a 100644 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -401,18 +401,19 @@ type RPCConnsJson struct { // Represents one connection instance towards a rater/cdrs server type RemoteHostJson struct { - Id *string - Address *string - Transport *string - Synchronous *bool - Tls *bool - Key_path *string - Cert_path *string - Ca_path *string - Conn_attempts *int - Reconnects *int - Connect_timeout *string - Reply_timeout *string + Id *string + Address *string + Transport *string + Synchronous *bool + Tls *bool + Key_path *string + Cert_path *string + Ca_path *string + Conn_attempts *int + Reconnects *int + MaxReconnectInterval *string + Connect_timeout *string + Reply_timeout *string } type AstConnJsonCfg struct { diff --git a/config/rpcconn.go b/config/rpcconn.go index 1cbaa7fba..a5aebc1d8 100644 --- a/config/rpcconn.go +++ b/config/rpcconn.go @@ -119,17 +119,18 @@ func (rC RPCConn) Clone() (cln *RPCConn) { // RemoteHost connection config type RemoteHost struct { - ID string - Address string - Transport string - ConnectAttempts int - Reconnects int - ConnectTimeout time.Duration - ReplyTimeout time.Duration - TLS bool - ClientKey string - ClientCertificate string - CaCertificate string + ID string + Address string + Transport string + ConnectAttempts int + Reconnects int + MaxReconnectInterval time.Duration + ConnectTimeout time.Duration + ReplyTimeout time.Duration + TLS bool + ClientKey string + ClientCertificate string + CaCertificate string } func (rh *RemoteHost) loadFromJSONCfg(jsnCfg *RemoteHostJson) (err error) { @@ -166,6 +167,11 @@ func (rh *RemoteHost) loadFromJSONCfg(jsnCfg *RemoteHostJson) (err error) { if jsnCfg.Reconnects != nil { rh.Reconnects = *jsnCfg.Reconnects } + if jsnCfg.MaxReconnectInterval != nil { + if rh.MaxReconnectInterval, err = utils.ParseDurationWithNanosecs(*jsnCfg.MaxReconnectInterval); err != nil { + return err + } + } if jsnCfg.Connect_timeout != nil { if rh.ConnectTimeout, err = utils.ParseDurationWithNanosecs(*jsnCfg.Connect_timeout); err != nil { return err @@ -206,6 +212,9 @@ func (rh *RemoteHost) AsMapInterface() (mp map[string]interface{}) { if rh.Reconnects != 0 { mp[utils.ReconnectsCfg] = rh.Reconnects } + if rh.MaxReconnectInterval != 0 { + mp[utils.MaxReconnectIntervalCfg] = rh.MaxReconnectInterval + } if rh.ConnectTimeout != 0 { mp[utils.ConnectTimeoutCfg] = rh.ConnectTimeout } @@ -218,17 +227,18 @@ func (rh *RemoteHost) AsMapInterface() (mp map[string]interface{}) { // Clone returns a deep copy of RemoteHost func (rh RemoteHost) Clone() (cln *RemoteHost) { return &RemoteHost{ - ID: rh.ID, - Address: rh.Address, - Transport: rh.Transport, - ConnectAttempts: rh.ConnectAttempts, - Reconnects: rh.Reconnects, - ConnectTimeout: rh.ConnectTimeout, - ReplyTimeout: rh.ReplyTimeout, - TLS: rh.TLS, - ClientKey: rh.ClientKey, - ClientCertificate: rh.ClientCertificate, - CaCertificate: rh.CaCertificate, + ID: rh.ID, + Address: rh.Address, + Transport: rh.Transport, + ConnectAttempts: rh.ConnectAttempts, + Reconnects: rh.Reconnects, + MaxReconnectInterval: rh.MaxReconnectInterval, + ConnectTimeout: rh.ConnectTimeout, + ReplyTimeout: rh.ReplyTimeout, + TLS: rh.TLS, + ClientKey: rh.ClientKey, + ClientCertificate: rh.ClientCertificate, + CaCertificate: rh.CaCertificate, } } @@ -247,6 +257,7 @@ func UpdateRPCCons(rpcConns RPCConns, newHosts map[string]*RemoteHost) (connIDs rh.Transport = newHost.Transport rh.ConnectAttempts = newHost.ConnectAttempts rh.Reconnects = newHost.Reconnects + rh.MaxReconnectInterval = newHost.MaxReconnectInterval rh.ConnectTimeout = newHost.ConnectTimeout rh.ReplyTimeout = newHost.ReplyTimeout rh.TLS = newHost.TLS @@ -272,6 +283,7 @@ func RemoveRPCCons(rpcConns RPCConns, hosts utils.StringSet) (connIDs utils.Stri rh.Transport = "" rh.ConnectAttempts = 0 rh.Reconnects = 0 + rh.MaxReconnectInterval = 0 rh.ConnectTimeout = 0 rh.ReplyTimeout = 0 rh.TLS = false