diff --git a/pkg/exchange/bybit/bybitapi/cancel_order_request.go b/pkg/exchange/bybit/bybitapi/cancel_order_request.go index 4ff7cb0a5..f7076be7d 100644 --- a/pkg/exchange/bybit/bybitapi/cancel_order_request.go +++ b/pkg/exchange/bybit/bybitapi/cancel_order_request.go @@ -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 diff --git a/pkg/exchange/bybit/bybitapi/cancel_order_request_requestgen.go b/pkg/exchange/bybit/bybitapi/cancel_order_request_requestgen.go index 715bf4214..e720203ac 100644 --- a/pkg/exchange/bybit/bybitapi/cancel_order_request_requestgen.go +++ b/pkg/exchange/bybit/bybitapi/cancel_order_request_requestgen.go @@ -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 - if err := response.DecodeJSON(&apiResponse); err != nil { - return nil, err + + 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 } diff --git a/pkg/exchange/bybit/exchange.go b/pkg/exchange/bybit/exchange.go index e0607de36..2b206e39d 100644 --- a/pkg/exchange/bybit/exchange.go +++ b/pkg/exchange/bybit/exchange.go @@ -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))