pkg/exchange: move rate limiter to api

This commit is contained in:
edwin 2024-10-14 22:42:48 +08:00
parent 6f7e02daef
commit f22e4a1810
3 changed files with 26 additions and 11 deletions

View File

@ -12,7 +12,7 @@ type CancelOrderResponse struct {
OrderLinkId string `json:"orderLinkId"`
}
//go:generate PostRequest -url "/v5/order/cancel" -type CancelOrderRequest -responseDataType .CancelOrderResponse
//go:generate PostRequest -url "/v5/order/cancel" -type CancelOrderRequest -responseDataType .CancelOrderResponse -rateLimiter 5+15/1s
type CancelOrderRequest struct {
client requestgen.AuthenticatedAPIClient

View File

@ -1,4 +1,4 @@
// Code generated by "requestgen -method POST -responseType .APIResponse -responseDataField Result -url /v5/order/cancel -type CancelOrderRequest -responseDataType .CancelOrderResponse"; DO NOT EDIT.
// Code generated by "requestgen -method POST -responseType .APIResponse -responseDataField Result -url /v5/order/cancel -type CancelOrderRequest -responseDataType .CancelOrderResponse -rateLimiter 5+15/1s"; DO NOT EDIT.
package bybitapi
@ -6,11 +6,14 @@ import (
"context"
"encoding/json"
"fmt"
"golang.org/x/time/rate"
"net/url"
"reflect"
"regexp"
)
var CancelOrderRequestLimiter = rate.NewLimiter(15.000000150000002, 5)
func (p *CancelOrderRequest) Category(category Category) *CancelOrderRequest {
p.category = category
return p
@ -194,6 +197,9 @@ func (p *CancelOrderRequest) GetPath() string {
// Do generates the request object and send the request object to the API endpoint
func (p *CancelOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) {
if err := CancelOrderRequestLimiter.Wait(ctx); err != nil {
return nil, err
}
params, err := p.GetParameters()
if err != nil {
@ -216,15 +222,29 @@ func (p *CancelOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, erro
}
var apiResponse APIResponse
type responseUnmarshaler interface {
Unmarshal(data []byte) error
}
if unmarshaler, ok := interface{}(&apiResponse).(responseUnmarshaler); ok {
if err := unmarshaler.Unmarshal(response.Body); err != nil {
return nil, err
}
} else {
// The line below checks the content type, however, some API server might not send the correct content type header,
// Hence, this is commented for backward compatibility
// response.IsJSON()
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
}
type responseValidator interface {
Validate() error
}
validator, ok := interface{}(apiResponse).(responseValidator)
if ok {
if validator, ok := interface{}(&apiResponse).(responseValidator); ok {
if err := validator.Validate(); err != nil {
return nil, err
}

View File

@ -33,7 +33,6 @@ var (
// this includes QueryMarkets, QueryTicker, QueryAccountBalances, GetFeeRates
sharedRateLimiter = rate.NewLimiter(rate.Every(time.Second/5), 5)
queryOrderTradeRateLimiter = rate.NewLimiter(rate.Every(time.Second/5), 5)
orderRateLimiter = rate.NewLimiter(rate.Every(time.Second/10), 10)
closedOrderQueryLimiter = rate.NewLimiter(rate.Every(time.Second), 1)
log = logrus.WithFields(logrus.Fields{
@ -361,10 +360,6 @@ func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (err
req.Symbol(order.Market.Symbol)
if err := orderRateLimiter.Wait(ctx); err != nil {
errs = multierr.Append(errs, fmt.Errorf("cancel order rate limiter wait, order id: %s, error: %w", order.ClientOrderID, err))
continue
}
res, err := req.Do(ctx)
if err != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to cancel order id: %s, err: %w", order.ClientOrderID, err))