Skip to content

Commit d62fb94

Browse files
committed
fix: fixing client refreshing and storing
1 parent dcb4fcc commit d62fb94

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

server/redis.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ if (!process.env.REDIS_URL) {
44
console.error("REDIS_URL not set");
55
}
66

7-
let client = null;
7+
/** @type {Record<string, ReturnType<typeof createClient>>} */
8+
let clients = {};
89

910
/**
1011
* @param {import("redis").RedisClientOptions} [options]
@@ -20,7 +21,13 @@ export default async function getClient(options) {
2021
options,
2122
);
2223

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) {
2431
return client;
2532
}
2633

@@ -29,18 +36,28 @@ export default async function getClient(options) {
2936
client
3037
.on("error", (err) => {
3138
console.error("Redis Client Error", err);
32-
void refreshClient();
39+
void refreshClient(client);
3340
})
3441
.connect();
3542

43+
clients[options.url] = client;
44+
3645
return client;
3746
}
3847

39-
async function refreshClient() {
48+
/**
49+
* @param {ReturnType<typeof createClient>} client
50+
*/
51+
async function refreshClient(client) {
4052
if (client) {
53+
const options = client.options;
54+
55+
if (options?.url) {
56+
delete clients[options?.url];
57+
}
58+
4159
await client.disconnect();
42-
client = null;
43-
}
4460

45-
client = await getClient();
61+
await getClient(options);
62+
}
4663
}

0 commit comments

Comments
 (0)