mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-28 15:10:32 +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 bitflyer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
||||
@@ -29,24 +30,29 @@ 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 request.Auth:
|
||||
time.Sleep(r.Auth.Reserve().Delay())
|
||||
return r.Auth.Wait(ctx)
|
||||
case orders:
|
||||
res := r.Auth.Reserve()
|
||||
time.Sleep(r.Order.Reserve().Delay())
|
||||
time.Sleep(res.Delay())
|
||||
err := r.Auth.Wait(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.Order.Wait(ctx)
|
||||
case lowVolume:
|
||||
authShell := r.Auth.Reserve()
|
||||
orderShell := r.Order.Reserve()
|
||||
time.Sleep(r.LowVolume.Reserve().Delay())
|
||||
time.Sleep(orderShell.Delay())
|
||||
time.Sleep(authShell.Delay())
|
||||
err := r.LowVolume.Wait(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = r.Order.Wait(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.Auth.Wait(ctx)
|
||||
default:
|
||||
time.Sleep(r.UnAuth.Reserve().Delay())
|
||||
return r.UnAuth.Wait(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetRateLimit returns the rate limit for the exchange
|
||||
|
||||
Reference in New Issue
Block a user