diff --git a/lib/DataHandler.ts b/lib/DataHandler.ts index 75652dd45..413085fb3 100644 --- a/lib/DataHandler.ts +++ b/lib/DataHandler.ts @@ -9,7 +9,7 @@ import Deque = require("denque"); import { EventEmitter } from "events"; import Command from "./Command"; import { Debug } from "./utils"; -import SubscriptionSet from "./SubscriptionSet"; +import SubscriptionSet, { ChannelName } from "./SubscriptionSet"; import { Decoder, RESP_TYPES } from "./resp/decoder"; import { TypeMapping } from "./resp/types"; @@ -142,7 +142,10 @@ export default class DataHandler { // command response. Routing it by content would misinterpret replies that // merely look like pub/sub messages (e.g. an LRANGE result starting with // "message") and desync the command queue. - if (this.redis.condition.protocol !== 3 && this.handleSubscriberReply(reply)) { + if ( + this.redis.condition.protocol !== 3 && + this.handleSubscriberReply(reply) + ) { return; } @@ -154,7 +157,7 @@ export default class DataHandler { this.redis.condition.subscriber = new SubscriptionSet(); this.redis.condition.subscriber.add( item.command.name, - reply[1].toString() + reply[1] as ChannelName ); if (!fillSubCommand(item.command, reply[2])) { @@ -190,7 +193,7 @@ export default class DataHandler { this.redis.condition.subscriber = new SubscriptionSet(); } - const channel = reply[1].toString(); + const channel = reply[1] as ChannelName; this.redis.condition.subscriber.add(replyType, channel); const item = this.shiftCommand(reply); if (!item) { @@ -206,16 +209,17 @@ export default class DataHandler { case "sunsubscribe": case "unsubscribe": case "punsubscribe": { + const count = reply[2] as string | Buffer | number; + if (this.redis.condition.subscriber) { - const channel = reply[1] ? reply[1].toString() : null; - if (channel) { + // Empty channel names are valid; only a null or undefined reply + // element means no channel. + const channel = reply[1] == null ? null : (reply[1] as ChannelName); + if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); } - } - const count = reply[2] as string | Buffer | number; - if (Number(count) === 0) { - this.redis.condition.subscriber = false; + leaveSubscriberModeIfDone(this.redis.condition, count); } if (this.handleUnsolicitedUnsubscribe(replyType)) { @@ -299,7 +303,7 @@ export default class DataHandler { case "ssubscribe": case "subscribe": case "psubscribe": { - const channel = reply[1].toString(); + const channel = reply[1] as ChannelName; this.redis.condition.subscriber.add(replyType, channel); const item = this.shiftCommand(reply); if (!item) { @@ -313,14 +317,14 @@ export default class DataHandler { case "sunsubscribe": case "unsubscribe": case "punsubscribe": { - const channel = reply[1] ? reply[1].toString() : null; - if (channel) { + // Empty channel names are valid; only a null or undefined reply + // element means no channel. + const channel = reply[1] == null ? null : (reply[1] as ChannelName); + if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); } const count = reply[2]; - if (Number(count) === 0) { - this.redis.condition.subscriber = false; - } + leaveSubscriberModeIfDone(this.redis.condition, count); if (this.handleUnsolicitedUnsubscribe(replyType)) { break; } @@ -391,6 +395,25 @@ export default class DataHandler { } } +// The count carried by the reply and the subscription set are each a partial +// view of the connection's subscriptions. Regular channels and patterns share +// one count and shard channels have their own, so a count of zero says nothing +// about the other group; the set tracks only what this connection subscribed +// to through ioredis, so it cannot see a subscription the server holds but +// never acknowledged. Subscriber mode is left only when both are empty. A +// reply carrying no count is not authoritative and does not hold the +// connection in subscriber mode on its own. +function leaveSubscriberModeIfDone( + condition: Condition, + count: string | Buffer | number +) { + const remaining = Number(count); + const subscriber = condition.subscriber as SubscriptionSet; + if (subscriber.isEmpty() && !(remaining > 0)) { + condition.subscriber = false; + } +} + // All string RESP types (simple, blob, verbatim) decode to Buffer rather than // utf8 strings. This keeps the parser encoding-agnostic: ioredis decides // utf8-vs-Buffer per command afterwards via `replyEncoding`. Decoding to utf8 diff --git a/lib/SubscriptionSet.ts b/lib/SubscriptionSet.ts index 98a92c627..c63536fa5 100644 --- a/lib/SubscriptionSet.ts +++ b/lib/SubscriptionSet.ts @@ -3,33 +3,61 @@ import { CommandNameFlags } from "./Command"; type AddSet = CommandNameFlags["ENTER_SUBSCRIBER_MODE"][number]; type DelSet = CommandNameFlags["EXIT_SUBSCRIBER_MODE"][number]; +// The channel name is read out of a reply, and a reply is not guaranteed to be +// the array a subscribe acknowledgement normally is: a server or proxy that +// answers `SUBSCRIBE` with a simple string leaves the caller indexing a Buffer, +// which yields a byte. Such a value has no bytes of its own to preserve, so it +// is keyed by its string form rather than rejected. +export type ChannelName = string | Buffer | number; + +// Channel names are binary safe, so they are keyed by their bytes rather than +// by their utf8 rendering: names that are distinct as bytes can render alike +// (e.g. the invalid sequences `<80>` and `<81>`, which both decode to U+FFFD), +// and sharing a key would drop one of them from the set. `latin1` maps every +// byte to one code unit, so the key round-trips the exact bytes. The value +// keeps the utf8 rendering, which is what `channels()` hands back. +type ChannelSet = Map; + +function channelKey(channel: ChannelName): string { + return ( + Buffer.isBuffer(channel) ? channel : Buffer.from(String(channel)) + ).toString("latin1"); +} + +function channelName(channel: ChannelName): string { + return Buffer.isBuffer(channel) ? channel.toString() : String(channel); +} + /** * Tiny class to simplify dealing with subscription set */ export default class SubscriptionSet { - private set: { [key: string]: { [channel: string]: boolean } } = { - subscribe: {}, - psubscribe: {}, - ssubscribe: {}, + private set: { [key: string]: ChannelSet } = { + subscribe: new Map(), + psubscribe: new Map(), + ssubscribe: new Map(), }; - add(set: AddSet, channel: string) { - this.set[mapSet(set)][channel] = true; + add(set: AddSet, channel: ChannelName) { + this.set[mapSet(set)].set(channelKey(channel), channelName(channel)); } - del(set: DelSet, channel: string) { - delete this.set[mapSet(set)][channel]; + del(set: DelSet, channel: ChannelName) { + this.set[mapSet(set)].delete(channelKey(channel)); } channels(set: AddSet | DelSet): string[] { - return Object.keys(this.set[mapSet(set)]); + return Array.from(this.set[mapSet(set)].values()); } + // Called once per unsubscribe acknowledgement, and the server sends one per + // channel, so this stays O(1): counting the keys instead would make + // unsubscribing N channels Θ(N²) work and stall the event loop. isEmpty(): boolean { return ( - this.channels("subscribe").length === 0 && - this.channels("psubscribe").length === 0 && - this.channels("ssubscribe").length === 0 + this.set.subscribe.size === 0 && + this.set.psubscribe.size === 0 && + this.set.ssubscribe.size === 0 ); } } diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts new file mode 100644 index 000000000..73c09893d --- /dev/null +++ b/test/unit/subscriberMode.ts @@ -0,0 +1,86 @@ +import { expect } from "chai"; +import MockServer, { pubSubReply } from "../helpers/mock_server"; +import Redis from "../../lib/Redis"; +import SubscriptionSet from "../../lib/SubscriptionSet"; + +// Redis counts regular channels and patterns together and shard channels +// separately, so the count carried by an unsubscribe reply describes only its +// own group. Dropping the subscriber state on that count alone loses +// subscriptions of the other group 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 group reports its own remaining count. + if (name === "subscribe" || name === "ssubscribe") { + return pubSubReply(protocol, name, argv[1], 1); + } + if (name === "unsubscribe" || name === "sunsubscribe") { + return pubSubReply(protocol, name, 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("clears the subscription state 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); + // `Redis#mode` reports the subscriber state under RESP2 only. + if (protocol === 2) { + expect(redis.mode).to.equal("normal"); + } + }); + }); +}); + +describe("SubscriptionSet", () => { + // `__proto__` is never stored as an own key on a plain object. + it("tracks a channel named __proto__", () => { + const set = new SubscriptionSet(); + set.add("subscribe", "__proto__"); + expect(set.isEmpty()).to.equal(false); + }); + + // `<80>` and `<81>` are both invalid utf8 and render as the same string. + it("keeps binary channel names that share a utf8 rendering apart", () => { + const set = new SubscriptionSet(); + set.add("subscribe", Buffer.from([0x80])); + set.add("subscribe", Buffer.from([0x81])); + set.del("unsubscribe", Buffer.from([0x80])); + expect(set.isEmpty()).to.equal(false); + }); +});