Skip to content

Commit

Permalink
Merge pull request #15 from martiliones/dev
Browse files Browse the repository at this point in the history
feat(options): add the option to disable health checks at startup
  • Loading branch information
martiliones authored Mar 25, 2022
2 parents d693e18 + 6643f74 commit 51dc7ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions helpers/healthCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
},
Expand Down Expand Up @@ -145,7 +148,7 @@ 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

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 51dc7ea

Please sign in to comment.