Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 13 additions & 5 deletions lib/DataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,18 @@ export default class DataHandler {
if (channel) {
this.redis.condition.subscriber.del(replyType, channel);
}

// The reply count only covers channels of the same kind as the
// command (SUNSUBSCRIBE reports remaining shard channels, and
// UNSUBSCRIBE/PUNSUBSCRIBE ignore shard channels), so it reaching
// zero does not mean the connection left subscriber mode. Drop the
// state only once every kind is empty.
if (this.redis.condition.subscriber.isEmpty()) {
this.redis.condition.subscriber = false;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
Comment thread
askalf marked this conversation as resolved.
Resolved
}
}

const count = reply[2] as string | Buffer | number;
if (Number(count) === 0) {
this.redis.condition.subscriber = false;
}

if (this.handleUnsolicitedUnsubscribe(replyType)) {
return;
Expand Down Expand Up @@ -317,10 +323,12 @@ export default class DataHandler {
if (channel) {
this.redis.condition.subscriber.del(replyType, channel);
}
const count = reply[2];
if (Number(count) === 0) {
// See the matching comment in `returnPush`: the count is per channel
// kind, so only an empty set means subscriber mode is over.
if (this.redis.condition.subscriber.isEmpty()) {
this.redis.condition.subscriber = false;
}
const count = reply[2];
if (this.handleUnsolicitedUnsubscribe(replyType)) {
break;
}
Expand Down
94 changes: 94 additions & 0 deletions test/unit/subscriberMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { expect } from "chai";
import MockServer, { pubSubReply } from "../helpers/mock_server";
import Redis from "../../lib/Redis";

// Redis counts shard channels separately from channels and patterns, so the
// count carried by an (S)UNSUBSCRIBE reply only describes channels of the same
// kind. Dropping the subscriber state on that count alone loses subscriptions
// of the other kinds that are still active on the connection.
const PROTOCOLS = [3, 2] as const;

PROTOCOLS.forEach((protocol, index) => {
describe(`subscriber mode across channel kinds (RESP${protocol})`, () => {
const port = 17940 + index;
let server: MockServer;
let redis: Redis;

beforeEach(() => {
server = new MockServer(port, (argv) => {
const name = String(argv[0]).toLowerCase();
if (name === "info") {
return "# Server\r\nredis_version:7.0.0\r\n";
}
// Each kind reports the remaining count for that kind only.
if (name === "subscribe" || name === "ssubscribe") {
return pubSubReply(protocol, name, argv[1], 1);
}
if (name === "sunsubscribe") {
return pubSubReply(protocol, "sunsubscribe", argv[1], 0);
}
if (name === "unsubscribe") {
return pubSubReply(protocol, "unsubscribe", argv[1], 0);
}
return "OK";
});
});

afterEach(() => {
if (redis && redis.status !== "end") {
redis.disconnect();
}
server.disconnect();
});

it("keeps the regular subscription after sunsubscribe", async () => {
redis = new Redis({ port, protocol });
await redis.subscribe("regular");
await redis.ssubscribe("shard");
await redis.sunsubscribe("shard");

const { subscriber } = redis.condition;
expect(subscriber).to.not.equal(false);
expect((subscriber as any).channels("subscribe")).to.eql(["regular"]);
expect((subscriber as any).channels("ssubscribe")).to.eql([]);
});

it("delivers messages on a channel still subscribed after sunsubscribe", (done) => {
redis = new Redis({ port, protocol });
redis.on("message", (channel, message) => {
expect(channel).to.eql("regular");
expect(message).to.eql("hi");
done();
});
(async () => {
await redis.subscribe("regular");
await redis.ssubscribe("shard");
await redis.sunsubscribe("shard");
server.broadcast(pubSubReply(protocol, "message", "regular", "hi"));
})();
});

it("keeps the shard subscription after unsubscribe", async () => {
redis = new Redis({ port, protocol });
await redis.ssubscribe("shard");
await redis.subscribe("regular");
await redis.unsubscribe("regular");

const { subscriber } = redis.condition;
expect(subscriber).to.not.equal(false);
expect((subscriber as any).channels("ssubscribe")).to.eql(["shard"]);
expect((subscriber as any).channels("subscribe")).to.eql([]);
});

it("leaves subscriber mode once every kind is unsubscribed", async () => {
redis = new Redis({ port, protocol });
await redis.subscribe("regular");
await redis.ssubscribe("shard");
await redis.unsubscribe("regular");
await redis.sunsubscribe("shard");

expect(redis.condition.subscriber).to.equal(false);
expect(redis.mode).to.equal("normal");
});
});
});