@@ -18,6 +18,10 @@ const DISCONNECTED = 'disconnected';
18
18
async function connect ( body ) {
19
19
// already exist
20
20
if ( redisInstanceCache [ body . id ] ) {
21
+ const redisConn = redisInstanceCache [ body . id ] ;
22
+ if ( redisConn . status == DISCONNECTED ) {
23
+ redisConn . redis . connect ( ) ;
24
+ }
21
25
return Promise . resolve ( body ) ;
22
26
}
23
27
@@ -26,22 +30,26 @@ async function connect(body) {
26
30
host : body . serverModel . ip , port : body . serverModel . port , db : body . serverModel . db ,
27
31
password : body . serverModel . password ,
28
32
showFriendlyErrorStack : true ,
33
+ maxRetriesPerRequest : 10 ,
29
34
} ) ;
35
+
36
+ body . serverModel . id = body . id ;
37
+
30
38
const timeoutHandler = setTimeout ( ( ) => {
31
39
redis . disconnect ( ) ;
32
- reject ( errors . newConnectTimeoutError ) ;
40
+ reject ( errors . newConnectTimeoutError ( body . serverModel ) ) ;
33
41
} , 3 * 1000 ) ;
34
42
35
43
redis . on ( 'error' , ( e ) => {
36
44
console . error ( e ) ;
37
45
body . status = DISCONNECTED ;
38
- redisInstanceCache [ body . id ] = null ;
39
-
40
46
} ) ;
41
47
42
48
redis . on ( 'ready' , ( ) => {
43
- redisInstanceCache [ body . id ] = redis ;
49
+ body . redis = redis ;
44
50
body . status = CONNECTED ;
51
+ redisInstanceCache [ body . id ] = body ;
52
+
45
53
resolve ( body ) ;
46
54
clearTimeout ( timeoutHandler ) ;
47
55
} ) ;
@@ -53,20 +61,21 @@ async function connect(body) {
53
61
*
54
62
* @param query the query params
55
63
*/
56
- function getRedis ( query ) {
57
- const redis = redisInstanceCache [ query . id ] ;
58
- if ( ! redis && redis . status !== CONNECTED ) {
59
- throw errors . newConnectFailedError ;
64
+ function getRedisConnection ( query ) {
65
+ const redisConn = redisInstanceCache [ query . id ] ;
66
+ if ( ! redisConn && redisConn . status !== CONNECTED ) {
67
+ throw errors . newConnectFailedError ( redisConn ? redisConn . serverModel : { } ) ;
60
68
}
61
- return redis ;
69
+ return redisConn ;
62
70
}
63
71
64
72
/**
65
73
* fetch the redis tree
66
74
* @param query the query params
67
75
*/
68
76
async function fetchTree ( query ) {
69
- const redis = getRedis ( query ) ;
77
+ const redisConn = getRedisConnection ( query ) ;
78
+ const redis = redisConn . redis ;
70
79
71
80
const root = { } ;
72
81
const lencommands = {
@@ -91,7 +100,7 @@ async function fetchTree(query) {
91
100
}
92
101
}
93
102
} catch ( e ) {
94
- throw errors . newReplyError ( e . message ) ;
103
+ throw errors . newReplyError ( redisConn . serverModel , e . message ) ;
95
104
}
96
105
97
106
const tree = { } ;
@@ -166,14 +175,15 @@ async function call(query, body) {
166
175
if ( ! lines || lines . length <= 0 ) {
167
176
return [ ] ;
168
177
}
169
- const redis = getRedis ( query ) ;
178
+ const redisConn = getRedisConnection ( query ) ;
179
+ const redis = redisConn . redis ;
170
180
171
181
const results = [ ] ;
172
182
for ( let i = 0 ; i < lines . length ; i ++ ) {
173
183
try {
174
184
results . push ( await redis . call ( ...lines [ i ] ) ) ;
175
185
} catch ( e ) {
176
- throw errors . newReplyError ( e . message , i + 1 ) ;
186
+ throw errors . newReplyError ( redisConn . serverModel , e . message , i + 1 ) ;
177
187
}
178
188
}
179
189
return results ;
@@ -185,12 +195,13 @@ async function call(query, body) {
185
195
* @return {Promise<*> }
186
196
*/
187
197
async function dump ( query ) {
188
- const redis = getRedis ( query ) ;
198
+ const redisConn = getRedisConnection ( query ) ;
199
+ const redis = redisConn . redis ;
189
200
190
201
try {
191
202
return await redisDump ( query . exportType , redis ) ;
192
203
} catch ( e ) {
193
- throw errors . newReplyError ( e . message ) ;
204
+ throw errors . newReplyError ( redisConn . serverModel , e . message ) ;
194
205
}
195
206
}
196
207
0 commit comments