mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-08 15:11:07 +00:00
exchanges/request: abstract and consolidate rate limiting code to request package (#1477)
* initial consolidation of rate limiting code to request package to reduce bespoke code implementation * continued * finish abstraction * lint * exchanges: fix tests * linter: fix * poloniex: fix auth rate limit not being set * ratelimiter: convert from token to weight * glorious: nits addressed with fire * linter: rip * change func name set -> get * fix test * derbit: impl --------- Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
This commit is contained in:
@@ -402,7 +402,7 @@ func (g *Gemini) SendHTTPRequest(ctx context.Context, ep exchange.URL, path stri
|
||||
HTTPRecording: g.HTTPRecording,
|
||||
}
|
||||
|
||||
return g.SendPayload(ctx, request.Unset, func() (*request.Item, error) {
|
||||
return g.SendPayload(ctx, request.UnAuth, func() (*request.Item, error) {
|
||||
return item, nil
|
||||
}, request.UnauthenticatedRequest)
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ func (g *Gemini) SetDefaults() {
|
||||
|
||||
g.Requester, err = request.New(g.Name,
|
||||
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
|
||||
request.WithLimiter(SetRateLimit()))
|
||||
request.WithLimiter(GetRateLimit()))
|
||||
if err != nil {
|
||||
log.Errorln(log.ExchangeSys, err)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package gemini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -15,24 +13,10 @@ const (
|
||||
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(ctx context.Context, f request.EndpointLimit) error {
|
||||
if f == request.Auth {
|
||||
return r.Auth.Wait(ctx)
|
||||
}
|
||||
return r.UnAuth.Wait(ctx)
|
||||
}
|
||||
|
||||
// SetRateLimit returns the rate limit for the exchange
|
||||
func SetRateLimit() *RateLimit {
|
||||
return &RateLimit{
|
||||
Auth: request.NewRateLimit(geminiRateInterval, geminiAuthRate),
|
||||
UnAuth: request.NewRateLimit(geminiRateInterval, geminiUnauthRate),
|
||||
// GetRateLimit returns the rate limit for the exchange
|
||||
func GetRateLimit() request.RateLimitDefinitions {
|
||||
return request.RateLimitDefinitions{
|
||||
request.Auth: request.NewRateLimitWithWeight(geminiRateInterval, geminiAuthRate, 1),
|
||||
request.UnAuth: request.NewRateLimitWithWeight(geminiRateInterval, geminiUnauthRate, 1),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user