mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-03 15:10:49 +00:00
rate limit: make context aware (#731)
* 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.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package hitbtc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
@@ -27,18 +28,17 @@ type RateLimit struct {
|
||||
}
|
||||
|
||||
// Limit limits outbound requests
|
||||
func (r *RateLimit) Limit(f request.EndpointLimit) error {
|
||||
func (r *RateLimit) Limit(ctx context.Context, f request.EndpointLimit) error {
|
||||
switch f {
|
||||
case marketRequests:
|
||||
time.Sleep(r.MarketData.Reserve().Delay())
|
||||
return r.MarketData.Wait(ctx)
|
||||
case tradingRequests:
|
||||
time.Sleep(r.Trading.Reserve().Delay())
|
||||
return r.Trading.Wait(ctx)
|
||||
case otherRequests:
|
||||
time.Sleep(r.Other.Reserve().Delay())
|
||||
return r.Other.Wait(ctx)
|
||||
default:
|
||||
return errors.New("functionality not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetRateLimit returns the rate limit for the exchange
|
||||
|
||||
Reference in New Issue
Block a user