Skip to content

Commit

Permalink
Request: Fix http.Client race issue when setting transport layer prox…
Browse files Browse the repository at this point in the history
…y and timeouts (thrasher-corp#885)

* backtester/request: trying to fix panic (WIP)

* request: fix race for transport layer

* request: linter issue fix

* request: more linter issues

* requester: Add function to remove the tracking of underlying http client and add to engine unload exchange.

* request: add more context to error return

* request: Fix after cherry pick issues

* request: fix niterinos

* exchanges: change return to package variable

* request: changed named

Co-authored-by: Ryan O'Hara-Reid <[email protected]>
  • Loading branch information
shazbert and Ryan O'Hara-Reid authored Feb 17, 2022
1 parent 11da520 commit 6127e2a
Show file tree
Hide file tree
Showing 53 changed files with 751 additions and 256 deletions.
13 changes: 10 additions & 3 deletions cmd/apichecker/apicheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,15 +1288,19 @@ func updateFile(name string) error {
// SendGetReq sends get req
func sendGetReq(path string, result interface{}) error {
var requester *request.Requester
var err error
if strings.Contains(path, "github") {
requester = request.New("Apichecker",
requester, err = request.New("Apichecker",
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(request.NewBasicRateLimit(time.Hour, 60)))
} else {
requester = request.New("Apichecker",
requester, err = request.New("Apichecker",
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(request.NewBasicRateLimit(time.Second, 100)))
}
if err != nil {
return err
}
item := &request.Item{
Method: http.MethodGet,
Path: path,
Expand All @@ -1309,9 +1313,12 @@ func sendGetReq(path string, result interface{}) error {

// sendAuthReq sends auth req
func sendAuthReq(method, path string, result interface{}) error {
requester := request.New("Apichecker",
requester, err := request.New("Apichecker",
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(request.NewBasicRateLimit(time.Second*10, 100)))
if err != nil {
return err
}
item := &request.Item{
Method: method,
Path: path,
Expand Down
6 changes: 5 additions & 1 deletion currency/coinmarketcap/coinmarketcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ func (c *Coinmarketcap) SetDefaults() {
c.Verbose = false
c.APIUrl = baseURL
c.APIVersion = version
c.Requester = request.New(c.Name,
var err error
c.Requester, err = request.New(c.Name,
common.NewHTTPClientWithTimeout(defaultTimeOut),
request.WithLimiter(request.NewBasicRateLimit(RateInterval, BasicRequestRate)),
)
if err != nil {
log.Errorln(log.Global, err)
}
}

// Setup sets user configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func (c *CurrencyConverter) Setup(config base.Settings) error {
c.Enabled = config.Enabled
c.Verbose = config.Verbose
c.PrimaryProvider = config.PrimaryProvider
c.Requester = request.New(c.Name,
var err error
c.Requester, err = request.New(c.Name,
common.NewHTTPClientWithTimeout(base.DefaultTimeOut),
request.WithLimiter(request.NewBasicRateLimit(rateInterval, requestRate)))
return nil
return err
}

// GetRates is a wrapper function to return rates
Expand Down
6 changes: 3 additions & 3 deletions currency/forexprovider/currencylayer/currencylayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func (c *CurrencyLayer) Setup(config base.Settings) error {
c.Verbose = config.Verbose
c.PrimaryProvider = config.PrimaryProvider
// Rate limit is based off a monthly counter - Open limit used.
c.Requester = request.New(c.Name,
var err error
c.Requester, err = request.New(c.Name,
common.NewHTTPClientWithTimeout(base.DefaultTimeOut))

return nil
return err
}

// GetRates is a wrapper function to return rates for GoCryptoTrader
Expand Down
5 changes: 3 additions & 2 deletions currency/forexprovider/exchangerate.host/exchangerate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ func (e *ExchangeRateHost) Setup(config base.Settings) error {
e.Enabled = config.Enabled
e.Verbose = config.Verbose
e.PrimaryProvider = config.PrimaryProvider
e.Requester = request.New(e.Name,
var err error
e.Requester, err = request.New(e.Name,
common.NewHTTPClientWithTimeout(base.DefaultTimeOut))
return nil
return err
}

// GetLatestRates returns a list of forex rates based on the supplied params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ func (e *ExchangeRates) Setup(config base.Settings) error {
e.PrimaryProvider = config.PrimaryProvider
e.APIKey = config.APIKey
e.APIKeyLvl = config.APIKeyLvl
e.Requester = request.New(e.Name,
var err error
e.Requester, err = request.New(e.Name,
common.NewHTTPClientWithTimeout(base.DefaultTimeOut),
request.WithLimiter(request.NewBasicRateLimit(rateLimitInterval, requestRate)))
return nil
return err
}

func (e *ExchangeRates) cleanCurrencies(baseCurrency, symbols string) string {
Expand Down
5 changes: 3 additions & 2 deletions currency/forexprovider/fixer.io/fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func (f *Fixer) Setup(config base.Settings) error {
f.Name = config.Name
f.Verbose = config.Verbose
f.PrimaryProvider = config.PrimaryProvider
f.Requester = request.New(f.Name,
var err error
f.Requester, err = request.New(f.Name,
common.NewHTTPClientWithTimeout(base.DefaultTimeOut))
return nil
return err
}

// GetSupportedCurrencies returns supported currencies
Expand Down
5 changes: 3 additions & 2 deletions currency/forexprovider/openexchangerates/openexchangerates.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func (o *OXR) Setup(config base.Settings) error {
o.Name = config.Name
o.Verbose = config.Verbose
o.PrimaryProvider = config.PrimaryProvider
o.Requester = request.New(o.Name,
var err error
o.Requester, err = request.New(o.Name,
common.NewHTTPClientWithTimeout(base.DefaultTimeOut))
return nil
return err
}

// GetRates is a wrapper function to return rates
Expand Down
6 changes: 5 additions & 1 deletion engine/exchange_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ func (m *ExchangeManager) RemoveExchange(exchName string) error {
if m.Len() == 0 {
return ErrNoExchangesLoaded
}
_, err := m.GetExchangeByName(exchName)
exch, err := m.GetExchangeByName(exchName)
if err != nil {
return err
}
m.m.Lock()
defer m.m.Unlock()
err = exch.GetBase().Requester.Shutdown()
if err != nil {
return err
}
delete(m.exchanges, strings.ToLower(exchName))
log.Infof(log.ExchangeSys, "%s exchange unloaded successfully.\n", exchName)
return nil
Expand Down
5 changes: 4 additions & 1 deletion exchanges/alphapoint/alphapoint_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ func (a *Alphapoint) SetDefaults() {
},
}

a.Requester = request.New(a.Name,
a.Requester, err = request.New(a.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
}

// FetchTradablePairs returns a list of the exchanges tradable pairs
Expand Down
5 changes: 4 additions & 1 deletion exchanges/binance/binance_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func TestMain(m *testing.M) {
if err != nil {
log.Fatalf("Mock server error %s", err)
}
b.HTTPClient = newClient
err = b.SetHTTPClient(newClient)
if err != nil {
log.Fatalf("Mock server error %s", err)
}
endpointMap := b.API.Endpoints.GetURLMap()
for k := range endpointMap {
err = b.API.Endpoints.SetRunning(k, serverDetails)
Expand Down
5 changes: 4 additions & 1 deletion exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ func (b *Binance) SetDefaults() {
},
}

b.Requester = request.New(b.Name,
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.API.Endpoints = b.NewEndpoints()
err = b.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: spotAPIURL,
Expand Down
5 changes: 4 additions & 1 deletion exchanges/bitfinex/bitfinex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ func (b *Bitfinex) SetDefaults() {
},
}

b.Requester = request.New(b.Name,
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.API.Endpoints = b.NewEndpoints()
err = b.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: bitfinexAPIURLBase,
Expand Down
5 changes: 4 additions & 1 deletion exchanges/bitflyer/bitflyer_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ func (b *Bitflyer) SetDefaults() {
},
}

b.Requester = request.New(b.Name,
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.API.Endpoints = b.NewEndpoints()
err = b.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: japanURL,
Expand Down
5 changes: 4 additions & 1 deletion exchanges/bithumb/bithumb_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ func (b *Bithumb) SetDefaults() {
},
},
}
b.Requester = request.New(b.Name,
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.API.Endpoints = b.NewEndpoints()
err = b.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: apiURL,
Expand Down
5 changes: 4 additions & 1 deletion exchanges/bitmex/bitmex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,12 @@ func (b *Bitmex) SetDefaults() {
},
}

b.Requester = request.New(b.Name,
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.API.Endpoints = b.NewEndpoints()
err = b.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: bitmexAPIURL,
Expand Down
5 changes: 4 additions & 1 deletion exchanges/bitstamp/bitstamp_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func TestMain(m *testing.M) {
log.Fatalf("Mock server error %s", err)
}

b.HTTPClient = newClient
err = b.SetHTTPClient(newClient)
if err != nil {
log.Fatalf("Mock server error %s", err)
}
endpointMap := b.API.Endpoints.GetURLMap()
for k := range endpointMap {
err = b.API.Endpoints.SetRunning(k, serverDetails+"/api")
Expand Down
5 changes: 4 additions & 1 deletion exchanges/bitstamp/bitstamp_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ func (b *Bitstamp) SetDefaults() {
},
}

b.Requester = request.New(b.Name,
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(request.NewBasicRateLimit(bitstampRateInterval, bitstampRequestRate)))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.API.Endpoints = b.NewEndpoints()
err = b.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: bitstampAPIURL,
Expand Down
5 changes: 4 additions & 1 deletion exchanges/bittrex/bittrex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@ func (b *Bittrex) SetDefaults() {
},
}

b.Requester = request.New(b.Name,
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(request.NewBasicRateLimit(ratePeriod, rateLimit)))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}

b.API.Endpoints = b.NewEndpoints()

Expand Down
5 changes: 4 additions & 1 deletion exchanges/btcmarkets/btcmarkets_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ func (b *BTCMarkets) SetDefaults() {
},
}

b.Requester = request.New(b.Name,
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.API.Endpoints = b.NewEndpoints()
err = b.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: btcMarketsAPIURL,
Expand Down
5 changes: 4 additions & 1 deletion exchanges/btse/btse_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ func (b *BTSE) SetDefaults() {
},
}

b.Requester = request.New(b.Name,
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.API.Endpoints = b.NewEndpoints()
err = b.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: btseAPIURL,
Expand Down
5 changes: 4 additions & 1 deletion exchanges/coinbasepro/coinbasepro_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ func (c *CoinbasePro) SetDefaults() {
},
}

c.Requester = request.New(c.Name,
c.Requester, err = request.New(c.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
c.API.Endpoints = c.NewEndpoints()
err = c.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: coinbaseproAPIURL,
Expand Down
5 changes: 4 additions & 1 deletion exchanges/coinut/coinut_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ func (c *COINUT) SetDefaults() {
},
}

c.Requester = request.New(c.Name,
c.Requester, err = request.New(c.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
c.API.Endpoints = c.NewEndpoints()
err = c.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: coinutAPIURL,
Expand Down
Loading

0 comments on commit 6127e2a

Please sign in to comment.