mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-03 15:10:49 +00:00
Request package update & rate limit system expansion (#413)
* Initial rework of rework of requester - WIP * Implementing and checking rate limits - WIP * implemented coinbene rate limiting shenanigans * add in remaining WIP * fixy * use authenticated rate limit * drop ceiling as this can be done with a counter later * add functionality to struct * purge config options for rate limiting so as to keep things minimal * prepare futures and swap rate limiting for implementation * Address linter issues * Addressed nits, fixed race * fix linter issue * remove global var as this was only setting when newrequester was called * moved rate limit functionality into its own file * Update Bitfinex with correct rate limit and test endpoints (WIP) * finish off bitfinex adjustments * fixes * fix linter issues * slowed rate for coinbasepro * drop rate limit for huobi as the doc times have intermittent 429 issues. * Set MACOSX_DEPLOYMENT_TARGET to remove linking warning * Addr Thrasher nits * Addr glorious nits * unexport do request function * fixed nitorinos * Fixed something I missed * move disabled rate limiter into loadexchange and use interface functionality * Add temp quick fix
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/common/crypto"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -22,16 +23,10 @@ const (
|
||||
|
||||
noError = "0000"
|
||||
|
||||
// Public API
|
||||
requestsPerSecondPublicAPI = 20
|
||||
|
||||
publicTicker = "/public/ticker/"
|
||||
publicOrderBook = "/public/orderbook/"
|
||||
publicTransactionHistory = "/public/transaction_history/"
|
||||
|
||||
// Private API
|
||||
requestsPerSecondPrivateAPI = 10
|
||||
|
||||
privateAccInfo = "/info/account"
|
||||
privateAccBalance = "/info/balance"
|
||||
privateWalletAdd = "/info/wallet_address"
|
||||
@@ -46,9 +41,6 @@ const (
|
||||
privateKRWWithdraw = "/trade/krw_withdrawal"
|
||||
privateMarketBuy = "/trade/market_buy"
|
||||
privateMarketSell = "/trade/market_sell"
|
||||
|
||||
bithumbAuthRate = 10
|
||||
bithumbUnauthRate = 20
|
||||
)
|
||||
|
||||
// Bithumb is the overarching type across the Bithumb package
|
||||
@@ -465,16 +457,14 @@ func (b *Bithumb) MarketSellOrder(currency string, units float64) (MarketSell, e
|
||||
|
||||
// SendHTTPRequest sends an unauthenticated HTTP request
|
||||
func (b *Bithumb) SendHTTPRequest(path string, result interface{}) error {
|
||||
return b.SendPayload(http.MethodGet,
|
||||
path,
|
||||
nil,
|
||||
nil,
|
||||
result,
|
||||
false,
|
||||
false,
|
||||
b.Verbose,
|
||||
b.HTTPDebugging,
|
||||
b.HTTPRecording)
|
||||
return b.SendPayload(&request.Item{
|
||||
Method: http.MethodGet,
|
||||
Path: path,
|
||||
Result: result,
|
||||
Verbose: b.Verbose,
|
||||
HTTPDebugging: b.HTTPDebugging,
|
||||
HTTPRecording: b.HTTPRecording,
|
||||
})
|
||||
}
|
||||
|
||||
// SendAuthenticatedHTTPRequest sends an authenticated HTTP request to bithumb
|
||||
@@ -510,16 +500,18 @@ func (b *Bithumb) SendAuthenticatedHTTPRequest(path string, params url.Values, r
|
||||
Message string `json:"message"`
|
||||
}{}
|
||||
|
||||
err := b.SendPayload(http.MethodPost,
|
||||
b.API.Endpoints.URL+path,
|
||||
headers,
|
||||
bytes.NewBufferString(payload),
|
||||
&intermediary,
|
||||
true,
|
||||
true,
|
||||
b.Verbose,
|
||||
b.HTTPDebugging,
|
||||
b.HTTPRecording)
|
||||
err := b.SendPayload(&request.Item{
|
||||
Method: http.MethodPost,
|
||||
Path: b.API.Endpoints.URL + path,
|
||||
Headers: headers,
|
||||
Body: bytes.NewBufferString(payload),
|
||||
Result: &intermediary,
|
||||
AuthRequest: true,
|
||||
NonceEnabled: true,
|
||||
Verbose: b.Verbose,
|
||||
HTTPDebugging: b.HTTPDebugging,
|
||||
HTTPRecording: b.HTTPRecording,
|
||||
Endpoint: request.Auth})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -104,9 +104,8 @@ func (b *Bithumb) SetDefaults() {
|
||||
}
|
||||
|
||||
b.Requester = request.New(b.Name,
|
||||
request.NewRateLimit(time.Second, bithumbAuthRate),
|
||||
request.NewRateLimit(time.Second, bithumbUnauthRate),
|
||||
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
|
||||
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
|
||||
SetRateLimit())
|
||||
|
||||
b.API.Endpoints.URLDefault = apiURL
|
||||
b.API.Endpoints.URL = b.API.Endpoints.URLDefault
|
||||
|
||||
39
exchanges/bithumb/ratelimit.go
Normal file
39
exchanges/bithumb/ratelimit.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package bithumb
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
// Exchange specific rate limit consts
|
||||
const (
|
||||
bithumbRateInterval = time.Second
|
||||
bithumbAuthRate = 95
|
||||
bithumbUnauthRate = 95
|
||||
)
|
||||
|
||||
// RateLimit implements the request.Limiter interface
|
||||
type RateLimit struct {
|
||||
Auth *rate.Limiter
|
||||
UnAuth *rate.Limiter
|
||||
}
|
||||
|
||||
// Limit limits requests
|
||||
func (r *RateLimit) Limit(f request.EndpointLimit) error {
|
||||
if f == request.Auth {
|
||||
time.Sleep(r.Auth.Reserve().Delay())
|
||||
return nil
|
||||
}
|
||||
time.Sleep(r.UnAuth.Reserve().Delay())
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetRateLimit returns the rate limit for the exchange
|
||||
func SetRateLimit() *RateLimit {
|
||||
return &RateLimit{
|
||||
Auth: request.NewRateLimit(bithumbRateInterval, bithumbAuthRate),
|
||||
UnAuth: request.NewRateLimit(bithumbRateInterval, bithumbUnauthRate),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user