Skip to content

Commit 77ef42c

Browse files
authored
feat: add support for node-redis cluster (#495)
Support for Redis cluster was added in `[email protected]`. Related: #494
1 parent b5da02d commit 77ef42c

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

lib/index.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ export class RedisAdapter extends Adapter {
900900
this.pubClient.constructor.name === "Cluster" ||
901901
this.pubClient.isCluster
902902
) {
903-
// Cluster
903+
// ioredis cluster
904904
const nodes = this.pubClient.nodes();
905905
return Promise.all(
906906
nodes.map((node) =>
@@ -914,11 +914,32 @@ export class RedisAdapter extends Adapter {
914914
return numSub;
915915
});
916916
} else if (typeof this.pubClient.pSubscribe === "function") {
917-
return this.pubClient
918-
.sendCommand(["pubsub", "numsub", this.requestChannel])
919-
.then((res) => parseInt(res[1], 10));
917+
// node-redis client
918+
const isCluster = Array.isArray(this.pubClient.masters);
919+
if (isCluster) {
920+
const nodes = this.pubClient.masters;
921+
return Promise.all(
922+
nodes.map((node) => {
923+
return node.client.sendCommand([
924+
"pubsub",
925+
"numsub",
926+
this.requestChannel,
927+
]);
928+
})
929+
).then((values) => {
930+
let numSub = 0;
931+
values.map((value) => {
932+
numSub += parseInt(value[1], 10);
933+
});
934+
return numSub;
935+
});
936+
} else {
937+
return this.pubClient
938+
.sendCommand(["pubsub", "numsub", this.requestChannel])
939+
.then((res) => parseInt(res[1], 10));
940+
}
920941
} else {
921-
// RedisClient or Redis
942+
// ioredis or node-redis v3 client
922943
return new Promise((resolve, reject) => {
923944
this.pubClient.send_command(
924945
"pubsub",

0 commit comments

Comments
 (0)