pkg/exchange: add rate limiter to QueryMarkets

This commit is contained in:
Edwin 2024-01-09 11:56:10 +08:00
parent ba5882f7b6
commit 6e160e7a36

View File

@ -3,7 +3,6 @@ package okex
import ( import (
"context" "context"
"fmt" "fmt"
"math"
"strconv" "strconv"
"time" "time"
@ -24,6 +23,8 @@ import (
var ( var (
marketDataLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 5) marketDataLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 5)
orderRateLimiter = rate.NewLimiter(rate.Every(300*time.Millisecond), 5) orderRateLimiter = rate.NewLimiter(rate.Every(300*time.Millisecond), 5)
queryMarketLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
) )
const ID = "okex" const ID = "okex"
@ -68,10 +69,11 @@ func (e *Exchange) Name() types.ExchangeName {
} }
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) { func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
instruments, err := e.client.NewGetInstrumentsRequest(). if err := queryMarketLimiter.Wait(ctx); err != nil {
InstrumentType(okexapi.InstrumentTypeSpot). return nil, fmt.Errorf("markets rate limiter wait error: %w", err)
Do(ctx) }
instruments, err := e.client.NewGetInstrumentsInfoRequest().Do(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -87,8 +89,8 @@ func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
BaseCurrency: instrument.BaseCurrency, BaseCurrency: instrument.BaseCurrency,
// convert tick size OKEx to precision // convert tick size OKEx to precision
PricePrecision: int(-math.Log10(instrument.TickSize.Float64())), PricePrecision: instrument.TickSize.NumFractionalDigits(),
VolumePrecision: int(-math.Log10(instrument.LotSize.Float64())), VolumePrecision: instrument.LotSize.NumFractionalDigits(),
// TickSize: OKEx's price tick, for BTC-USDT it's "0.1" // TickSize: OKEx's price tick, for BTC-USDT it's "0.1"
TickSize: instrument.TickSize, TickSize: instrument.TickSize,