From f2ef69f00d0e462178feab9589aa93a16fda07b9 Mon Sep 17 00:00:00 2001 From: Maxim Shishmarev Date: Tue, 24 Mar 2020 11:53:32 +1100 Subject: [PATCH] Handle timeouts in guard testing; address some review comments --- js/modules/loki_rpc.js | 4 ++-- js/modules/loki_snode_api.js | 28 +++++++++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/js/modules/loki_rpc.js b/js/modules/loki_rpc.js index 13f661b2c..e7d6a524a 100644 --- a/js/modules/loki_rpc.js +++ b/js/modules/loki_rpc.js @@ -116,9 +116,9 @@ const sendOnionRequest = async (req_idx, nodePath, targetNode, plaintext) => { const url = `https://${nodePath[0].ip}:${nodePath[0].port}/onion_req`; // we only proxy to snodes... - process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; const response = await nodeFetch(url, fetchOptions); - process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1; + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'; return await processOnionResponse(req_idx, response, ctx_1.symmetricKey, true); } diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 8aaa22813..6e8719461 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -34,7 +34,7 @@ class LokiSnodeAPI { async test_guard_node(snode) { - log.info("Testing a candidate guard node ", snode); + log.info("[maxim] Testing a candidate guard node ", snode); // Send a post request and make sure it is OK const endpoint = "/storage_rpc/v1"; @@ -57,15 +57,26 @@ class LokiSnodeAPI { method: 'POST', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' }, - timeout: 1000 // 1s, we want a small timeout for testing + timeout: 10000 // 10s, we want a smaller timeout for testing }; - process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; - const response = await nodeFetch(url, fetchOptions); - process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1; + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; + + let response; + + try { + response = await nodeFetch(url, fetchOptions); + } catch (e) { + if (e.type === 'request-timeout') { + log.warn(`[maxim] test timeout for node,`, snode); + } + return false; + } finally { + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'; + } if (!response.ok) { - log.log(`Node ${snode} failed the guard test`); + log.info(`Node failed the guard test:`, snode); } return response.ok; @@ -166,6 +177,8 @@ class LokiSnodeAPI { const all_nodes = await this.getRandomSnodePool(); + log.info("[maxim] all nodes: ", all_nodes.length); + if (this.guardNodes.length == 0) { // Not cached, load from DB @@ -242,7 +255,8 @@ class LokiSnodeAPI { const trySeedNode = async (consecutiveErrors = 0) => { // Removed limit until there is a way to get snode info - // for individual nodes (needed for guard nodes) + // for individual nodes (needed for guard nodes); this way + // we get all active nodes const params = { active_only: true, fields: {