From f04c1a3728e786225b6aad2fbec8c57a25fdcd28 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 2 Aug 2020 13:12:57 -0400 Subject: [PATCH 1/4] use timeout5 instead of timeout10 --- services/gunDB/contact-api/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index f35964ce..89b53180 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -88,7 +88,7 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { let resolvedValue try { - resolvedValue = await timeout10( + resolvedValue = await timeout5( promGen( require('../../Mediator/index').getGun(), require('../../Mediator/index').getUser() From 724350ebd74843897e93ef8121d7a65449ed0270 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 2 Aug 2020 13:13:20 -0400 Subject: [PATCH 2/4] first retry should be quickly after first try --- services/gunDB/contact-api/utils/index.js | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 89b53180..81b12fad 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -116,6 +116,36 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { ` args: ${promGen.toString()} -- ${shouldRetry.toString()}` ) + await delay(200) + + try { + resolvedValue = await timeout5( + promGen( + require('../../Mediator/index').getGun(), + require('../../Mediator/index').getUser() + ) + ) + + if (shouldRetry(resolvedValue)) { + logger.info( + 'force retrying' + + ` args: ${promGen.toString()} -- ${shouldRetry.toString()} \n resolvedValue: ${resolvedValue}, type: ${typeof resolvedValue}` + ) + } else { + return resolvedValue + } + } catch (e) { + logger.error(e) + if (e.message === 'NOT_AUTH') { + throw e + } + } + + logger.info( + `\n retrying \n` + + ` args: ${promGen.toString()} -- ${shouldRetry.toString()}` + ) + await delay(3000) try { From 465a5049c24cd5b74add0961d1ad32f3ed423f52 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 2 Aug 2020 13:50:14 -0400 Subject: [PATCH 3/4] timeout2 routine --- services/gunDB/contact-api/utils/index.js | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 81b12fad..a5951e16 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -70,6 +70,29 @@ const timeout5 = promise => { ]) } +/** + * @template T + * @param {Promise} promise + * @returns {Promise} + */ +const timeout2 = promise => { + /** @type {NodeJS.Timeout} */ + // @ts-ignore + let timeoutID + return Promise.race([ + promise.then(v => { + clearTimeout(timeoutID) + return v + }), + + new Promise((_, rej) => { + timeoutID = setTimeout(() => { + rej(new Error(Constants.ErrorCode.TIMEOUT_ERR)) + }, 2000) + }) + ]) +} + /** * @template T * @param {(gun: GUNNode, user: UserGUNNode) => Promise} promGen The function @@ -330,5 +353,6 @@ module.exports = { mySecret, promisifyGunNode: require('./promisifygun'), timeout5, + timeout2, isNodeOnline } From 7be5016bb20796823072c64f49c074d9eb0e3af1 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 2 Aug 2020 13:53:17 -0400 Subject: [PATCH 4/4] use timeout2 on first try --- services/gunDB/contact-api/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index a5951e16..a35bbea5 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -111,7 +111,7 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { let resolvedValue try { - resolvedValue = await timeout5( + resolvedValue = await timeout2( promGen( require('../../Mediator/index').getGun(), require('../../Mediator/index').getUser()