mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +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.
43 lines
885 B
Go
43 lines
885 B
Go
package btse
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
const (
|
|
btseRateInterval = time.Second
|
|
btseQueryLimit = 15
|
|
btseOrdersLimit = 75
|
|
|
|
queryFunc request.EndpointLimit = iota
|
|
orderFunc
|
|
)
|
|
|
|
// RateLimit implements the request.Limiter interface
|
|
type RateLimit struct {
|
|
Query *rate.Limiter
|
|
Orders *rate.Limiter
|
|
}
|
|
|
|
// Limit executes rate limiting functionality for exchange
|
|
func (r *RateLimit) Limit(ctx context.Context, f request.EndpointLimit) error {
|
|
switch f {
|
|
case orderFunc:
|
|
return r.Orders.Wait(ctx)
|
|
default:
|
|
return r.Query.Wait(ctx)
|
|
}
|
|
}
|
|
|
|
// SetRateLimit returns the rate limit for the exchange
|
|
func SetRateLimit() *RateLimit {
|
|
return &RateLimit{
|
|
Orders: request.NewRateLimit(btseRateInterval, btseOrdersLimit),
|
|
Query: request.NewRateLimit(btseRateInterval, btseQueryLimit),
|
|
}
|
|
}
|