Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt retries to spawn connection upon transient error #294

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions helpers/private/connection/spawn-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down