File tree 1 file changed +24
-7
lines changed
1 file changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,8 @@ if (!process.env.REDIS_URL) {
4
4
console . error ( "REDIS_URL not set" ) ;
5
5
}
6
6
7
- let client = null ;
7
+ /** @type {Record<string, ReturnType<typeof createClient>> } */
8
+ let clients = { } ;
8
9
9
10
/**
10
11
* @param {import("redis").RedisClientOptions } [options]
@@ -20,7 +21,13 @@ export default async function getClient(options) {
20
21
options ,
21
22
) ;
22
23
23
- if ( client && client . options ?. url === options . url ) {
24
+ if ( ! options . url ) {
25
+ throw new Error ( "You must pass a URL to connect" ) ;
26
+ }
27
+
28
+ let client = clients [ options . url ] ;
29
+
30
+ if ( client ) {
24
31
return client ;
25
32
}
26
33
@@ -29,18 +36,28 @@ export default async function getClient(options) {
29
36
client
30
37
. on ( "error" , ( err ) => {
31
38
console . error ( "Redis Client Error" , err ) ;
32
- void refreshClient ( ) ;
39
+ void refreshClient ( client ) ;
33
40
} )
34
41
. connect ( ) ;
35
42
43
+ clients [ options . url ] = client ;
44
+
36
45
return client ;
37
46
}
38
47
39
- async function refreshClient ( ) {
48
+ /**
49
+ * @param {ReturnType<typeof createClient> } client
50
+ */
51
+ async function refreshClient ( client ) {
40
52
if ( client ) {
53
+ const options = client . options ;
54
+
55
+ if ( options ?. url ) {
56
+ delete clients [ options ?. url ] ;
57
+ }
58
+
41
59
await client . disconnect ( ) ;
42
- client = null ;
43
- }
44
60
45
- client = await getClient ( ) ;
61
+ await getClient ( options ) ;
62
+ }
46
63
}
You can’t perform that action at this time.
0 commit comments