Skip to content

Subscribe only to known room to reduce redis traffic #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,25 @@ export class RedisAdapter extends Adapter {

const isRedisV4 = typeof this.pubClient.pSubscribe === "function";
if (isRedisV4) {
this.subClient.pSubscribe(
this.channel + "*",
this.subClient.subscribe(
[this.channel],
(msg, channel) => {
this.onmessage(null, channel, msg);
},
true
);
this.on("create-room", (room) => {
this.subClient.subscribe(
this.channel + room + "#",
(msg, channel) => {
this.onmessage(null, channel, msg);
},
true
);
});
this.on("delete-room", (room) => {
this.subClient.unsubscribe(this.channel + room + "#");
});
this.subClient.subscribe(
[this.requestChannel, this.responseChannel, specificResponseChannel],
(msg, channel) => {
Expand All @@ -152,15 +164,25 @@ export class RedisAdapter extends Adapter {
true
);
} else {
this.subClient.psubscribe(this.channel + "*");
this.subClient.on("pmessageBuffer", this.onmessage.bind(this));
this.subClient.subscribe(this.channel);
this.on("create-room", (room) => {
this.subClient.subscribe(this.channel + room + "#");
});
this.on("delete-room", (room) => {
this.subClient.unsubscribe(this.channel + room + "#");
});

this.subClient.subscribe([
this.requestChannel,
this.responseChannel,
specificResponseChannel,
]);
this.subClient.on("messageBuffer", this.onrequest.bind(this));
this.subClient.on("messageBuffer", async (channel, msg) => {
if (channel.toString().startsWith(this.channel)) {
return this.onmessage(null, channel, msg);
}
return await this.onrequest(channel, msg);
});
}

const registerFriendlyErrorHandler = (redisClient) => {
Expand All @@ -185,7 +207,7 @@ export class RedisAdapter extends Adapter {

const channelMatches = channel.startsWith(this.channel);
if (!channelMatches) {
return debug("ignore different channel");
return debug(`ignore different channel (onmessage): ${channel}`);
}

const room = channel.slice(this.channel.length, -1);
Expand Down Expand Up @@ -228,7 +250,7 @@ export class RedisAdapter extends Adapter {
if (channel.startsWith(this.responseChannel)) {
return this.onresponse(channel, msg);
} else if (!channel.startsWith(this.requestChannel)) {
return debug("ignore different channel");
return debug(`ignore different channel (onrequest): ${channel}`);
}

let request;
Expand Down