Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Fix connector crash on timeout error
Browse files Browse the repository at this point in the history
  • Loading branch information
priojeetpriyom committed Jul 27, 2023
1 parent 659e069 commit a2a638e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 5 additions & 2 deletions services/blockchain-connector/shared/sdk/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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--);
Expand Down
14 changes: 11 additions & 3 deletions services/blockchain-connector/shared/sdk/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}.`);
});
};

Expand Down

0 comments on commit a2a638e

Please sign in to comment.