@hastehaul/common 2.0.47 → 2.0.48
Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ export declare class RedisClient {
|
|
4
4
|
private static instance;
|
5
5
|
private constructor();
|
6
6
|
get client(): RedisClient;
|
7
|
-
static connect(masterHost: string, replicaHosts: string[]): RedisClient
|
7
|
+
static connect(masterHost: string, replicaHosts: string[]): Promise<RedisClient>;
|
8
8
|
private executeMasterOperation;
|
9
9
|
private executeReplicaOperation;
|
10
10
|
private executeOperation;
|
@@ -38,11 +38,31 @@ class RedisClient {
|
|
38
38
|
constructor(masterHost, replicaHosts) {
|
39
39
|
this.master = new ioredis_1.default({ host: masterHost, port: 6379 });
|
40
40
|
this.replicas = replicaHosts.map(replicaHost => new ioredis_1.default({ host: replicaHost, port: 6379 }));
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
const masterConnected = new Promise((resolve) => {
|
42
|
+
this.master.on('connect', () => {
|
43
|
+
console.log('Master connected');
|
44
|
+
resolve();
|
45
|
+
});
|
46
|
+
this.master.on('error', (error) => {
|
47
|
+
console.log('Master connection error:', error.message);
|
48
|
+
resolve();
|
49
|
+
});
|
50
|
+
});
|
51
|
+
const replicaPromises = this.replicas.map((replica) => {
|
52
|
+
return new Promise((resolve) => {
|
53
|
+
replica.on('connect', () => {
|
54
|
+
console.log('Replica connected');
|
55
|
+
resolve();
|
56
|
+
});
|
57
|
+
replica.on('error', (error) => {
|
58
|
+
console.log('Replica connection error:', error.message);
|
59
|
+
resolve();
|
60
|
+
});
|
61
|
+
});
|
62
|
+
});
|
63
|
+
// Wait for all connections to be established
|
64
|
+
Promise.all([masterConnected, ...replicaPromises]).then(() => {
|
65
|
+
console.log('All connections established');
|
46
66
|
});
|
47
67
|
}
|
48
68
|
get client() {
|
@@ -51,10 +71,14 @@ class RedisClient {
|
|
51
71
|
return RedisClient.instance;
|
52
72
|
}
|
53
73
|
static connect(masterHost, replicaHosts) {
|
54
|
-
|
55
|
-
RedisClient.instance
|
56
|
-
|
57
|
-
|
74
|
+
return new Promise((resolve) => {
|
75
|
+
if (!RedisClient.instance) {
|
76
|
+
const redisClient = new RedisClient(masterHost, replicaHosts);
|
77
|
+
RedisClient.instance = redisClient;
|
78
|
+
resolve(redisClient);
|
79
|
+
}
|
80
|
+
resolve(RedisClient.instance);
|
81
|
+
});
|
58
82
|
}
|
59
83
|
executeMasterOperation(command, ...args) {
|
60
84
|
return __awaiter(this, void 0, void 0, function* () {
|