-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
62 lines (50 loc) · 2.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"use strict";
var defaults = require("./defaults");
module.exports = function init (moduleOptions) {
var redisExists = false;
if (moduleOptions) {
if (moduleOptions.hasOwnProperty("number_of_retry_attempts") && !isNaN(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)) {
defaults.DELAY_OF_RETRY_ATTEMPTS = moduleOptions.delay_of_retry_attempts;
}
if (moduleOptions.hasOwnProperty("wait_time") && !isNaN(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;
}
}
/**
* In case of Redis down, every WAIT_TIME try to connect for NUMBER_OF_RETRY_ATTEMPTS
* times (each time separated by DELAY_OF_RETRY_ATTEMPTS).
* It will try to connect to redis forever.
*
* @example every 5 min, try for 5 times
*
* @author Simone Sacchi
* @version 2019/05/08
*/
return function retryStrategy (options) {
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
return undefined;
}
if (!options.attempt) {
// if there is no attempt, try again
return defaults.DELAY_OF_RETRY_ATTEMPTS;
}
if ((options.attempt % (defaults.NUMBER_OF_RETRY_ATTEMPTS + 1)) === 0) {
return defaults.WAIT_TIME;
} else {
return defaults.DELAY_OF_RETRY_ATTEMPTS;
}
};
};