eip-cloud-services 1.2.4 → 1.2.5
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 +122 -20
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,6 +8,45 @@ 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;
|
|
@@ -32,6 +71,20 @@ const applyPrefix = (value) => {
|
|
|
32
71
|
*/
|
|
33
72
|
const getClient = async ( clientId = 'main' ) => {
|
|
34
73
|
try {
|
|
74
|
+
if ( clients[ clientId ] ) {
|
|
75
|
+
const status = clients[ clientId ].status;
|
|
76
|
+
if ( status === 'end' || status === 'close' ) {
|
|
77
|
+
try {
|
|
78
|
+
clients[ clientId ].disconnect ();
|
|
79
|
+
}
|
|
80
|
+
catch ( _err ) {}
|
|
81
|
+
delete clients[ clientId ];
|
|
82
|
+
if ( clientId === 'main_sub' ) {
|
|
83
|
+
mainSubListenerAttached = false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
35
88
|
if ( !clients[ clientId ] ) {
|
|
36
89
|
let redisClient;
|
|
37
90
|
|
|
@@ -52,8 +105,8 @@ const getClient = async ( clientId = 'main' ) => {
|
|
|
52
105
|
redisClient.once ( 'error', reject );
|
|
53
106
|
} );
|
|
54
107
|
|
|
55
|
-
redisClient.on ( 'node error',
|
|
56
|
-
|
|
108
|
+
redisClient.on ( 'node error', error => {
|
|
109
|
+
logConnectionError ( clientId, error );
|
|
57
110
|
} );
|
|
58
111
|
}
|
|
59
112
|
else if ( config?.redis?.clusterEnabled && ( !Array.isArray ( config.redis.cluster ) || config.redis.cluster.length === 0 ) ) {
|
|
@@ -66,8 +119,7 @@ const getClient = async ( clientId = 'main' ) => {
|
|
|
66
119
|
} );
|
|
67
120
|
|
|
68
121
|
redisClient.on ( 'error', error => {
|
|
69
|
-
|
|
70
|
-
throw error;
|
|
122
|
+
logConnectionError ( clientId, error );
|
|
71
123
|
} );
|
|
72
124
|
|
|
73
125
|
await new Promise ( ( resolve, reject ) => {
|
|
@@ -77,6 +129,14 @@ const getClient = async ( clientId = 'main' ) => {
|
|
|
77
129
|
}
|
|
78
130
|
|
|
79
131
|
clients[ clientId ] = redisClient;
|
|
132
|
+
|
|
133
|
+
if ( clientId === 'main_sub' ) {
|
|
134
|
+
attachMainSubscriberListener ( redisClient );
|
|
135
|
+
const channels = [ ...subscriptionHandlers.keys () ];
|
|
136
|
+
if ( channels.length > 0 ) {
|
|
137
|
+
await redisClient.subscribe ( ...channels );
|
|
138
|
+
}
|
|
139
|
+
}
|
|
80
140
|
}
|
|
81
141
|
|
|
82
142
|
return clients[ clientId ];
|
|
@@ -88,8 +148,19 @@ const getClient = async ( clientId = 'main' ) => {
|
|
|
88
148
|
|
|
89
149
|
const deleteClient = async ( clientId ) => {
|
|
90
150
|
if ( clients[ clientId ] ){
|
|
91
|
-
|
|
151
|
+
try {
|
|
152
|
+
await clients[ clientId ].quit ();
|
|
153
|
+
}
|
|
154
|
+
catch ( _err ) {
|
|
155
|
+
try {
|
|
156
|
+
clients[ clientId ].disconnect ();
|
|
157
|
+
}
|
|
158
|
+
catch ( __err ) {}
|
|
159
|
+
}
|
|
92
160
|
delete clients[ clientId ];
|
|
161
|
+
if ( clientId === 'main_sub' ) {
|
|
162
|
+
mainSubListenerAttached = false;
|
|
163
|
+
}
|
|
93
164
|
}
|
|
94
165
|
};
|
|
95
166
|
|
|
@@ -731,7 +802,7 @@ exports.publish = async ( channel, message ) => {
|
|
|
731
802
|
try {
|
|
732
803
|
if ( typeof channel === 'string' && typeof message === 'string' ) {
|
|
733
804
|
const fullChannel = applyPrefix ( channel );
|
|
734
|
-
const client = await
|
|
805
|
+
const client = await exports.getPubClient ();
|
|
735
806
|
|
|
736
807
|
return client.publish ( fullChannel, message );
|
|
737
808
|
}
|
|
@@ -756,16 +827,21 @@ exports.subscribe = async ( channel, messageHandler ) => {
|
|
|
756
827
|
try {
|
|
757
828
|
if ( typeof channel === 'string' && typeof messageHandler === 'function' ) {
|
|
758
829
|
const fullChannel = applyPrefix ( channel );
|
|
759
|
-
const client = await
|
|
830
|
+
const client = await exports.getSubClient ();
|
|
831
|
+
attachMainSubscriberListener ( client );
|
|
760
832
|
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
833
|
+
if ( !subscriptionHandlers.has ( fullChannel ) ) {
|
|
834
|
+
subscriptionHandlers.set ( fullChannel, new Set () );
|
|
835
|
+
try {
|
|
836
|
+
await client.subscribe ( fullChannel );
|
|
764
837
|
}
|
|
765
|
-
|
|
838
|
+
catch ( error ) {
|
|
839
|
+
subscriptionHandlers.delete ( fullChannel );
|
|
840
|
+
throw error;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
subscriptionHandlers.get ( fullChannel ).add ( messageHandler );
|
|
766
844
|
|
|
767
|
-
await client.subscribe ( fullChannel );
|
|
768
|
-
|
|
769
845
|
return;
|
|
770
846
|
}
|
|
771
847
|
else {
|
|
@@ -781,21 +857,32 @@ exports.subscribe = async ( channel, messageHandler ) => {
|
|
|
781
857
|
/**
|
|
782
858
|
* Unsubscribes from a channel.
|
|
783
859
|
* @param {string} channel - The channel to unsubscribe from.
|
|
860
|
+
* @param {Function} [messageHandler] - Optional specific handler to unsubscribe.
|
|
784
861
|
* @returns {Promise<void>} - A promise that resolves when the unsubscribe is complete.
|
|
785
862
|
*
|
|
786
863
|
* @description This method unsubscribes from the specified channel in Redis for incoming messages.
|
|
787
864
|
*/
|
|
788
|
-
exports.unsubscribe = async ( channel ) => {
|
|
865
|
+
exports.unsubscribe = async ( channel, messageHandler ) => {
|
|
789
866
|
try {
|
|
790
867
|
if ( typeof channel === 'string' ) {
|
|
791
868
|
const fullChannel = applyPrefix ( channel );
|
|
792
|
-
const
|
|
793
|
-
|
|
794
|
-
|
|
869
|
+
const handlers = subscriptionHandlers.get ( fullChannel );
|
|
870
|
+
if ( !handlers ) {
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
795
873
|
|
|
796
|
-
|
|
797
|
-
|
|
874
|
+
if ( typeof messageHandler === 'function' ) {
|
|
875
|
+
handlers.delete ( messageHandler );
|
|
876
|
+
}
|
|
877
|
+
else {
|
|
878
|
+
handlers.clear ();
|
|
879
|
+
}
|
|
798
880
|
|
|
881
|
+
if ( handlers.size === 0 ) {
|
|
882
|
+
subscriptionHandlers.delete ( fullChannel );
|
|
883
|
+
const client = await exports.getSubClient ();
|
|
884
|
+
await client.unsubscribe ( fullChannel );
|
|
885
|
+
}
|
|
799
886
|
return;
|
|
800
887
|
}
|
|
801
888
|
else {
|
|
@@ -833,5 +920,20 @@ exports.info = async () => {
|
|
|
833
920
|
* @description This method closes the Redis connection using the 'quit' command, ensuring a graceful shutdown of the connection.
|
|
834
921
|
*/
|
|
835
922
|
exports.kill = async () => {
|
|
836
|
-
await Promise.all (
|
|
923
|
+
await Promise.all (
|
|
924
|
+
Object.entries ( clients ).map ( async ( [ clientId, client ] ) => {
|
|
925
|
+
try {
|
|
926
|
+
await client.quit ();
|
|
927
|
+
}
|
|
928
|
+
catch ( _err ) {
|
|
929
|
+
try {
|
|
930
|
+
client.disconnect ();
|
|
931
|
+
}
|
|
932
|
+
catch ( __err ) {}
|
|
933
|
+
}
|
|
934
|
+
delete clients[ clientId ];
|
|
935
|
+
} )
|
|
936
|
+
);
|
|
937
|
+
subscriptionHandlers.clear ();
|
|
938
|
+
mainSubListenerAttached = false;
|
|
837
939
|
};
|