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:
Ryan O'Hara-Reid
2021-08-10 12:08:27 +10:00
committed by GitHub
parent 4602ade809
commit 232d6ebc1f
18 changed files with 245 additions and 198 deletions

View File

@@ -1,6 +1,7 @@
package huobi
import (
"context"
"time"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
@@ -41,24 +42,23 @@ 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 {
// TODO: Add futures and swap functionality
case huobiFuturesAuth:
time.Sleep(r.FuturesAuth.Reserve().Delay())
return r.FuturesAuth.Wait(ctx)
case huobiFuturesUnAuth:
time.Sleep(r.FuturesUnauth.Reserve().Delay())
return r.FuturesUnauth.Wait(ctx)
case huobiFuturesTransfer:
time.Sleep(r.FuturesXfer.Reserve().Delay())
return r.FuturesXfer.Wait(ctx)
case huobiSwapAuth:
time.Sleep(r.SwapAuth.Reserve().Delay())
return r.SwapAuth.Wait(ctx)
case huobiSwapUnauth:
time.Sleep(r.SwapUnauth.Reserve().Delay())
return r.SwapUnauth.Wait(ctx)
default:
// Spot calls
time.Sleep(r.Spot.Reserve().Delay())
return r.Spot.Wait(ctx)
}
return nil
}
// SetRateLimit returns the rate limit for the exchange