From d33af3787d458427e4339a1b4541c300f69ff8a9 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 09:38:25 -0400 Subject: [PATCH 1/5] Use timeout5 here --- 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 fb6963bf..08da945f 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -164,7 +164,7 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { } } - return timeout10( + return timeout5( promGen( require('../../Mediator/index').getGun(), require('../../Mediator/index').getUser() From 6d3f9e9f0182cc43b2cf087d377ca8b40f853de0 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 09:38:45 -0400 Subject: [PATCH 2/5] Better handling of timeouts in pubToEpub() --- services/gunDB/contact-api/utils/index.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 08da945f..0a802879 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -180,7 +180,9 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { */ const pubToEpub = async pub => { try { - const epub = await timeout10( + const TIMEOUT_PTR = {} + + const epubOrTimeout = await Promise.race([ CommonUtils.makePromise(res => { require('../../Mediator/index') .getGun() @@ -191,14 +193,23 @@ const pubToEpub = async pub => { res(data) } }) + }), + CommonUtils.makePromise(res => { + setTimeout(() => { + res(TIMEOUT_PTR) + }, 10000) }) - ) + ]) - return epub + if (epubOrTimeout === TIMEOUT_PTR) { + throw new Error(`Timeout inside pubToEpub()`) + } + + return epubOrTimeout } catch (err) { logger.error(`Error inside pubToEpub:`) logger.error(err) - throw new Error(`pubToEpub() -> ${err.message}`) + throw err } } From ae5cd44b74719b3f29b98e7372c078a1e2c31ca2 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 09:39:14 -0400 Subject: [PATCH 3/5] Remove unused timeout10() --- services/gunDB/contact-api/utils/index.js | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 0a802879..619b04d2 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -24,29 +24,6 @@ const delay = ms => new Promise(res => setTimeout(res, ms)) */ const mySecret = () => Promise.resolve(require('../../Mediator').getMySecret()) -/** - * @template T - * @param {Promise} promise - * @returns {Promise} - */ -const timeout10 = 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)) - }, 10000) - }) - ]) -} - /** * @template T * @param {Promise} promise From 163cd033e53491a90462e65fe86265c5bbac2581 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 15:04:06 -0400 Subject: [PATCH 4/5] New timeout mechanics with better error stack --- services/gunDB/contact-api/utils/index.js | 58 ++++++++++++----------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 619b04d2..7022e221 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -25,50 +25,54 @@ const delay = ms => new Promise(res => setTimeout(res, ms)) const mySecret = () => Promise.resolve(require('../../Mediator').getMySecret()) /** - * @template T - * @param {Promise} promise - * @returns {Promise} + * Just a pointer. */ -const timeout5 = promise => { +const TIMEOUT_PTR = {} + +/** + * @param {number} ms Milliseconds + * @returns {(promise: Promise) => Promise} + */ +const timeout = ms => async promise => { /** @type {NodeJS.Timeout} */ // @ts-ignore let timeoutID - return Promise.race([ + + const result = await Promise.race([ promise.then(v => { clearTimeout(timeoutID) return v }), - new Promise((_, rej) => { + CommonUtils.makePromise(res => { timeoutID = setTimeout(() => { - rej(new Error(Constants.ErrorCode.TIMEOUT_ERR)) - }, 5000) + clearTimeout(timeoutID) + res(TIMEOUT_PTR) + }, ms) }) ]) + + if (result === TIMEOUT_PTR) { + throw new Error(Constants.TIMEOUT_ERR) + } + + return result } /** - * @template T - * @param {Promise} promise - * @returns {Promise} + * Time outs at 10 seconds. */ -const timeout2 = promise => { - /** @type {NodeJS.Timeout} */ - // @ts-ignore - let timeoutID - return Promise.race([ - promise.then(v => { - clearTimeout(timeoutID) - return v - }), +const timeout10 = timeout(10) - new Promise((_, rej) => { - timeoutID = setTimeout(() => { - rej(new Error(Constants.ErrorCode.TIMEOUT_ERR)) - }, 2000) - }) - ]) -} +/** + * Time outs at 5 seconds. + */ +const timeout5 = timeout(5) + +/** + * Time outs at 2 seconds. + */ +const timeout2 = timeout(2) /** * @template T From c327fff3cdef9c7fae123d0475a7c475fec92338 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 15:04:35 -0400 Subject: [PATCH 5/5] Use timeout10 again --- 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 7022e221..f599afcc 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -145,7 +145,7 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { } } - return timeout5( + return timeout10( promGen( require('../../Mediator/index').getGun(), require('../../Mediator/index').getUser()