Merge pull request #1126 from c9s/c9s/max/separate-rate-limiter

FIX: max: move submitOrderLimiter to the exchange wide var
This commit is contained in:
Yo-An Lin 2023-03-21 17:35:12 +08:00 committed by GitHub
commit da381dc252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,10 +22,8 @@ import (
// closedOrderQueryLimiter is used for the closed orders query rate limit, 1 request per second // closedOrderQueryLimiter is used for the closed orders query rate limit, 1 request per second
var closedOrderQueryLimiter = rate.NewLimiter(rate.Every(1*time.Second), 1) var closedOrderQueryLimiter = rate.NewLimiter(rate.Every(1*time.Second), 1)
var tradeQueryLimiter = rate.NewLimiter(rate.Every(3*time.Second), 1)
var accountQueryLimiter = rate.NewLimiter(rate.Every(3*time.Second), 1) var accountQueryLimiter = rate.NewLimiter(rate.Every(3*time.Second), 1)
var marketDataLimiter = rate.NewLimiter(rate.Every(2*time.Second), 10) var marketDataLimiter = rate.NewLimiter(rate.Every(2*time.Second), 10)
var submitOrderLimiter = rate.NewLimiter(rate.Every(300*time.Millisecond), 10)
var log = logrus.WithField("exchange", "max") var log = logrus.WithField("exchange", "max")
@ -37,6 +35,8 @@ type Exchange struct {
v3order *v3.OrderService v3order *v3.OrderService
v3margin *v3.MarginService v3margin *v3.MarginService
submitOrderLimiter, queryTradeLimiter *rate.Limiter
} }
func New(key, secret string) *Exchange { func New(key, secret string) *Exchange {
@ -54,6 +54,9 @@ func New(key, secret string) *Exchange {
secret: secret, secret: secret,
v3order: &v3.OrderService{Client: client}, v3order: &v3.OrderService{Client: client},
v3margin: &v3.MarginService{Client: client}, v3margin: &v3.MarginService{Client: client},
queryTradeLimiter: rate.NewLimiter(rate.Every(1*time.Second), 2),
submitOrderLimiter: rate.NewLimiter(rate.Every(100*time.Millisecond), 10),
} }
} }
@ -452,7 +455,7 @@ func (e *Exchange) Withdraw(ctx context.Context, asset string, amount fixedpoint
} }
func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (createdOrder *types.Order, err error) { func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (createdOrder *types.Order, err error) {
if err := submitOrderLimiter.Wait(ctx); err != nil { if err := e.submitOrderLimiter.Wait(ctx); err != nil {
return nil, err return nil, err
} }
@ -792,7 +795,7 @@ func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since,
// give LastTradeID -> ignore start_time (but still can filter the end_time) // give LastTradeID -> ignore start_time (but still can filter the end_time)
// without any parameters -> return trades within 24 hours // without any parameters -> return trades within 24 hours
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) { func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
if err := tradeQueryLimiter.Wait(ctx); err != nil { if err := e.queryTradeLimiter.Wait(ctx); err != nil {
return nil, err return nil, err
} }