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

Commit

Permalink
🔨 Tweaks to avoid OOMs and high resource utilization
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Jan 10, 2024
1 parent a0c2b8d commit 59175fe
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions services/blockchain-connector/shared/sdk/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ const {
Signals,
HTTP,
Exceptions: { TimeoutException },
Utils: { isObject, waitForIt },
Utils: { delay, isObject, waitForIt },
} = require('lisk-service-framework');
const { createWSClient, createIPCClient } = require('@liskhq/lisk-api-client');

const crypto = require('crypto');

const config = require('../../config');
const delay = require('../utils/delay');

const logger = Logger();

Expand Down Expand Up @@ -121,6 +120,7 @@ const instantiateNewClient = async () => {
}
};

let isReInstantiateIntervalRunning = false;
const initClientPool = async poolSize => {
// Set the intervals only at application init
if (clientPool.length === 0) {
Expand All @@ -134,20 +134,31 @@ const initClientPool = async poolSize => {
}
}, 5 * 60 * 1000);

setInterval(() => {
clientPool.forEach(async (apiClient, index) => {
if (isObject(apiClient)) return;
// Re-instantiate interval: Replaces nulls in clientPool with new active apiClients
// isReInstantiateIntervalRunning is the safety check to skip callback execution if the previous one is already in-progress
setInterval(async () => {
if (isReInstantiateIntervalRunning) return;
isReInstantiateIntervalRunning = true;

for (let index = 0; index < clientPool.length; index++) {
const apiClient = clientPool[index];

// eslint-disable-next-line no-continue
if (isObject(apiClient)) continue;

// Re-instantiate when null
const newApiClient = await instantiateNewClient()
.then(client => {
client.poolIndex = index;
return client;
})
.catch(() => null);
// Delay to lower stress on the node
.catch(() => delay(Math.ceil(2 * WS_SERVER_PING_INTERVAL), null));
clientPool[index] = newApiClient;
if (newApiClient) Signals.get('newApiClient').dispatch(newApiClient.poolIndex);
});
}

isReInstantiateIntervalRunning = false;
}, WS_SERVER_PING_INTERVAL);
}

Expand Down

0 comments on commit 59175fe

Please sign in to comment.