Skip to content

Commit 8669149

Browse files
authored
refactor(core): optimize redis error handling (#5965)
1 parent 5391dbc commit 8669149

File tree

3 files changed

+160
-100
lines changed

3 files changed

+160
-100
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"pg-protocol": "^1.6.0",
8585
"pluralize": "^8.0.0",
8686
"qrcode": "^1.5.3",
87-
"redis": "^4.6.5",
87+
"redis": "^4.6.14",
8888
"roarr": "^7.11.0",
8989
"samlify": "2.8.11",
9090
"semver": "^7.3.8",

packages/core/src/caches/index.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ abstract class RedisCacheBase implements CacheStore {
2828

2929
async connect() {
3030
if (this.client) {
31-
await this.client.connect();
32-
const pong = await this.ping();
31+
try {
32+
await this.client.connect();
33+
const pong = await this.ping();
3334

34-
if (pong === 'PONG') {
35-
cacheConsole.info('Connected to Redis');
36-
return;
35+
if (pong === 'PONG') {
36+
cacheConsole.info('Connected to Redis');
37+
return;
38+
}
39+
} catch (error: unknown) {
40+
cacheConsole.error(error);
3741
}
3842
}
3943
cacheConsole.warn('No Redis client initialized, skipping');
@@ -46,14 +50,14 @@ abstract class RedisCacheBase implements CacheStore {
4650
}
4751
}
4852

49-
protected getSocketOptions(url: URL) {
50-
const certFile = url.searchParams.get('cert');
51-
const keyFile = url.searchParams.get('key');
52-
const caFile = url.searchParams.get('certroot');
53+
protected getSocketOptions(url?: URL) {
54+
const certFile = url?.searchParams.get('cert');
55+
const keyFile = url?.searchParams.get('key');
56+
const caFile = url?.searchParams.get('certroot');
5357

5458
return {
55-
rejectUnauthorized: yes(url.searchParams.get('reject_unauthorized')),
56-
tls: url.protocol === 'rediss',
59+
rejectUnauthorized: yes(url?.searchParams.get('reject_unauthorized')),
60+
tls: url?.protocol === 'rediss',
5761
cert: certFile ? fs.readFileSync(certFile).toString() : undefined,
5862
key: keyFile ? fs.readFileSync(keyFile).toString() : undefined,
5963
ca: caFile ? fs.readFileSync(caFile).toString() : undefined,
@@ -64,7 +68,11 @@ abstract class RedisCacheBase implements CacheStore {
6468
return false;
6569
}
6670

67-
return Math.min(retries * 50, 500);
71+
if (retries > 5) {
72+
return new Error('Too many retries');
73+
}
74+
75+
return retries * 500;
6876
},
6977
};
7078
}
@@ -81,11 +89,12 @@ export class RedisCache extends RedisCacheBase {
8189
if (redisUrl) {
8290
this.client = createClient({
8391
url: conditional(!yes(redisUrl) && redisUrl),
84-
socket: trySafe(() => this.getSocketOptions(new URL(redisUrl))),
92+
socket: this.getSocketOptions(trySafe(() => new URL(redisUrl))),
8593
});
8694

8795
this.client.on('error', (error) => {
8896
void appInsights.trackException(error);
97+
cacheConsole.error(error);
8998
});
9099
}
91100
}
@@ -127,6 +136,7 @@ export class RedisClusterCache extends RedisCacheBase {
127136

128137
this.client.on('error', (error) => {
129138
void appInsights.trackException(error);
139+
cacheConsole.error(error);
130140
});
131141
}
132142

0 commit comments

Comments
 (0)