@mimik/rediser 1.5.1 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +49 -27
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -17,6 +17,7 @@ const DISCONNECTED = 2;
|
|
|
17
17
|
let displayCreate = true;
|
|
18
18
|
let displayDisconnect = true;
|
|
19
19
|
let state = DISCONNECTED;
|
|
20
|
+
let clusterConnectInterval;
|
|
20
21
|
|
|
21
22
|
module.exports = (set, config) => {
|
|
22
23
|
const { redisSettings } = config;
|
|
@@ -70,9 +71,10 @@ module.exports = (set, config) => {
|
|
|
70
71
|
displayCreate = false;
|
|
71
72
|
}
|
|
72
73
|
let client;
|
|
73
|
-
|
|
74
|
+
const isClusterEnabled = redisSettings.cluster.set === 'on';
|
|
75
|
+
if (isClusterEnabled) {
|
|
74
76
|
const clusterConfig = {
|
|
75
|
-
rootNodes: redisSettings.domain.split(',').map((url) => ({ url })),
|
|
77
|
+
rootNodes: redisSettings.domain.split(',').map((url) => ({ url: `redis://${url}` })),
|
|
76
78
|
useReplicas: redisSettings.cluster.useReplicas === 'on',
|
|
77
79
|
minimizeConnections: redisSettings.cluster.minimizeConnections === 'on',
|
|
78
80
|
};
|
|
@@ -87,21 +89,40 @@ module.exports = (set, config) => {
|
|
|
87
89
|
else {
|
|
88
90
|
client = redis.createClient({ url: redisSettings.url, ...redisSettings.options });
|
|
89
91
|
}
|
|
90
|
-
client.connect();
|
|
91
|
-
|
|
92
92
|
client.on('error', (err) => {
|
|
93
93
|
state = err;
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
94
|
+
})
|
|
95
|
+
.on('reconnecting', disconnectHandler)
|
|
96
|
+
.on('ready', () => {
|
|
97
|
+
state = CONNECTED;
|
|
98
|
+
if (interval) {
|
|
99
|
+
clearInterval(interval);
|
|
100
|
+
interval = null;
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
.on('end', () => disconnectHandler)
|
|
104
|
+
.connect();
|
|
104
105
|
|
|
106
|
+
// Todo: remove this if block, when redis library has events implemented for cluster connections
|
|
107
|
+
if (!clusterConnectInterval && isClusterEnabled) {
|
|
108
|
+
const connectionTestKey = 'b7ddc6b6-57a8-4cde-80a1-c6a8f174f386';
|
|
109
|
+
clusterConnectInterval = setInterval(() => {
|
|
110
|
+
if (state !== CONNECTED) {
|
|
111
|
+
client.set('connectionTestKey', connectionTestKey)
|
|
112
|
+
.then(() => client.get('connectionTestKey'))
|
|
113
|
+
.then((value) => {
|
|
114
|
+
if (value === connectionTestKey) state = CONNECTED;
|
|
115
|
+
})
|
|
116
|
+
.catch((error) => {
|
|
117
|
+
logger.error('connection not ready yet', { error }, correlationId);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
clearInterval(clusterConnectInterval);
|
|
122
|
+
clusterConnectInterval = null;
|
|
123
|
+
}
|
|
124
|
+
}, redisSettings.validationCheck);
|
|
125
|
+
}
|
|
105
126
|
return client;
|
|
106
127
|
};
|
|
107
128
|
|
|
@@ -130,20 +151,21 @@ module.exports = (set, config) => {
|
|
|
130
151
|
}
|
|
131
152
|
}, config.redisSettings.connectTimeout * 1000); // convert in seconds
|
|
132
153
|
|
|
133
|
-
return Promise.delay(redisSettings.validationCheck)
|
|
134
|
-
|
|
135
|
-
|
|
154
|
+
return Promise.delay(redisSettings.validationCheck)
|
|
155
|
+
.then(() => {
|
|
156
|
+
if (state !== DISCONNECTED && state !== CONNECTED) {
|
|
157
|
+
const error = new Error(`cache connection not established: ${state}`);
|
|
136
158
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
159
|
+
logger.error('cache connection error', { type, error }, correlationId);
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
if (state === DISCONNECTED) {
|
|
163
|
+
return validate();
|
|
164
|
+
}
|
|
165
|
+
clearInterval(interval);
|
|
166
|
+
logger.info('cache connection established', { type }, correlationId);
|
|
167
|
+
return null;
|
|
168
|
+
});
|
|
147
169
|
};
|
|
148
170
|
|
|
149
171
|
if (state !== CONNECTED) initializeSync();
|