@mereb/shared-packages 0.0.10 → 0.0.11
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/dist/cache/redis.d.ts +1 -0
- package/dist/cache/redis.d.ts.map +1 -1
- package/dist/cache/redis.js +39 -1
- package/package.json +1 -1
package/dist/cache/redis.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type RedisClientType } from '@redis/client';
|
|
2
2
|
export interface RedisOptions {
|
|
3
3
|
url: string;
|
|
4
|
+
connectTimeoutMs?: number;
|
|
4
5
|
}
|
|
5
6
|
export declare function getRedisClient(options: RedisOptions): Promise<RedisClientType>;
|
|
6
7
|
export declare function disconnectRedis(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis.d.ts","sourceRoot":"","sources":["../../src/cache/redis.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"redis.d.ts","sourceRoot":"","sources":["../../src/cache/redis.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;AAQnE,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAiCD,wBAAsB,cAAc,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,eAAe,CAAC,CAepF;AAED,wBAAsB,eAAe,kBAKpC"}
|
package/dist/cache/redis.js
CHANGED
|
@@ -8,14 +8,52 @@ exports.disconnectRedis = disconnectRedis;
|
|
|
8
8
|
const client_1 = require("@redis/client");
|
|
9
9
|
const pino_1 = __importDefault(require("pino"));
|
|
10
10
|
const logger = (0, pino_1.default)({ name: 'redis' });
|
|
11
|
+
const DEFAULT_CONNECT_TIMEOUT_MS = 5_000;
|
|
11
12
|
let client;
|
|
13
|
+
function withTimeout(promise, timeoutMs) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
const timer = setTimeout(() => {
|
|
16
|
+
reject(new Error(`Timed out connecting to Redis after ${timeoutMs}ms`));
|
|
17
|
+
}, timeoutMs);
|
|
18
|
+
promise
|
|
19
|
+
.then((value) => {
|
|
20
|
+
clearTimeout(timer);
|
|
21
|
+
resolve(value);
|
|
22
|
+
})
|
|
23
|
+
.catch((error) => {
|
|
24
|
+
clearTimeout(timer);
|
|
25
|
+
reject(error);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async function connectClient(instance, timeoutMs) {
|
|
30
|
+
try {
|
|
31
|
+
await withTimeout(instance.connect(), timeoutMs);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
try {
|
|
35
|
+
await instance.disconnect();
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// ignore disconnect errors during cleanup
|
|
39
|
+
}
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
12
43
|
async function getRedisClient(options) {
|
|
13
44
|
if (!client) {
|
|
45
|
+
const timeoutMs = options.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS;
|
|
14
46
|
client = (0, client_1.createClient)({ url: options.url });
|
|
15
47
|
client.on('error', (err) => {
|
|
16
48
|
logger.error({ err }, 'Redis connection error');
|
|
17
49
|
});
|
|
18
|
-
|
|
50
|
+
try {
|
|
51
|
+
await connectClient(client, timeoutMs);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
client = undefined;
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
19
57
|
}
|
|
20
58
|
return client;
|
|
21
59
|
}
|