From 9757ca290bad8457158980c3dc3ee9444e10ece1 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 18 Mar 2022 17:33:10 +0800 Subject: [PATCH] kucoin: add trades, orders rate limiter --- pkg/exchange/kucoin/exchange.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/exchange/kucoin/exchange.go b/pkg/exchange/kucoin/exchange.go index 7c63bd3ee..ce2e34b28 100644 --- a/pkg/exchange/kucoin/exchange.go +++ b/pkg/exchange/kucoin/exchange.go @@ -13,11 +13,13 @@ import ( "golang.org/x/time/rate" "github.com/c9s/bbgo/pkg/exchange/kucoin/kucoinapi" - "github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/types" ) var marketDataLimiter = rate.NewLimiter(rate.Every(500*time.Millisecond), 1) +var queryTradeLimiter = rate.NewLimiter(rate.Every(5*time.Second), 1) +var queryOrderLimiter = rate.NewLimiter(rate.Every(5*time.Second), 1) var ErrMissingSequence = errors.New("sequence is missing") @@ -314,6 +316,10 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, req.EndAt(until) } + if err := queryOrderLimiter.Wait(ctx); err != nil { + return nil, err + } + orderList, err := req.Do(ctx) if err != nil { return orders, err @@ -354,6 +360,10 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type req.EndAt(*options.EndTime) } + if err := queryTradeLimiter.Wait(ctx); err != nil { + return trades, err + } + response, err := req.Do(ctx) if err != nil { return trades, err