diff options
author | Vesselina Ratcheva <vesselina.ratcheva@10gen.com> | 2018-02-15 13:39:58 -0500 |
---|---|---|
committer | Vesselina Ratcheva <vesselina.ratcheva@10gen.com> | 2018-02-15 13:43:54 -0500 |
commit | 30698911d3bbde3e351c455c8bacc96bb916fac8 (patch) | |
tree | 2b0207497f4c2d26e792d19a3ef32b61f7d896b7 /src/mongo/shell/utils.js | |
parent | 4c97e0cf511426a3d9df9f80bb9323468e8e59fe (diff) | |
download | mongo-30698911d3bbde3e351c455c8bacc96bb916fac8.tar.gz |
SERVER-32647 Retry connecting to replica set when given a seed node
Diffstat (limited to 'src/mongo/shell/utils.js')
-rw-r--r-- | src/mongo/shell/utils.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/mongo/shell/utils.js b/src/mongo/shell/utils.js index 1378dadcf75..c07aebac8d7 100644 --- a/src/mongo/shell/utils.js +++ b/src/mongo/shell/utils.js @@ -37,6 +37,32 @@ function _getErrorWithCode(codeOrObj, message) { return e; } +/** + * Executes the specified function and retries it if it fails due to exception related to network + * error. If it exhausts the number of allowed retries, it simply throws the last exception. + * + * Returns the return value of the input call. + */ +function retryOnNetworkError(func, numRetries, sleepMs) { + numRetries = numRetries || 1; + sleepMs = sleepMs || 1000; + + while (true) { + try { + return func(); + } catch (e) { + if (isNetworkError(e) && numRetries > 0) { + print("Network error occurred and the call will be retried: " + + tojson({error: e.toString(), stack: e.stack})); + numRetries--; + sleep(sleepMs); + } else { + throw e; + } + } + } +} + // Checks if a javascript exception is a network error. function isNetworkError(error) { return error.message.indexOf("network error") >= 0 || |