From ce3fc2c9f66dd970d101f18523b6ecde3afb3baa Mon Sep 17 00:00:00 2001 From: martiliones Date: Thu, 12 Jan 2023 22:38:15 +0600 Subject: [PATCH 1/3] feat: add check health timeout --- src/helpers/constants.js | 3 +-- src/helpers/healthCheck.js | 16 +++++++++++----- src/index.js | 3 ++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/helpers/constants.js b/src/helpers/constants.js index 9af3e15..0a3f598 100644 --- a/src/helpers/constants.js +++ b/src/helpers/constants.js @@ -1,5 +1,4 @@ module.exports = { - epochTime: new Date(Date.UTC(2017, 8, 2, 17, 0, 0, 0)), fees: { send: 50000000, @@ -27,6 +26,7 @@ module.exports = { STATE: 9, }, maxVotesPerTransaction: 33, + HEALTH_CHECK_TIMEOUT: 4000, // 4 seconds SAT: 100000000, RE_HEX: /^[a-fA-F0-9]+$/, RE_BASE64: /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/, @@ -42,5 +42,4 @@ module.exports = { RE_HTTP_URL: /^https?:\/\/(.*)$/, RE_IP: /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/, - }; diff --git a/src/helpers/healthCheck.js b/src/helpers/healthCheck.js index e0cd4bc..c8c4bfa 100644 --- a/src/helpers/healthCheck.js +++ b/src/helpers/healthCheck.js @@ -5,12 +5,17 @@ const socket = require('./wsClient'); const logger = require('./logger'); const validator = require('./validator'); -const {RE_IP, RE_HTTP_URL} = require('./constants'); +const {RE_IP, RE_HTTP_URL, HEALTH_CHECK_TIMEOUT} = require('./constants'); 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, checkHealthAtStartup = true, checkHealthAtStartupCallback) => { +module.exports = ( + nodes, + checkHealthAtStartup = true, + timeout = HEALTH_CHECK_TIMEOUT, + checkHealthAtStartupCallback, +) => { const nodesList = nodes; let isCheckingNodes = false; let startupCallback = checkHealthAtStartupCallback; @@ -56,7 +61,7 @@ module.exports = (nodes, checkHealthAtStartup = true, checkHealthAtStartupCallba try { const start = unixTimestamp(); - const req = await checkNode(`${node}/api/node/status`); + const req = await checkNode(`${node}/api/node/status`, timeout); const [url] = node.replace(RE_HTTP_URL, '$1').split(':'); const ifIP = RE_IP.test(url); @@ -215,10 +220,11 @@ async function getIP(url) { /** * Requests status from a single ADAMANT node * @param {string} url Node URL to request + * @param {number} timeout Request timeout * @return {Promise} Node's status information */ -function checkNode(url) { - return axios.get(url) +function checkNode(url, timeout) { + return axios.get(url, {timeout}) .then((response) => ({status: response.data})) .catch((err) => false); } diff --git a/src/index.js b/src/index.js index 7ff83d5..3941f3f 100644 --- a/src/index.js +++ b/src/index.js @@ -24,7 +24,7 @@ const keys = require('./helpers/keys'); /** * Create new API instance - * @param {{ node: string, checkHealthAtStartup: boolean, logLevel: string}} params + * @param {{ node: string, checkHealthAtStartup: boolean, logLevel: string, checkHealthTimeout: number}} params * @param {{ error: function, warn: function, info: function, log: function }?} customLogger * @param {function?} checkHealthAtStartupCallback callback which is called after the first health check or * just at startup when params.checkHealthAtStartup is passed @@ -38,6 +38,7 @@ module.exports = (params, customLogger, checkHealthAtStartupCallback) => { const nodeManager = healthCheck( params.node, params.checkHealthAtStartup, + params.checkHealthTimeout, checkHealthAtStartupCallback, ); From 65ff52eb040d7167e7c57d7e824510d488ddffc4 Mon Sep 17 00:00:00 2001 From: martiliones Date: Thu, 12 Jan 2023 22:47:55 +0600 Subject: [PATCH 2/3] chore: change param name checkHealthTimeout -> checkNodeTimeout --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3941f3f..1b6d7bb 100644 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,7 @@ module.exports = (params, customLogger, checkHealthAtStartupCallback) => { const nodeManager = healthCheck( params.node, params.checkHealthAtStartup, - params.checkHealthTimeout, + params.checkNodeTimeout, checkHealthAtStartupCallback, ); From c5dc8fbd753b2e726860bcfe6357a095e113ded4 Mon Sep 17 00:00:00 2001 From: martiliones Date: Fri, 13 Jan 2023 01:02:30 +0600 Subject: [PATCH 3/3] chore: pump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4056750..14f67a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "adamant-api", - "version": "1.7.0", + "version": "1.8.0", "description": "REST API for ADAMANT Blockchain", "main": "src/index.js", "scripts": {