node-redis-retry-strategy
Advanced tools
Comparing version 1.0.11 to 2.0.0
34
index.js
"use strict"; | ||
var path = require("path"); | ||
var times = require(path.resolve(__dirname, "times")); | ||
var defaults = require(path.resolve(__dirname, "defaults")); | ||
module.exports = function init (moduleOptions) { | ||
var redisExists = false; | ||
if (moduleOptions) { | ||
if (moduleOptions.hasOwnProperty("number_of_retry_attempts") && !isNaN(moduleOptions.number_of_retry_attempts)) { | ||
times.NUMBER_OF_RETRY_ATTEMPTS = moduleOptions.number_of_retry_attempts; | ||
defaults.NUMBER_OF_RETRY_ATTEMPTS = moduleOptions.number_of_retry_attempts; | ||
} | ||
if (moduleOptions.hasOwnProperty("delay_of_retry_attempts") && !isNaN(moduleOptions.delay_of_retry_attempts)) { | ||
times.DELAY_OF_RETRY_ATTEMPTS = moduleOptions.delay_of_retry_attempts; | ||
defaults.DELAY_OF_RETRY_ATTEMPTS = moduleOptions.delay_of_retry_attempts; | ||
} | ||
if (moduleOptions.hasOwnProperty("wait_time") && !isNaN(moduleOptions.wait_time)) { | ||
times.WAIT_TIME = moduleOptions.wait_time; | ||
defaults.WAIT_TIME = moduleOptions.wait_time; | ||
} | ||
if (moduleOptions.hasOwnProperty("allow_to_start_without_connection") && typeof moduleOptions.allow_to_start_without_connection === "boolean") { | ||
defaults.ALLOW_TO_START_WITHOUT_CONNECTION = moduleOptions.allow_to_start_without_connection; | ||
} | ||
} | ||
/** | ||
@@ -32,3 +38,11 @@ * In case of Redis down, every WAIT_TIME try to connect for NUMBER_OF_RETRY_ATTEMPTS | ||
if (!times.NUMBER_OF_RETRY_ATTEMPTS) { | ||
if (!redisExists && !defaults.ALLOW_TO_START_WITHOUT_CONNECTION && options.error && options.error.code === "ECONNREFUSED") { | ||
// End reconnecting on a specific error and flush all commands with | ||
// a individual error | ||
return new Error("The server refused the connection"); | ||
} | ||
redisExists = true; | ||
if (!defaults.NUMBER_OF_RETRY_ATTEMPTS) { | ||
// End reconnecting with built in error | ||
@@ -40,9 +54,9 @@ return undefined; | ||
// if there is no attempt, try again | ||
return times.DELAY_OF_RETRY_ATTEMPTS; | ||
return defaults.DELAY_OF_RETRY_ATTEMPTS; | ||
} | ||
if ((options.attempt % (times.NUMBER_OF_RETRY_ATTEMPTS + 1)) === 0) { | ||
return times.WAIT_TIME; | ||
if ((options.attempt % (defaults.NUMBER_OF_RETRY_ATTEMPTS + 1)) === 0) { | ||
return defaults.WAIT_TIME; | ||
} else { | ||
return times.DELAY_OF_RETRY_ATTEMPTS; | ||
return defaults.DELAY_OF_RETRY_ATTEMPTS; | ||
} | ||
@@ -49,0 +63,0 @@ }; |
{ | ||
"name": "node-redis-retry-strategy", | ||
"version": "1.0.11", | ||
"version": "2.0.0", | ||
"description": "My custom node_redis retry_strategy function", | ||
@@ -27,4 +27,4 @@ "main": "./index.js", | ||
"codacy-coverage": "^3.4.0", | ||
"codecov": "^3.7.0", | ||
"jest": "^26.0.1" | ||
"codecov": "^3.8.1", | ||
"jest": "^26.6.3" | ||
}, | ||
@@ -31,0 +31,0 @@ "files": [ |
@@ -14,3 +14,5 @@ # node-redis-retry-strategy | ||
The strategy is: every `wait_time` try to connect for `number_of_retry_attempts` times (each time separated by `delay_of_retry_attempts`). | ||
By default, every 5 minutes, try 5 times to reconnect (every attempts is separated by 500 ms). It retries `forever`. | ||
By default, every 5 minutes, try 5 times to reconnect (every attempt is separated by 500 ms). It retries `forever`. | ||
`* 2.0 breaking change`: by default do not allow to start the service without a redis connection; to have the previous behaviour set `allow_to_start_without_connection`:`true` | ||
@@ -47,8 +49,33 @@ ## Install | ||
}); | ||
redisClient.on("end", function () { | ||
console.log("redis connection has closed"); | ||
}); | ||
redisClient.on("reconnecting", function (o) { | ||
console.log("redis client reconnecting", o.attempt, o.delay); | ||
}); | ||
``` | ||
## Options | ||
Accepts an options object as a parameter with 3 possible keys: | ||
It accepts options object as a parameter with 4 possible keys: | ||
#### allow_to_start_without_connection `type boolean` | ||
Default: `false` | ||
2 possible scenarios: | ||
- `false` if there is no connection when the service starts, end reconnecting throwing an error and flush all commands | ||
- `true` allow to start the service without a redis connection | ||
```js | ||
var redis = require("redis"); | ||
var retryStrategy = require("node-redis-retry-strategy"); | ||
var client = redis.createClient({ | ||
host: "127.0.0.1", | ||
port: 6379, | ||
retry_strategy: retryStrategy({ | ||
allow_to_start_without_connection: true | ||
}) | ||
}); | ||
module.exports = client; | ||
``` | ||
#### number_of_retry_attempts `type number` ms | ||
Default: `5` | ||
The number of attempts separated by the `delay_of_retry_attempts`. If set to 0, it end reconnecting with the built in error. | ||
The number of attempts separated by the `delay_of_retry_attempts`. If set to 0, it ends reconnecting with the built in error. | ||
```js | ||
@@ -55,0 +82,0 @@ var redis = require("redis"); |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
8537
50
129
4