Added GlobalVarS as service to manage the reload for the global variables

This commit is contained in:
Trial97
2020-10-16 11:41:07 +03:00
committed by Dan Christian Bogos
parent e0e9190abf
commit 616f7b283c
28 changed files with 599 additions and 390 deletions

View File

@@ -162,9 +162,6 @@ func TestGetSpecialPricedSeconds(t *testing.T) {
}
func TestAccountStorageStore(t *testing.T) {
if DB == "mongo" {
return // mongo will have a problem with null and {} so the Equal will not work
}
b1 := &Balance{Value: 10, Weight: 10,
DestinationIDs: utils.StringMap{"NAT": true}}
b2 := &Balance{Value: 100, Weight: 20, DestinationIDs: utils.StringMap{"RET": true}}

View File

@@ -385,8 +385,7 @@ func callURL(ub *Account, a *Action, acs Actions, extraData interface{}) error {
if err != nil {
return err
}
pstr, err := NewHTTPPoster(config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(),
config.CgrConfig().GeneralCfg().ReplyTimeout, a.ExtraParameters,
pstr, err := NewHTTPPoster(config.CgrConfig().GeneralCfg().ReplyTimeout, a.ExtraParameters,
utils.CONTENT_JSON, config.CgrConfig().GeneralCfg().PosterAttempts)
if err != nil {
return err
@@ -405,8 +404,7 @@ func callURLAsync(ub *Account, a *Action, acs Actions, extraData interface{}) er
if err != nil {
return err
}
pstr, err := NewHTTPPoster(config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(),
config.CgrConfig().GeneralCfg().ReplyTimeout, a.ExtraParameters,
pstr, err := NewHTTPPoster(config.CgrConfig().GeneralCfg().ReplyTimeout, a.ExtraParameters,
utils.CONTENT_JSON, config.CgrConfig().GeneralCfg().PosterAttempts)
if err != nil {
return err
@@ -974,8 +972,7 @@ func postEvent(ub *Account, a *Action, acs Actions, extraData interface{}) error
if err != nil {
return err
}
pstr, err := NewHTTPPoster(config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(),
config.CgrConfig().GeneralCfg().ReplyTimeout, a.ExtraParameters,
pstr, err := NewHTTPPoster(config.CgrConfig().GeneralCfg().ReplyTimeout, a.ExtraParameters,
utils.CONTENT_JSON, config.CgrConfig().GeneralCfg().PosterAttempts)
if err != nil {
return err

View File

@@ -35,42 +35,15 @@ const (
RECURSION_MAX_DEPTH = 3
MIN_PREFIX_MATCH = 1
FALLBACK_SUBJECT = utils.ANY
DB = "map"
)
func init() {
var data DataDB
switch DB {
case "map":
if cgrCfg := config.CgrConfig(); cgrCfg == nil {
cgrCfg, _ = config.NewDefaultCGRConfig()
config.SetCgrConfig(cgrCfg)
}
data = NewInternalDB(nil, nil, true)
}
dm = NewDataManager(data, config.CgrConfig().CacheCfg(), connMgr)
}
var (
dm *DataManager
cdrStorage CdrStorage
debitPeriod = 10 * time.Second
globalRoundingDecimals = 6
connMgr *ConnManager
rpSubjectPrefixMatching bool
rpSubjectPrefixMatchingMutex sync.RWMutex // used to reload rpSubjectPrefixMatching
)
// SetDataStorage is the exported method to set the storage getter.
func SetDataStorage(dm2 *DataManager) {
dm = dm2
}
// SetConnManager is the exported method to set the connectionManager used when operate on an account.
func SetConnManager(conMgr *ConnManager) {
connMgr = conMgr
}
// SetRoundingDecimals sets the global rounding method and decimal precision for GetCost method
func SetRoundingDecimals(rd int) {
globalRoundingDecimals = rd
@@ -91,11 +64,6 @@ func getRpSubjectPrefixMatching() (flag bool) {
return
}
// SetCdrStorage sets the database for CDR storing, used by *cdrlog in first place
func SetCdrStorage(cStorage CdrStorage) {
cdrStorage = cStorage
}
// NewCallDescriptorFromCGREvent converts a CGREvent into CallDescriptor
func NewCallDescriptorFromCGREvent(cgrEv *utils.CGREvent,
timezone string) (cd *CallDescriptor, err error) {

View File

@@ -22,7 +22,6 @@ import (
"encoding/json"
"fmt"
"math"
"net/http"
"strconv"
"strings"
"time"
@@ -334,8 +333,8 @@ func (cdr *CDR) exportFieldValue(cfgCdrFld *config.FCTemplate, filterS *FilterS)
return
}
func (cdr *CDR) formatField(cfgFld *config.FCTemplate, pstrTransport *http.Transport,
groupedCDRs []*CDR, filterS *FilterS) (outVal string, err error) {
func (cdr *CDR) formatField(cfgFld *config.FCTemplate, groupedCDRs []*CDR,
filterS *FilterS) (outVal string, err error) {
switch cfgFld.Type {
case utils.META_FILLER:
outVal, err = cfgFld.Value.ParseValue(utils.EmptyString)
@@ -364,7 +363,7 @@ func (cdr *CDR) formatField(cfgFld *config.FCTemplate, pstrTransport *http.Trans
}
if len(httpAddr) == 0 {
err = fmt.Errorf("Empty http address for field %s type %s", cfgFld.Tag, cfgFld.Type)
} else if outValByte, err = HTTPPostJSON(httpAddr, pstrTransport, jsn); err == nil {
} else if outValByte, err = HTTPPostJSON(httpAddr, jsn); err == nil {
outVal = string(outValByte)
if len(outVal) == 0 && cfgFld.Mandatory {
err = fmt.Errorf("Empty result for http_post field: %s", cfgFld.Tag)
@@ -390,8 +389,8 @@ func (cdr *CDR) formatField(cfgFld *config.FCTemplate, pstrTransport *http.Trans
// AsExportRecord is used in place where we need to export the CDR based on an export template
// ExportRecord is a []string to keep it compatible with encoding/csv Writer
func (cdr *CDR) AsExportRecord(exportFields []*config.FCTemplate,
pstrTransport *http.Transport, groupedCDRs []*CDR, filterS *FilterS) (expRecord []string, err error) {
func (cdr *CDR) AsExportRecord(exportFields []*config.FCTemplate, groupedCDRs []*CDR,
filterS *FilterS) (expRecord []string, err error) {
nM := utils.MapStorage{
utils.MetaReq: cdr.AsMapStringIface(),
utils.MetaEC: cdr.CostDetails,
@@ -407,7 +406,7 @@ func (cdr *CDR) AsExportRecord(exportFields []*config.FCTemplate,
continue
}
var fmtOut string
if fmtOut, err = cdr.formatField(cfgFld, pstrTransport, groupedCDRs, filterS); err != nil {
if fmtOut, err = cdr.formatField(cfgFld, groupedCDRs, filterS); err != nil {
utils.Logger.Warning(fmt.Sprintf("<CDR> error: %s exporting field: %s, CDR: %s\n",
err.Error(), utils.ToJSON(cfgFld), utils.ToJSON(cdr)))
return nil, err

View File

@@ -641,8 +641,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Value: prsr,
Timezone: "UTC",
}
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err != nil {
t.Error(err)
} else if expRecord[0] != cdr.Destination {
t.Errorf("Expecting:\n%s\nReceived:\n%s", cdr.Destination, expRecord)
@@ -661,8 +660,7 @@ func TestCDRAsExportRecord(t *testing.T) {
MaskLen: 3,
}
eDst := "+4986517174***"
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err != nil {
t.Error(err)
} else if expRecord[0] != eDst {
t.Errorf("Expecting:\n%s\nReceived:\n%s", eDst, expRecord[0])
@@ -675,8 +673,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Value: prsr,
MaskDestID: "MASKED_DESTINATIONS",
}
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err != nil {
t.Error(err)
} else if expRecord[0] != "1" {
t.Errorf("Expecting:\n%s\nReceived:\n%s", "1", expRecord[0])
@@ -692,8 +689,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Filters: []string{"*string:~*req.Tenant:itsyscom.com"},
Timezone: "UTC",
}
if rcrd, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, &FilterS{dm: dmForCDR, cfg: defaultCfg}); err != nil {
if rcrd, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, &FilterS{dm: dmForCDR, cfg: defaultCfg}); err != nil {
t.Error(err)
} else if len(rcrd) != 0 {
t.Error("failed using filter")
@@ -710,8 +706,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Layout: layout,
Timezone: "UTC",
}
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, &FilterS{dm: dmForCDR, cfg: defaultCfg}); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, &FilterS{dm: dmForCDR, cfg: defaultCfg}); err != nil {
t.Error(err)
} else if expRecord[0] != "2014-06-11 19:19:00" {
t.Error("Expecting: 2014-06-11 19:19:00, got: ", expRecord[0])
@@ -727,8 +722,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Layout: layout,
Timezone: "UTC",
}
if rcrd, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, &FilterS{dm: dmForCDR, cfg: defaultCfg}); err != nil {
if rcrd, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, &FilterS{dm: dmForCDR, cfg: defaultCfg}); err != nil {
t.Error(err)
} else if len(rcrd) != 0 {
t.Error("failed using filter")
@@ -743,8 +737,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Layout: layout,
Timezone: "UTC"}
// Test time parse error
if _, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err == nil {
if _, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err == nil {
t.Error("Should give error here, got none.")
}
@@ -755,8 +748,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Path: "*exp.CGRIDFromCostDetails",
Value: prsr,
}
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err != nil {
t.Error(err)
} else if expRecord[0] != cdr.CostDetails.CGRID {
t.Errorf("Expecting:\n%s\nReceived:\n%s", cdr.CostDetails.CGRID, expRecord)
@@ -768,8 +760,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Path: "*exp.CustomAccountID",
Value: prsr,
}
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err != nil {
t.Error(err)
} else if expRecord[0] != cdr.CostDetails.AccountSummary.ID {
t.Errorf("Expecting:\n%s\nReceived:\n%s", cdr.CostDetails.AccountSummary.ID, expRecord)
@@ -783,8 +774,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Path: "*exp.CustomDestinationID",
Value: prsr,
}
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err != nil {
t.Error(err)
} else if expRecord[0] != expected {
t.Errorf("Expecting: <%q>,\n Received: <%q>", expected, expRecord[0])
@@ -798,8 +788,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Path: "*exp.CustomDestinationID",
Value: prsr,
}
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err != nil {
t.Error(err)
} else if expRecord[0] != expected {
t.Errorf("Expecting: <%q>,\n Received: <%q>", expected, expRecord[0])
@@ -813,8 +802,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Path: "*exp.CustomDestinationID",
Value: prsr,
}
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err != nil {
t.Error(err)
} else if expRecord[0] != expected {
t.Errorf("Expecting: <%q>,\n Received: <%q>", expected, expRecord[0])
@@ -828,8 +816,7 @@ func TestCDRAsExportRecord(t *testing.T) {
Path: "*exp.CustomDestinationID",
Value: prsr,
}
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld},
config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), nil, nil); err != nil {
if expRecord, err := cdr.AsExportRecord([]*config.FCTemplate{cfgCdrFld}, nil, nil); err != nil {
t.Error(err)
} else if expRecord[0] != expected {
t.Errorf("Expecting: <%q>,\n Received: <%q>", expected, expRecord[0])
@@ -887,7 +874,7 @@ func TestCDRAsCDRsql(t *testing.T) {
func TestCDRNewCDRFromSQL(t *testing.T) {
extraFields := map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}
cdrSql := &CDRsql{
cdrSQL := &CDRsql{
ID: 123,
Cgrid: "abecd993d06672714c4218a6dcf8278e0589a171",
RunID: utils.MetaDefault,
@@ -928,7 +915,7 @@ func TestCDRNewCDRFromSQL(t *testing.T) {
ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
}
if eCDR, err := NewCDRFromSQL(cdrSql); err != nil {
if eCDR, err := NewCDRFromSQL(cdrSQL); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(cdr, eCDR) {
t.Errorf("Expecting: %+v, received: %+v", cdr, eCDR)

View File

@@ -150,9 +150,8 @@ func (dH *DispatcherHost) TenantID() string {
}
// GetRPCConnection builds or returns the cached connection
func (dH *DispatcherHost) Call(serviceMethod string, args interface{}, reply interface{}) error {
func (dH *DispatcherHost) Call(serviceMethod string, args interface{}, reply interface{}) (err error) {
if dH.rpcConn == nil {
var err error
// connect the rpcConn
cfg := config.CgrConfig()
if dH.rpcConn, err = NewRPCPool(
@@ -162,7 +161,7 @@ func (dH *DispatcherHost) Call(serviceMethod string, args interface{}, reply int
cfg.GeneralCfg().ConnectAttempts, cfg.GeneralCfg().Reconnects,
cfg.GeneralCfg().ConnectTimeout, cfg.GeneralCfg().ReplyTimeout,
dH.Conns, IntRPC.GetInternalChanel(), false); err != nil {
return err
return
}
}

179
engine/globalvars.go Normal file
View File

@@ -0,0 +1,179 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"crypto/tls"
"net"
"net/http"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
// this file will contain all the global variable that are used by other subsystems
var (
httpPstrTransport *http.Transport
dm *DataManager
cdrStorage CdrStorage
connMgr *ConnManager
)
func init() {
dm = NewDataManager(NewInternalDB(nil, nil, true), config.CgrConfig().CacheCfg(), connMgr)
httpPstrTransport, _ = NewHTTPTransport(config.CgrConfig().HTTPCfg().ClientOpts)
}
// SetDataStorage is the exported method to set the storage getter.
func SetDataStorage(dm2 *DataManager) {
dm = dm2
}
// SetConnManager is the exported method to set the connectionManager used when operate on an account.
func SetConnManager(conMgr *ConnManager) {
connMgr = conMgr
}
// SetCdrStorage sets the database for CDR storing, used by *cdrlog in first place
func SetCdrStorage(cStorage CdrStorage) {
cdrStorage = cStorage
}
// SetHTTPPstrTransport sets the http transport to be used by the HTTP Poster
func SetHTTPPstrTransport(pstrTransport *http.Transport) {
httpPstrTransport = pstrTransport
}
// NewHTTPTransport will create a new transport for HTTP client
func NewHTTPTransport(opts map[string]interface{}) (trsp *http.Transport, err error) {
trsp = &http.Transport{
Proxy: http.ProxyFromEnvironment,
}
if val, has := opts[utils.HTTPClientTLSClientConfigCfg]; has {
var skipTLSVerify bool
if skipTLSVerify, err = utils.IfaceAsBool(val); err != nil {
return
}
trsp.TLSClientConfig = &tls.Config{InsecureSkipVerify: skipTLSVerify}
}
if val, has := opts[utils.HTTPClientTLSHandshakeTimeoutCfg]; has {
var tlsHndTimeout time.Duration
if tlsHndTimeout, err = utils.IfaceAsDuration(val); err != nil {
return
}
trsp.TLSHandshakeTimeout = tlsHndTimeout
}
if val, has := opts[utils.HTTPClientDisableKeepAlivesCfg]; has {
var disKeepAlives bool
if disKeepAlives, err = utils.IfaceAsBool(val); err != nil {
return
}
trsp.DisableKeepAlives = disKeepAlives
}
if val, has := opts[utils.HTTPClientDisableCompressionCfg]; has {
var disCmp bool
if disCmp, err = utils.IfaceAsBool(val); err != nil {
return
}
trsp.DisableCompression = disCmp
}
if val, has := opts[utils.HTTPClientMaxIdleConnsCfg]; has {
var maxIdleConns int64
if maxIdleConns, err = utils.IfaceAsTInt64(val); err != nil {
return
}
trsp.MaxIdleConns = int(maxIdleConns)
}
if val, has := opts[utils.HTTPClientMaxIdleConnsPerHostCfg]; has {
var maxIdleConns int64
if maxIdleConns, err = utils.IfaceAsTInt64(val); err != nil {
return
}
trsp.MaxIdleConnsPerHost = int(maxIdleConns)
}
if val, has := opts[utils.HTTPClientMaxConnsPerHostCfg]; has {
var maxConns int64
if maxConns, err = utils.IfaceAsTInt64(val); err != nil {
return
}
trsp.MaxConnsPerHost = int(maxConns)
}
if val, has := opts[utils.HTTPClientIdleConnTimeoutCfg]; has {
var idleTimeout time.Duration
if idleTimeout, err = utils.IfaceAsDuration(val); err != nil {
return
}
trsp.IdleConnTimeout = idleTimeout
}
if val, has := opts[utils.HTTPClientResponseHeaderTimeoutCfg]; has {
var responseTimeout time.Duration
if responseTimeout, err = utils.IfaceAsDuration(val); err != nil {
return
}
trsp.ResponseHeaderTimeout = responseTimeout
}
if val, has := opts[utils.HTTPClientExpectContinueTimeoutCfg]; has {
var continueTimeout time.Duration
if continueTimeout, err = utils.IfaceAsDuration(val); err != nil {
return
}
trsp.ExpectContinueTimeout = continueTimeout
}
if val, has := opts[utils.HTTPClientForceAttemptHTTP2Cfg]; has {
var forceHTTP2 bool
if forceHTTP2, err = utils.IfaceAsBool(val); err != nil {
return
}
trsp.ForceAttemptHTTP2 = forceHTTP2
}
var dial *net.Dialer
if dial, err = newDialer(opts); err != nil {
return
}
trsp.DialContext = dial.DialContext
return
}
// newDialer returns the objects that creates the DialContext function
func newDialer(opts map[string]interface{}) (dial *net.Dialer, err error) {
dial = &net.Dialer{
DualStack: true,
}
if val, has := opts[utils.HTTPClientDialTimeoutCfg]; has {
var timeout time.Duration
if timeout, err = utils.IfaceAsDuration(val); err != nil {
return
}
dial.Timeout = timeout
}
if val, has := opts[utils.HTTPClientDialFallbackDelayCfg]; has {
var fallDelay time.Duration
if fallDelay, err = utils.IfaceAsDuration(val); err != nil {
return
}
dial.FallbackDelay = fallDelay
}
if val, has := opts[utils.HTTPClientDialKeepAliveCfg]; has {
var keepAlive time.Duration
if keepAlive, err = utils.IfaceAsDuration(val); err != nil {
return
}
dial.KeepAlive = keepAlive
}
return
}

172
engine/globalvars_test.go Normal file
View File

@@ -0,0 +1,172 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"crypto/tls"
"net"
"net/http"
"testing"
"time"
"github.com/cgrates/cgrates/utils"
)
func TestNewHTTPTransport(t *testing.T) {
opts := map[string]interface{}{
utils.HTTPClientTLSClientConfigCfg: false,
utils.HTTPClientTLSHandshakeTimeoutCfg: "10s",
utils.HTTPClientDisableKeepAlivesCfg: false,
utils.HTTPClientDisableCompressionCfg: false,
utils.HTTPClientMaxIdleConnsCfg: 100.,
utils.HTTPClientMaxIdleConnsPerHostCfg: 2.,
utils.HTTPClientMaxConnsPerHostCfg: 0.,
utils.HTTPClientIdleConnTimeoutCfg: "90s",
utils.HTTPClientResponseHeaderTimeoutCfg: "0",
utils.HTTPClientExpectContinueTimeoutCfg: "0",
utils.HTTPClientForceAttemptHTTP2Cfg: true,
utils.HTTPClientDialTimeoutCfg: "30s",
utils.HTTPClientDialFallbackDelayCfg: "300ms",
utils.HTTPClientDialKeepAliveCfg: "30s",
}
expDialer := &net.Dialer{
DualStack: true,
Timeout: 30 * time.Second,
FallbackDelay: 300 * time.Millisecond,
KeepAlive: 30 * time.Second,
}
if dial, err := newDialer(opts); err != nil {
t.Fatal(err)
} else if !(expDialer != nil && dial != nil &&
expDialer.DualStack == dial.DualStack &&
expDialer.Timeout == dial.Timeout &&
expDialer.FallbackDelay == dial.FallbackDelay &&
expDialer.KeepAlive == dial.KeepAlive) {
t.Errorf("Expected %+v, received %+v", expDialer, dial)
}
expTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
MaxConnsPerHost: 0,
IdleConnTimeout: 90 * time.Second,
ForceAttemptHTTP2: true,
}
if trsp, err := NewHTTPTransport(opts); err != nil {
t.Fatal(err)
} else if !(expTransport != nil && trsp != nil && // the dial options are not included
expTransport.TLSClientConfig.InsecureSkipVerify == trsp.TLSClientConfig.InsecureSkipVerify &&
expTransport.TLSHandshakeTimeout == trsp.TLSHandshakeTimeout &&
expTransport.DisableKeepAlives == trsp.DisableKeepAlives &&
expTransport.DisableCompression == trsp.DisableCompression &&
expTransport.MaxIdleConns == trsp.MaxIdleConns &&
expTransport.MaxIdleConnsPerHost == trsp.MaxIdleConnsPerHost &&
expTransport.MaxConnsPerHost == trsp.MaxConnsPerHost &&
expTransport.IdleConnTimeout == trsp.IdleConnTimeout &&
expTransport.ResponseHeaderTimeout == trsp.ResponseHeaderTimeout &&
expTransport.ExpectContinueTimeout == trsp.ExpectContinueTimeout &&
expTransport.ForceAttemptHTTP2 == trsp.ForceAttemptHTTP2) {
t.Errorf("Expected %+v, received %+v", expTransport, trsp)
}
opts[utils.HTTPClientDialKeepAliveCfg] = "30as"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientDialFallbackDelayCfg] = "300ams"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientDialTimeoutCfg] = "30as"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientForceAttemptHTTP2Cfg] = "string"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientExpectContinueTimeoutCfg] = "0a"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientResponseHeaderTimeoutCfg] = "0a"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientIdleConnTimeoutCfg] = "90as"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientMaxConnsPerHostCfg] = "not a number"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientMaxIdleConnsPerHostCfg] = "not a number"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientMaxIdleConnsCfg] = "not a number"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientDisableCompressionCfg] = "string"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientDisableKeepAlivesCfg] = "string"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientTLSHandshakeTimeoutCfg] = "10as"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
opts[utils.HTTPClientTLSClientConfigCfg] = "string"
if _, err := NewHTTPTransport(opts); err == nil {
t.Error("Expected error but the transport was builded succesfully")
}
}
func TestSetHTTPPstrTransport(t *testing.T) {
tmp := httpPstrTransport
SetHTTPPstrTransport(nil)
if httpPstrTransport != nil {
t.Error("Expected the transport to be nil", httpPstrTransport)
}
httpPstrTransport = tmp
}
func TestSetCdrStorage(t *testing.T) {
tmp := cdrStorage
SetCdrStorage(nil)
if cdrStorage != nil {
t.Error("Expected the cdrStorage to be nil", cdrStorage)
}
cdrStorage = tmp
}
func TestSetDataStorage(t *testing.T) {
tmp := dm
SetDataStorage(nil)
if dm != nil {
t.Error("Expected the dm to be nil", dm)
}
dm = tmp
}

View File

@@ -157,8 +157,7 @@ func (expEv *ExportEvents) ReplayFailedPosts(attempts int) (failedEvents *Export
switch expEv.Format {
case utils.MetaHTTPjsonCDR, utils.MetaHTTPjsonMap, utils.MetaHTTPjson, utils.MetaHTTPPost:
var pstr *HTTPPoster
pstr, err = NewHTTPPoster(config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(),
config.CgrConfig().GeneralCfg().ReplyTimeout, expEv.Path,
pstr, err = NewHTTPPoster(config.CgrConfig().GeneralCfg().ReplyTimeout, expEv.Path,
utils.PosterTransportContentTypes[expEv.Format],
config.CgrConfig().GeneralCfg().PosterAttempts)
if err != nil {

View File

@@ -30,8 +30,8 @@ import (
)
// HTTPPostJSON posts without automatic failover
func HTTPPostJSON(url string, posterTransport *http.Transport, content []byte) (respBody []byte, err error) {
client := &http.Client{Transport: posterTransport}
func HTTPPostJSON(url string, content []byte) (respBody []byte, err error) {
client := &http.Client{Transport: httpPstrTransport}
var resp *http.Response
if resp, err = client.Post(url, "application/json", bytes.NewBuffer(content)); err != nil {
return
@@ -48,13 +48,13 @@ func HTTPPostJSON(url string, posterTransport *http.Transport, content []byte) (
}
// NewHTTPPoster return a new HTTP poster
func NewHTTPPoster(posterTransport *http.Transport, replyTimeout time.Duration,
addr, contentType string, attempts int) (httposter *HTTPPoster, err error) {
func NewHTTPPoster(replyTimeout time.Duration, addr, contentType string,
attempts int) (httposter *HTTPPoster, err error) {
if !utils.SliceHasMember([]string{utils.CONTENT_FORM, utils.CONTENT_JSON, utils.CONTENT_TEXT}, contentType) {
return nil, fmt.Errorf("unsupported ContentType: %s", contentType)
}
return &HTTPPoster{
httpClient: &http.Client{Transport: posterTransport, Timeout: replyTimeout},
httpClient: &http.Client{Transport: httpPstrTransport, Timeout: replyTimeout},
addr: addr,
contentType: contentType,
attempts: attempts,

View File

@@ -182,7 +182,7 @@ func SureTaxProcessCdr(cdr *CDR) error {
}
if sureTaxClient == nil { // First time used, init the client here
sureTaxClient = &http.Client{
Transport: config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(),
Transport: httpPstrTransport,
}
}
req, err := NewSureTaxRequest(cdr, stCfg)

View File

@@ -32,10 +32,6 @@ import (
"github.com/cgrates/cgrates/utils"
)
// var (
// TPExportFormats = []string{utils.CSV}
// )
func NewTPExporter(storDb LoadStorage, tpID, expPath, fileFormat, sep string, compress bool) (*TPExporter, error) {
if len(tpID) == 0 {
return nil, errors.New("Missing TPid")

View File

@@ -24,7 +24,6 @@ import (
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
@@ -39,7 +38,7 @@ func TestHttpJsonPost(t *testing.T) {
Usage: "0.00000001", ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
}
jsn, _ := json.Marshal(cdrOut)
if _, err := HTTPPostJSON("http://localhost:8000", config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), jsn); err == nil {
if _, err := HTTPPostJSON("http://localhost:8000", jsn); err == nil {
t.Error(err)
}
}

View File

@@ -67,7 +67,7 @@ func TestHttpJsonPoster(t *testing.T) {
config.CgrConfig().GeneralCfg().FailedPostsDir = "/tmp"
content := &TestContent{Var1: "Val1", Var2: "Val2"}
jsn, _ := json.Marshal(content)
pstr, err := NewHTTPPoster(config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), time.Duration(2*time.Second), "http://localhost:8080/invalid", utils.CONTENT_JSON, 3)
pstr, err := NewHTTPPoster(time.Duration(2*time.Second), "http://localhost:8080/invalid", utils.CONTENT_JSON, 3)
if err != nil {
t.Error(err)
}
@@ -100,7 +100,7 @@ func TestHttpBytesPoster(t *testing.T) {
content := []byte(`Test
Test2
`)
pstr, err := NewHTTPPoster(config.CgrConfig().HTTPCfg().GetDefaultHTTPTransort(), time.Duration(2*time.Second), "http://localhost:8080/invalid", utils.CONTENT_TEXT, 3)
pstr, err := NewHTTPPoster(time.Duration(2*time.Second), "http://localhost:8080/invalid", utils.CONTENT_TEXT, 3)
if err != nil {
t.Error(err)
}