From fa494fcff1fae635a2e324ed516467ea3fc92f63 Mon Sep 17 00:00:00 2001 From: Steve Hackbarth Date: Thu, 2 Jun 2022 08:41:53 -0400 Subject: [PATCH] attempt retries to spawn connection upon transient error --- .../private/connection/spawn-connection.js | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/helpers/private/connection/spawn-connection.js b/helpers/private/connection/spawn-connection.js index 477afca..a8fd4d4 100644 --- a/helpers/private/connection/spawn-connection.js +++ b/helpers/private/connection/spawn-connection.js @@ -31,6 +31,38 @@ module.exports = function spawnConnection(datastore, cb) { return cb(err); }, failed: function failedToConnect(err) { + + function isTransientError(err) { + var errorMessage = err.message || (err.error || {}).message || ""; + var errorCode = err.code || (err.error || {}).code || ""; + return errorMessage.indexOf("Client has encountered a connection error") >= 0 || + errorMessage.indexOf("terminating connection") >= 0 || + errorMessage.indexOf("Connection terminated") >= 0 || + ["ECONNRESET", "ECONNREFUSED"].indexOf(errorCode) >= 0; + } + + if (datastore.config.attemptRetries && + isTransientError(err) && + (datastore.retryCount || 0) < datastore.config.attemptRetries) { + console.error("Error is transient, trying again in " + 2 * 4 ** (datastore.retryCount || 0) + " seconds."); + try { + setTimeout(() => { + var newFirstParam = Object.assign({}, datastore, { retryCount: (datastore.retryCount || 0) + 1 }); + spawnConnection(newFirstParam, (cbErr, cbResult) => { + if (cbErr) { + cb(cbErr); + return; + } + cb(null, cbResult); + }); + }, 2000 * 4 ** (datastore.retryCount || 0)); + } catch (retryError) { + console.error("Error during retry regimen"); + cb(err); + } + return; + } + // Setup some basic troubleshooting tips console.error('Troubleshooting tips:'); console.error('');