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:
Ryan O'Hara-Reid
2020-02-06 11:44:28 +11:00
committed by GitHub
parent 4625ef9b94
commit 0a84c5d97a
103 changed files with 3906 additions and 2581 deletions

View File

@@ -13,6 +13,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"
"github.com/thrasher-corp/gocryptotrader/exchanges/websocket/wshandler"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
@@ -43,10 +44,6 @@ const (
geminiHeartbeat = "heartbeat"
geminiVolume = "notionalvolume"
// gemini limit rates
geminiAuthRate = 600
geminiUnauthRate = 120
// Too many requests returns this
geminiRateError = "429"
@@ -346,16 +343,14 @@ func (g *Gemini) PostHeartbeat() (string, error) {
// SendHTTPRequest sends an unauthenticated request
func (g *Gemini) SendHTTPRequest(path string, result interface{}) error {
return g.SendPayload(http.MethodGet,
path,
nil,
nil,
result,
false,
false,
g.Verbose,
g.HTTPDebugging,
g.HTTPRecording)
return g.SendPayload(&request.Item{
Method: http.MethodGet,
Path: path,
Result: result,
Verbose: g.Verbose,
HTTPDebugging: g.HTTPDebugging,
HTTPRecording: g.HTTPRecording,
})
}
// SendAuthenticatedHTTPRequest sends an authenticated HTTP request to the
@@ -393,16 +388,18 @@ func (g *Gemini) SendAuthenticatedHTTPRequest(method, path string, params map[st
headers["X-GEMINI-SIGNATURE"] = crypto.HexEncodeToString(hmac)
headers["Cache-Control"] = "no-cache"
return g.SendPayload(method,
g.API.Endpoints.URL+"/v1/"+path,
headers,
nil,
result,
true,
true,
g.Verbose,
g.HTTPDebugging,
g.HTTPRecording)
return g.SendPayload(&request.Item{
Method: method,
Path: g.API.Endpoints.URL + "/v1/" + path,
Headers: headers,
Result: result,
AuthRequest: true,
NonceEnabled: true,
Verbose: g.Verbose,
HTTPDebugging: g.HTTPDebugging,
HTTPRecording: g.HTTPRecording,
Endpoint: request.Auth,
})
}
// GetFee returns an estimate of fee based on type of transaction

View File

@@ -105,9 +105,8 @@ func (g *Gemini) SetDefaults() {
}
g.Requester = request.New(g.Name,
request.NewRateLimit(time.Minute, geminiAuthRate),
request.NewRateLimit(time.Minute, geminiUnauthRate),
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
SetRateLimit())
g.API.Endpoints.URLDefault = geminiAPIURL
g.API.Endpoints.URL = g.API.Endpoints.URLDefault

View File

@@ -0,0 +1,39 @@
package gemini
import (
"time"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"golang.org/x/time/rate"
)
const (
// gemini limit rates
geminiRateInterval = time.Minute
geminiAuthRate = 600
geminiUnauthRate = 120
)
// RateLimit implements the request.Limiter interface
type RateLimit struct {
Auth *rate.Limiter
UnAuth *rate.Limiter
}
// Limit limits the endpoint functionality
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(geminiRateInterval, geminiAuthRate),
UnAuth: request.NewRateLimit(geminiRateInterval, geminiUnauthRate),
}
}