From 1a861c98a1dccb3fae515aa9259ee82b34759f55 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 4 Nov 2021 12:49:33 +0800 Subject: [PATCH] binance: add order rate limiter for binance --- pkg/exchange/binance/exchange.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/exchange/binance/exchange.go b/pkg/exchange/binance/exchange.go index 7cbbb8c79..8511bd204 100644 --- a/pkg/exchange/binance/exchange.go +++ b/pkg/exchange/binance/exchange.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/adshao/go-binance/v2/futures" + "golang.org/x/time/rate" "os" "strconv" "strings" @@ -22,6 +23,8 @@ import ( const BNB = "BNB" +var orderLimiter = rate.NewLimiter(rate.Every(10 * time.Second), 50) + var log = logrus.WithFields(logrus.Fields{ "exchange": "binance", }) @@ -732,8 +735,11 @@ func (e *Exchange) submitSpotOrder(ctx context.Context, order types.SubmitOrder) func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error) { for _, order := range orders { - var createdOrder *types.Order + if err := orderLimiter.Wait(ctx) ; err != nil { + log.WithError(err).Errorf("order rate limiter wait error") + } + var createdOrder *types.Order if e.IsMargin { createdOrder, err = e.submitMarginOrder(ctx, order) } else {