mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-16 07:26:47 +00:00
* rate limits: Make context aware * binance: rate limit allow for cancellation of reservation when deadline is exceeded * request: add context.done() before initiating any bulk work. * binance: update error return for rate limiting * request: updated dealine check to remove after time.Now procedure as this will obfuscate a deadline which will be limited by the context check on every attempt, so no need to sleep with delay.
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package zb
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
const (
|
|
zbRateInterval = time.Second
|
|
zbAuthLimit = 60
|
|
zbUnauthLimit = 60
|
|
|
|
zbKlineDataInterval = time.Second * 2
|
|
zbKlineDataLimit = 1
|
|
|
|
// Used to match endpints to rate limits
|
|
klineFunc request.EndpointLimit = iota
|
|
)
|
|
|
|
// RateLimit implements the request.Limiter interface
|
|
type RateLimit struct {
|
|
Auth *rate.Limiter
|
|
UnAuth *rate.Limiter
|
|
KlineData *rate.Limiter
|
|
}
|
|
|
|
// Limit limits the outbound requests
|
|
func (r *RateLimit) Limit(ctx context.Context, f request.EndpointLimit) error {
|
|
switch f {
|
|
case request.Auth:
|
|
return r.Auth.Wait(ctx)
|
|
case klineFunc:
|
|
return r.KlineData.Wait(ctx)
|
|
default:
|
|
return r.UnAuth.Wait(ctx)
|
|
}
|
|
}
|
|
|
|
// SetRateLimit returns the rate limit for the exchange
|
|
func SetRateLimit() *RateLimit {
|
|
return &RateLimit{
|
|
Auth: request.NewRateLimit(zbRateInterval, zbAuthLimit),
|
|
UnAuth: request.NewRateLimit(zbRateInterval, zbUnauthLimit),
|
|
KlineData: request.NewRateLimit(zbKlineDataInterval, zbKlineDataLimit),
|
|
}
|
|
}
|