eip-cloud-services 1.2.4 → 1.2.6
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/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/src/redis.js +147 -22
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
|
|
4
4
|
All notable changes to this project will be documented in this file.
|
|
5
5
|
|
|
6
|
+
## [1.2.5] - 2026-03-02
|
|
7
|
+
|
|
8
|
+
### Fixed
|
|
9
|
+
- Hardened Redis error handling to avoid throwing from client error events, reducing process instability during transient disconnects.
|
|
10
|
+
- Reworked pub/sub client usage to use shared publisher/subscriber clients instead of per-channel client fan-out.
|
|
11
|
+
- Improved subscription lifecycle management by tracking channel handlers and unsubscribing only when no handlers remain.
|
|
12
|
+
- Improved Redis client cleanup and stale-client recovery behavior.
|
|
13
|
+
|
|
6
14
|
## [1.1.0] - 2023-11-18
|
|
7
15
|
|
|
8
16
|
### Changed
|
package/package.json
CHANGED
package/src/redis.js
CHANGED
|
@@ -8,12 +8,71 @@ if ( fs.existsSync ( configDirPath ) && fs.statSync ( configDirPath ).isDirector
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
const clients = {};
|
|
11
|
+
const subscriptionHandlers = new Map ();
|
|
12
|
+
let mainSubListenerAttached = false;
|
|
13
|
+
const lastConnectionErrorByClient = new Map ();
|
|
14
|
+
const CONNECTION_ERROR_LOG_WINDOW_MS = 30000;
|
|
15
|
+
|
|
16
|
+
const logConnectionError = ( clientId, error ) => {
|
|
17
|
+
const now = Date.now ();
|
|
18
|
+
const last = lastConnectionErrorByClient.get ( clientId ) || 0;
|
|
19
|
+
if ( now - last < CONNECTION_ERROR_LOG_WINDOW_MS ) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
lastConnectionErrorByClient.set ( clientId, now );
|
|
23
|
+
console.error ( '\x1b[33mREDIS CONNECTION FAILED: Redis connection failed. If you\'re running locally, is a redis server actually active? Also, check your connection configuration.\x1b[0m', {
|
|
24
|
+
clientId,
|
|
25
|
+
error: error?.message || error
|
|
26
|
+
} );
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const attachMainSubscriberListener = ( client ) => {
|
|
30
|
+
if ( mainSubListenerAttached ) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
client.on ( 'message', ( receivedChannel, message ) => {
|
|
34
|
+
const handlers = subscriptionHandlers.get ( receivedChannel );
|
|
35
|
+
if ( !handlers || handlers.size === 0 ) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
handlers.forEach ( handler => {
|
|
39
|
+
try {
|
|
40
|
+
handler ( message );
|
|
41
|
+
}
|
|
42
|
+
catch ( error ) {
|
|
43
|
+
console.error ( 'REDIS LIB ERROR [subscribe handler]', error );
|
|
44
|
+
}
|
|
45
|
+
} );
|
|
46
|
+
} );
|
|
47
|
+
mainSubListenerAttached = true;
|
|
48
|
+
};
|
|
49
|
+
|
|
11
50
|
const applyPrefix = (value) => {
|
|
12
51
|
const prefix = config?.redis?.prefix;
|
|
13
52
|
if (!prefix || typeof value !== 'string') return value;
|
|
14
53
|
return value.startsWith(prefix) ? value : `${prefix}${value}`;
|
|
15
54
|
};
|
|
16
55
|
|
|
56
|
+
const toPlainObject = ( value ) => {
|
|
57
|
+
if ( value && typeof value === 'object' && !Array.isArray ( value ) ) {
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
return {};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const getRedisOptionsForClient = ( clientId, { cluster = false } = {} ) => {
|
|
64
|
+
const baseOptions = toPlainObject ( config?.redis?.options );
|
|
65
|
+
const perClientOptions = toPlainObject ( config?.redis?.clientOptionsById?.[ clientId ] );
|
|
66
|
+
const mergedOptions = { ...baseOptions, ...perClientOptions };
|
|
67
|
+
|
|
68
|
+
// Preserve existing cluster behaviour (TLS by default) unless explicitly overridden.
|
|
69
|
+
if ( cluster && !Object.prototype.hasOwnProperty.call ( mergedOptions, 'tls' ) ) {
|
|
70
|
+
mergedOptions.tls = {};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return mergedOptions;
|
|
74
|
+
};
|
|
75
|
+
|
|
17
76
|
/**
|
|
18
77
|
* Creates or retrieves a Redis client instance based on a given client identifier.
|
|
19
78
|
* If the client does not exist, it creates a new one, either connecting to a Redis Cluster
|
|
@@ -32,6 +91,20 @@ const applyPrefix = (value) => {
|
|
|
32
91
|
*/
|
|
33
92
|
const getClient = async ( clientId = 'main' ) => {
|
|
34
93
|
try {
|
|
94
|
+
if ( clients[ clientId ] ) {
|
|
95
|
+
const status = clients[ clientId ].status;
|
|
96
|
+
if ( status === 'end' || status === 'close' ) {
|
|
97
|
+
try {
|
|
98
|
+
clients[ clientId ].disconnect ();
|
|
99
|
+
}
|
|
100
|
+
catch ( _err ) {}
|
|
101
|
+
delete clients[ clientId ];
|
|
102
|
+
if ( clientId === 'main_sub' ) {
|
|
103
|
+
mainSubListenerAttached = false;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
35
108
|
if ( !clients[ clientId ] ) {
|
|
36
109
|
let redisClient;
|
|
37
110
|
|
|
@@ -40,10 +113,11 @@ const getClient = async ( clientId = 'main' ) => {
|
|
|
40
113
|
host: node.host,
|
|
41
114
|
port: node.port
|
|
42
115
|
} ) );
|
|
116
|
+
const redisOptions = getRedisOptionsForClient ( clientId, { cluster: true } );
|
|
43
117
|
|
|
44
118
|
redisClient = new Redis.Cluster ( clusterNodes, {
|
|
45
119
|
dnsLookup: (address, callback) => callback(null, address),
|
|
46
|
-
redisOptions
|
|
120
|
+
redisOptions
|
|
47
121
|
} );
|
|
48
122
|
|
|
49
123
|
// Await until the cluster is ready
|
|
@@ -52,22 +126,23 @@ const getClient = async ( clientId = 'main' ) => {
|
|
|
52
126
|
redisClient.once ( 'error', reject );
|
|
53
127
|
} );
|
|
54
128
|
|
|
55
|
-
redisClient.on ( 'node error',
|
|
56
|
-
|
|
129
|
+
redisClient.on ( 'node error', error => {
|
|
130
|
+
logConnectionError ( clientId, error );
|
|
57
131
|
} );
|
|
58
132
|
}
|
|
59
133
|
else if ( config?.redis?.clusterEnabled && ( !Array.isArray ( config.redis.cluster ) || config.redis.cluster.length === 0 ) ) {
|
|
60
134
|
throw new Error ( 'Redis Cluster is enabled but there were no cluster nodes defined in config.redis.cluster' );
|
|
61
135
|
}
|
|
62
136
|
else {
|
|
137
|
+
const redisOptions = getRedisOptionsForClient ( clientId );
|
|
63
138
|
redisClient = new Redis ( {
|
|
64
139
|
host: config?.redis?.host,
|
|
65
|
-
port: config?.redis?.port
|
|
140
|
+
port: config?.redis?.port,
|
|
141
|
+
...redisOptions
|
|
66
142
|
} );
|
|
67
143
|
|
|
68
144
|
redisClient.on ( 'error', error => {
|
|
69
|
-
|
|
70
|
-
throw error;
|
|
145
|
+
logConnectionError ( clientId, error );
|
|
71
146
|
} );
|
|
72
147
|
|
|
73
148
|
await new Promise ( ( resolve, reject ) => {
|
|
@@ -77,6 +152,14 @@ const getClient = async ( clientId = 'main' ) => {
|
|
|
77
152
|
}
|
|
78
153
|
|
|
79
154
|
clients[ clientId ] = redisClient;
|
|
155
|
+
|
|
156
|
+
if ( clientId === 'main_sub' ) {
|
|
157
|
+
attachMainSubscriberListener ( redisClient );
|
|
158
|
+
const channels = [ ...subscriptionHandlers.keys () ];
|
|
159
|
+
if ( channels.length > 0 ) {
|
|
160
|
+
await redisClient.subscribe ( ...channels );
|
|
161
|
+
}
|
|
162
|
+
}
|
|
80
163
|
}
|
|
81
164
|
|
|
82
165
|
return clients[ clientId ];
|
|
@@ -88,8 +171,19 @@ const getClient = async ( clientId = 'main' ) => {
|
|
|
88
171
|
|
|
89
172
|
const deleteClient = async ( clientId ) => {
|
|
90
173
|
if ( clients[ clientId ] ){
|
|
91
|
-
|
|
174
|
+
try {
|
|
175
|
+
await clients[ clientId ].quit ();
|
|
176
|
+
}
|
|
177
|
+
catch ( _err ) {
|
|
178
|
+
try {
|
|
179
|
+
clients[ clientId ].disconnect ();
|
|
180
|
+
}
|
|
181
|
+
catch ( __err ) {}
|
|
182
|
+
}
|
|
92
183
|
delete clients[ clientId ];
|
|
184
|
+
if ( clientId === 'main_sub' ) {
|
|
185
|
+
mainSubListenerAttached = false;
|
|
186
|
+
}
|
|
93
187
|
}
|
|
94
188
|
};
|
|
95
189
|
|
|
@@ -731,7 +825,7 @@ exports.publish = async ( channel, message ) => {
|
|
|
731
825
|
try {
|
|
732
826
|
if ( typeof channel === 'string' && typeof message === 'string' ) {
|
|
733
827
|
const fullChannel = applyPrefix ( channel );
|
|
734
|
-
const client = await
|
|
828
|
+
const client = await exports.getPubClient ();
|
|
735
829
|
|
|
736
830
|
return client.publish ( fullChannel, message );
|
|
737
831
|
}
|
|
@@ -756,16 +850,21 @@ exports.subscribe = async ( channel, messageHandler ) => {
|
|
|
756
850
|
try {
|
|
757
851
|
if ( typeof channel === 'string' && typeof messageHandler === 'function' ) {
|
|
758
852
|
const fullChannel = applyPrefix ( channel );
|
|
759
|
-
const client = await
|
|
853
|
+
const client = await exports.getSubClient ();
|
|
854
|
+
attachMainSubscriberListener ( client );
|
|
760
855
|
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
856
|
+
if ( !subscriptionHandlers.has ( fullChannel ) ) {
|
|
857
|
+
subscriptionHandlers.set ( fullChannel, new Set () );
|
|
858
|
+
try {
|
|
859
|
+
await client.subscribe ( fullChannel );
|
|
764
860
|
}
|
|
765
|
-
|
|
861
|
+
catch ( error ) {
|
|
862
|
+
subscriptionHandlers.delete ( fullChannel );
|
|
863
|
+
throw error;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
subscriptionHandlers.get ( fullChannel ).add ( messageHandler );
|
|
766
867
|
|
|
767
|
-
await client.subscribe ( fullChannel );
|
|
768
|
-
|
|
769
868
|
return;
|
|
770
869
|
}
|
|
771
870
|
else {
|
|
@@ -781,21 +880,32 @@ exports.subscribe = async ( channel, messageHandler ) => {
|
|
|
781
880
|
/**
|
|
782
881
|
* Unsubscribes from a channel.
|
|
783
882
|
* @param {string} channel - The channel to unsubscribe from.
|
|
883
|
+
* @param {Function} [messageHandler] - Optional specific handler to unsubscribe.
|
|
784
884
|
* @returns {Promise<void>} - A promise that resolves when the unsubscribe is complete.
|
|
785
885
|
*
|
|
786
886
|
* @description This method unsubscribes from the specified channel in Redis for incoming messages.
|
|
787
887
|
*/
|
|
788
|
-
exports.unsubscribe = async ( channel ) => {
|
|
888
|
+
exports.unsubscribe = async ( channel, messageHandler ) => {
|
|
789
889
|
try {
|
|
790
890
|
if ( typeof channel === 'string' ) {
|
|
791
891
|
const fullChannel = applyPrefix ( channel );
|
|
792
|
-
const
|
|
793
|
-
|
|
794
|
-
|
|
892
|
+
const handlers = subscriptionHandlers.get ( fullChannel );
|
|
893
|
+
if ( !handlers ) {
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
795
896
|
|
|
796
|
-
|
|
797
|
-
|
|
897
|
+
if ( typeof messageHandler === 'function' ) {
|
|
898
|
+
handlers.delete ( messageHandler );
|
|
899
|
+
}
|
|
900
|
+
else {
|
|
901
|
+
handlers.clear ();
|
|
902
|
+
}
|
|
798
903
|
|
|
904
|
+
if ( handlers.size === 0 ) {
|
|
905
|
+
subscriptionHandlers.delete ( fullChannel );
|
|
906
|
+
const client = await exports.getSubClient ();
|
|
907
|
+
await client.unsubscribe ( fullChannel );
|
|
908
|
+
}
|
|
799
909
|
return;
|
|
800
910
|
}
|
|
801
911
|
else {
|
|
@@ -833,5 +943,20 @@ exports.info = async () => {
|
|
|
833
943
|
* @description This method closes the Redis connection using the 'quit' command, ensuring a graceful shutdown of the connection.
|
|
834
944
|
*/
|
|
835
945
|
exports.kill = async () => {
|
|
836
|
-
await Promise.all (
|
|
946
|
+
await Promise.all (
|
|
947
|
+
Object.entries ( clients ).map ( async ( [ clientId, client ] ) => {
|
|
948
|
+
try {
|
|
949
|
+
await client.quit ();
|
|
950
|
+
}
|
|
951
|
+
catch ( _err ) {
|
|
952
|
+
try {
|
|
953
|
+
client.disconnect ();
|
|
954
|
+
}
|
|
955
|
+
catch ( __err ) {}
|
|
956
|
+
}
|
|
957
|
+
delete clients[ clientId ];
|
|
958
|
+
} )
|
|
959
|
+
);
|
|
960
|
+
subscriptionHandlers.clear ();
|
|
961
|
+
mainSubListenerAttached = false;
|
|
837
962
|
};
|