mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 15:09:51 +00:00
* 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
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package request
|
|
|
|
import (
|
|
"errors"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
// Const here define individual functionality sub types for rate limiting
|
|
const (
|
|
Unset EndpointLimit = iota
|
|
Auth
|
|
UnAuth
|
|
)
|
|
|
|
// BasicLimit denotes basic rate limit that implements the Limiter interface
|
|
// does not need to set endpoint functionality.
|
|
type BasicLimit struct {
|
|
r *rate.Limiter
|
|
}
|
|
|
|
// Limit executes a single rate limit set by NewRateLimit
|
|
func (b *BasicLimit) Limit(_ EndpointLimit) error {
|
|
time.Sleep(b.r.Reserve().Delay())
|
|
return nil
|
|
}
|
|
|
|
// EndpointLimit defines individual endpoint rate limits that are set when
|
|
// New is called.
|
|
type EndpointLimit int
|
|
|
|
// Limiter interface groups rate limit functionality defined in the REST
|
|
// wrapper for extended rate limiting configuration i.e. Shells of rate
|
|
// limits with a global rate for sub rates.
|
|
type Limiter interface {
|
|
Limit(EndpointLimit) error
|
|
}
|
|
|
|
// NewRateLimit creates a new RateLimit based of time interval and how many
|
|
// actions allowed and breaks it down to an actions-per-second basis -- Burst
|
|
// rate is kept as one as this is not supported for out-bound requests.
|
|
func NewRateLimit(interval time.Duration, actions int) *rate.Limiter {
|
|
if actions <= 0 || interval <= 0 {
|
|
// Returns an un-restricted rate limiter
|
|
return rate.NewLimiter(rate.Inf, 1)
|
|
}
|
|
|
|
i := 1 / interval.Seconds()
|
|
rps := i * float64(actions)
|
|
return rate.NewLimiter(rate.Limit(rps), 1)
|
|
}
|
|
|
|
// NewBasicRateLimit returns an object that implements the limiter interface
|
|
// for basic rate limit
|
|
func NewBasicRateLimit(interval time.Duration, actions int) Limiter {
|
|
return &BasicLimit{NewRateLimit(interval, actions)}
|
|
}
|
|
|
|
// InitiateRateLimit sleeps for designated end point rate limits
|
|
func (r *Requester) InitiateRateLimit(e EndpointLimit) error {
|
|
if atomic.LoadInt32(&r.disableRateLimiter) == 1 {
|
|
return nil
|
|
}
|
|
|
|
if r.Limiter != nil {
|
|
return r.Limiter.Limit(e)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DisableRateLimiter disables the rate limiting system for the exchange
|
|
func (r *Requester) DisableRateLimiter() error {
|
|
if !atomic.CompareAndSwapInt32(&r.disableRateLimiter, 0, 1) {
|
|
return errors.New("rate limiter already disabled")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EnableRateLimiter enables the rate limiting system for the exchange
|
|
func (r *Requester) EnableRateLimiter() error {
|
|
if !atomic.CompareAndSwapInt32(&r.disableRateLimiter, 1, 0) {
|
|
return errors.New("rate limiter already enabled")
|
|
}
|
|
return nil
|
|
}
|