From a2a638e90d0fdc28949add049e0c33e8149031ad Mon Sep 17 00:00:00 2001 From: Priojeet Das Priyom Date: Thu, 27 Jul 2023 17:41:33 +0600 Subject: [PATCH] Fix connector crash on timeout error --- services/blockchain-connector/shared/sdk/client.js | 7 +++++-- services/blockchain-connector/shared/sdk/events.js | 14 +++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/services/blockchain-connector/shared/sdk/client.js b/services/blockchain-connector/shared/sdk/client.js index 8bed48c15d..18ae918098 100644 --- a/services/blockchain-connector/shared/sdk/client.js +++ b/services/blockchain-connector/shared/sdk/client.js @@ -97,8 +97,11 @@ const invokeEndpoint = async (endpoint, params = {}, numRetries = NUM_REQUEST_RE const response = await apiClient._channel.invoke(endpoint, params); return response; } catch (err) { - if (retries && err instanceof TimeoutException) await delay(10); - else throw err; + if (retries && err.message.includes(timeoutMessage)) { + await delay(10); + } else { + throw err; + } } /* eslint-enable no-await-in-loop */ } while (retries--); diff --git a/services/blockchain-connector/shared/sdk/events.js b/services/blockchain-connector/shared/sdk/events.js index 87073d89a2..1f6166578a 100644 --- a/services/blockchain-connector/shared/sdk/events.js +++ b/services/blockchain-connector/shared/sdk/events.js @@ -38,6 +38,12 @@ const events = [ EVENT_TX_POOL_TRANSACTION_NEW, ]; +const catchAndRetry = async (err, fn, ...params) => { + const RETRY_MS = 1000; + logger.warn(`Invocation for ${fn.name} failed with error: ${err.message}. Retrying in ${RETRY_MS}ms.`); + setTimeout(() => fn(params).catch((_err) => logger.warn(`Retry for ${fn.name} failed with error: ${_err.message}.`)), RETRY_MS); +}; + const subscribeToAllRegisteredEvents = async () => { const apiClient = await getApiClient(); const registeredEvents = await getRegisteredEvents(); @@ -48,15 +54,17 @@ const subscribeToAllRegisteredEvents = async () => { async payload => { // Force update necessary caches on new chain events if (event.startsWith('chain_')) { - await getNodeInfo(true); - await getEscrowedAmounts(true); + await getNodeInfo(true) + .catch((err) => catchAndRetry(err, getNodeInfo, true)); + await getEscrowedAmounts(true) + .catch((err) => catchAndRetry(err, getEscrowedAmounts, true)); } logger.debug(`Received event: ${event} with payload:\n${util.inspect(payload)}`); Signals.get(event).dispatch(payload); }, ); - logger.info(`Subscribed to the API client event: ${event}`); + logger.info(`Subscribed to the API client event: ${event}.`); }); };