From a115c4133b7b15580bccc6e8a593538f5ec6543b Mon Sep 17 00:00:00 2001 From: martiliones Date: Tue, 22 Mar 2022 05:12:19 +0600 Subject: [PATCH] feat(options): add the option to disable health checks at startup --- helpers/healthCheck.js | 27 +++++++++++++++------------ index.js | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/helpers/healthCheck.js b/helpers/healthCheck.js index 71b57e7..f68c504 100644 --- a/helpers/healthCheck.js +++ b/helpers/healthCheck.js @@ -7,7 +7,7 @@ const dnsPromises = require('dns').promises; const CHECK_NODES_INTERVAL = 60 * 5 * 1000; // Update active nodes every 5 minutes const HEIGHT_EPSILON = 5; // Used to group nodes by height and choose synced -module.exports = (nodes) => { +module.exports = (nodes, checkHealthAtStartup = true) => { isCheckingNodes = false; nodesList = nodes; @@ -16,29 +16,32 @@ module.exports = (nodes) => { /** * Updates active nodes. If nodes are already updating, returns Promise of previous call - * @returns {Promise} Call changeNodes().then to do something when update complete - */ - function changeNodes (isPlannedUpdate = false) { + * @returns {Promise} Call changeNodes().then to do something when update complete + */ + function changeNodes(isPlannedUpdate = false) { if (!isCheckingNodes) { changeNodesPromise = new Promise(async (resolve) => { if (!isPlannedUpdate) { logger.warn('[ADAMANT js-api] Health check: Forcing to update active nodes…'); } - await checkNodes(isPlannedUpdate? false : true) + await checkNodes(isPlannedUpdate ? false : true) resolve(true) }); } return changeNodesPromise } - changeNodes(true) - setInterval(() => { changeNodes(true) }, CHECK_NODES_INTERVAL); + + if (checkHealthAtStartup) { + changeNodes(true) + setInterval(() => { changeNodes(true) }, CHECK_NODES_INTERVAL); + } return { /** - * @returns {string} Current active node, f. e. http://88.198.156.44:36666 - */ + * @returns {string} Current active node, f. e. http://88.198.156.44:36666 + */ node: () => { return activeNode; }, @@ -145,10 +148,10 @@ async function checkNodes(forceChangeActiveNode) { this.liveNodes.sort((a, b) => a.ping - b.ping); if (forceChangeActiveNode && biggestGroup.length > 1 && this.activeNode === biggestGroup[0].node) - this.activeNode = biggestGroup[validator.getRandomIntInclusive(1, biggestGroup.length-1)].node // Use random node from which are synced + this.activeNode = biggestGroup[validator.getRandomIntInclusive(1, biggestGroup.length - 1)].node // Use random node from which are synced else this.activeNode = biggestGroup[0].node; // Use node with minimum ping among which are synced - + } socket.reviseConnection(this.liveNodes); let unavailableCount = this.nodesList.length - this.liveNodes.length; @@ -159,7 +162,7 @@ async function checkNodes(forceChangeActiveNode) { if (outOfSyncCount) nodesInfoString += `, ${outOfSyncCount} nodes are not synced` logger.log(`[ADAMANT js-api] Health check: Found ${supportedCount} supported and synced nodes${nodesInfoString}. Active node is ${this.activeNode}.`); - + } } catch (e) { diff --git a/index.js b/index.js index daba8ec..7a2e1ef 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ const logger = require('./helpers/logger'); module.exports = (params, log) => { log = log || console; logger.initLogger(params.logLevel, log); - const nodeManager = healthCheck(params.node); + const nodeManager = healthCheck(params.node, params.checkHealthAtStartup); return { get: get(nodeManager),