pkg/exchange: add rate limiter to ticker/tickers

This commit is contained in:
Edwin 2024-01-09 13:57:19 +08:00
parent 6d7a01ffae
commit 188b781116

View File

@ -25,6 +25,8 @@ var (
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) queryMarketLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
queryTickerLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
queryTickersLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
) )
const ID = "okex" const ID = "okex"
@ -112,18 +114,29 @@ func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
} }
func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) { func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) {
symbol = toLocalSymbol(symbol) if err := queryTickerLimiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("ticker rate limiter wait error: %w", err)
}
marketTicker, err := e.client.MarketTicker(ctx, symbol) symbol = toLocalSymbol(symbol)
marketTicker, err := e.client.NewGetTickerRequest().InstId(symbol).Do(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return toGlobalTicker(*marketTicker), nil if len(marketTicker) != 1 {
return nil, fmt.Errorf("unexpected length of %s market ticker, got: %v", symbol, marketTicker)
}
return toGlobalTicker(marketTicker[0]), nil
} }
func (e *Exchange) QueryTickers(ctx context.Context, symbols ...string) (map[string]types.Ticker, error) { func (e *Exchange) QueryTickers(ctx context.Context, symbols ...string) (map[string]types.Ticker, error) {
marketTickers, err := e.client.MarketTickers(ctx, okexapi.InstrumentTypeSpot) if err := queryTickersLimiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("tickers rate limiter wait error: %w", err)
}
marketTickers, err := e.client.NewGetTickersRequest().Do(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }