XChange icon indicating copy to clipboard operation
XChange copied to clipboard

is okx thread safe?

Open douggie opened this issue 1 year ago • 2 comments

I noticed a lot of okx requests get rejected with invaild sign when submitting multiple requests at same time.

{"instId":"BTC-USDT","ordId":"633717666398052352","clOrderId":null}
2023-10-15 09:27:46.33 [pool-18-thread-7] TRACE si.mazi.rescu.HttpTemplate - Request headers = {Accept=application/json, OK-ACCESS-KEY=abc key, OK-ACCESS-TIMESTAMP=2023-10-15T09:27:46.332Z, OK-ACCESS-SIGN=qr7/wIi21sfltMSzysANBSwoaqBOCM/XFrWhR8kk+Wc=, OK-ACCESS-PASSPHRASE=abc phrase, Content-Type=application/json}```

```2023-10-15 09:27:46.33 [pool-18-thread-14] DEBUG si.mazi.rescu.HttpTemplate - Executing POST request at https://www.okx.com/api/v5/trade/cancel-order  body
{"instId":"BTC-USDT","ordId":"633717666570018817","clOrderId":null}
2023-10-15 09:27:46.33 [pool-18-thread-14] TRACE si.mazi.rescu.HttpTemplate - Request headers = {Accept=application/json, OK-ACCESS-KEY=abc key, OK-ACCESS-TIMESTAMP=2023-10-15T09:27:46.332Z, OK-ACCESS-SIGN=LTCY6+x9eKDUlajK6evemhNCl62YbxG/q6zy1ml2q6w=, OK-ACCESS-PASSPHRASE=abc phrase, Content-Type=application/json}

Such issues don't happen on binace xchange implementation, they both seem to use the same decorator pattern, so wondering if there is something else here is not thread safe. Pointers welcomed on where the threading issue might be! Making the public String digestParams(RestInvocation restInvocation) synchronised seems a bit overkill public synchronized String digestParams(RestInvocation restInvocation)

Binance

      Instrument pair, Long orderId, String origClientOrderId, String newClientOrderId,  Boolean isMarginOrder)
      throws IOException, BinanceException {
    return decorateApiCall(
            () ->
                    (pair instanceof FuturesContract)
            ? binanceFutures.cancelFutureOrder(
                            BinanceAdapters.toSymbol(pair),
                            orderId,
                            origClientOrderId,
                            getRecvWindow(),
                            getTimestampFactory(),
                            super.apiKey,
                            super.signatureCreator
                    ) :
                        isMarginOrder ?
                            binance.cancelMarginOrder(
                                BinanceAdapters.toSymbol(pair),
                                Boolean.FALSE,
                                orderId,
                                origClientOrderId,
                                newClientOrderId,
                                getRecvWindow(),
                                getTimestampFactory(),
                                super.apiKey,
                                super.signatureCreator)
            :   binance.cancelOrder(
                    BinanceAdapters.toSymbol(pair),
                    orderId,
                    origClientOrderId,
                    newClientOrderId,
                    getRecvWindow(),
                    getTimestampFactory(),
                    super.apiKey,
                    super.signatureCreator))
        .withRetry(retry("cancelOrder"))
        .withRateLimiter(rateLimiter(REQUEST_WEIGHT_RATE_LIMITER))
        .call();
  }
public OkexResponse<List<OkexOrderResponse>> cancelOkexOrder(OkexCancelOrderRequest order)
      throws IOException {
    try {
      return decorateApiCall(
              () ->
                  okexAuthenticated.cancelOrder(
                      exchange.getExchangeSpecification().getApiKey(),
                      signatureCreator,
                      DateUtils.toUTCISODateString(new Date()),
                      (String)
                          exchange
                              .getExchangeSpecification()
                              .getExchangeSpecificParametersItem(PARAM_PASSPHRASE),
                      (String)
                          exchange
                              .getExchangeSpecification()
                              .getExchangeSpecificParametersItem(PARAM_SIMULATED),
                      order))
          .withRateLimiter(rateLimiter(OkexAuthenticated.cancelOrderPath))
          .call();
    } catch (OkexException e) {
      throw handleError(e);
    }
  }

douggie avatar Oct 15 '23 10:10 douggie