@mimik/rediser 1.4.15 → 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.
Files changed (2) hide show
  1. package/index.js +65 -26
  2. package/package.json +3 -3
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;
@@ -69,22 +70,59 @@ module.exports = (set, config) => {
69
70
  else logger.info('creating a cache connection', { type, settings: redisSettings }, correlationId);
70
71
  displayCreate = false;
71
72
  }
72
- const client = redis.createClient({ url: redisSettings.url, ...redisSettings.options });
73
- client.connect();
74
-
73
+ let client;
74
+ const isClusterEnabled = redisSettings.cluster.set === 'on';
75
+ if (isClusterEnabled) {
76
+ const clusterConfig = {
77
+ rootNodes: redisSettings.domain.split(',').map((url) => ({ url: `redis://${url}` })),
78
+ useReplicas: redisSettings.cluster.useReplicas === 'on',
79
+ minimizeConnections: redisSettings.cluster.minimizeConnections === 'on',
80
+ };
81
+ if (redisSettings.username && redisSettings.password) {
82
+ clusterConfig.defaults = {
83
+ username: redisSettings.username,
84
+ password: redisSettings.password,
85
+ };
86
+ }
87
+ client = redis.createCluster(clusterConfig);
88
+ }
89
+ else {
90
+ client = redis.createClient({ url: redisSettings.url, ...redisSettings.options });
91
+ }
75
92
  client.on('error', (err) => {
76
93
  state = err;
77
- });
78
- client.on('reconnecting', disconnectHandler);
79
- client.on('ready', () => {
80
- state = CONNECTED;
81
- if (interval) {
82
- clearInterval(interval);
83
- interval = null;
84
- }
85
- });
86
- client.on('end', () => disconnectHandler);
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();
87
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
+ }
88
126
  return client;
89
127
  };
90
128
 
@@ -113,20 +151,21 @@ module.exports = (set, config) => {
113
151
  }
114
152
  }, config.redisSettings.connectTimeout * 1000); // convert in seconds
115
153
 
116
- return Promise.delay(redisSettings.validationCheck).then(() => {
117
- if (state !== DISCONNECTED && state !== CONNECTED) {
118
- const error = new Error(`cache connection not established: ${state}`);
154
+ return Promise.delay(redisSettings.validationCheck)
155
+ .then(() => {
156
+ if (state !== DISCONNECTED && state !== CONNECTED) {
157
+ const error = new Error(`cache connection not established: ${state}`);
119
158
 
120
- logger.error('cache connection error', { type, error }, correlationId);
121
- throw error;
122
- }
123
- if (state === DISCONNECTED) {
124
- return validate();
125
- }
126
- clearInterval(interval);
127
- logger.info('cache connection established', { type }, correlationId);
128
- return null;
129
- });
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
+ });
130
169
  };
131
170
 
132
171
  if (state !== CONNECTED) initializeSync();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mimik/rediser",
3
- "version": "1.4.15",
3
+ "version": "1.5.2",
4
4
  "description": "Helper for setting up redis using redis for mimik microservices",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,12 +32,12 @@
32
32
  "@mimik/lib-filters": "^1.4.8",
33
33
  "@mimik/sumologic-winston-logger": "^1.6.19",
34
34
  "bluebird": "3.7.2",
35
- "redis": "4.6.8"
35
+ "redis": "4.6.10"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@mimik/eslint-plugin-dependencies": "^2.4.5",
39
39
  "@mimik/eslint-plugin-document-env": "^1.0.5",
40
- "eslint": "8.48.0",
40
+ "eslint": "8.50.0",
41
41
  "eslint-config-airbnb": "19.0.4",
42
42
  "eslint-plugin-import": "2.28.1",
43
43
  "eslint-plugin-jsx-a11y": "6.7.1",