Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
57 changes: 43 additions & 14 deletions lib/DataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,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 string | Buffer
);

if (!fillSubCommand(item.command, reply[2])) {
Expand Down Expand Up @@ -190,7 +190,7 @@ export default class DataHandler {
this.redis.condition.subscriber = new SubscriptionSet();
}

const channel = reply[1].toString();
const channel = reply[1] as string | Buffer;
this.redis.condition.subscriber.add(replyType, channel);
const item = this.shiftCommand(reply);
if (!item) {
Expand All @@ -206,16 +206,19 @@ 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) {
// `""` is a valid channel name, so both guards test for a missing
// reply element rather than truthiness — skipping `del()` for it
// would keep the set non-empty and strand subscriber mode below.
const channel =
reply[1] == null ? null : (reply[1] as string | Buffer);
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)) {
Expand Down Expand Up @@ -299,7 +302,7 @@ export default class DataHandler {
case "ssubscribe":
case "subscribe":
case "psubscribe": {
const channel = reply[1].toString();
const channel = reply[1] as string | Buffer;
this.redis.condition.subscriber.add(replyType, channel);
const item = this.shiftCommand(reply);
if (!item) {
Expand All @@ -313,14 +316,13 @@ export default class DataHandler {
case "sunsubscribe":
case "unsubscribe":
case "punsubscribe": {
const channel = reply[1] ? reply[1].toString() : null;
if (channel) {
// See the matching comment in `returnPush` about the empty channel name.
const channel = reply[1] == null ? null : (reply[1] as string | Buffer);
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;
}
Expand Down Expand Up @@ -391,6 +393,33 @@ export default class DataHandler {
}
}

// Both the count carried by the reply and the subscription set are partial
// views of the connection's subscriptions, and neither one alone answers
// "is this connection still in subscriber mode?":
//
// - The 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 says nothing
// about the other kinds.
// - The set only tracks what this connection subscribed to through ioredis,
// so it cannot see a subscription the server holds but never acknowledged
// to it.
//
// Requiring both covers each one's blind spot with the other: the count rules
// out a remaining same-kind subscription, and the set rules out the kinds the
// count ignores. A reply carrying no count is not authoritative about
// anything, so it 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;
Comment thread
askalf marked this conversation as resolved.
Resolved
Comment thread
askalf marked this conversation as resolved.
Resolved
}
}

// 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
Expand Down
45 changes: 33 additions & 12 deletions lib/SubscriptionSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,54 @@ import { CommandNameFlags } from "./Command";
type AddSet = CommandNameFlags["ENTER_SUBSCRIBER_MODE"][number];
type DelSet = CommandNameFlags["EXIT_SUBSCRIBER_MODE"][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<string, string>;

function channelKey(channel: string | Buffer): string {
return (Buffer.isBuffer(channel) ? channel : Buffer.from(channel)).toString(
"latin1"
);
}

function channelName(channel: string | Buffer): string {
return Buffer.isBuffer(channel) ? channel.toString() : channel;
Comment thread
askalf marked this conversation as resolved.
Resolved
}

/**
* 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: string | Buffer) {
this.set[mapSet(set)].set(channelKey(channel), channelName(channel));
}

del(set: DelSet, channel: string) {
delete this.set[mapSet(set)][channel];
del(set: DelSet, channel: string | Buffer) {
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
);
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/helpers/mock_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,35 @@ export function pubSubReply(
}
return [type, channel, value];
}

// `pubSubReply` goes through the string-based writer, which cannot carry a
// channel name that is not valid utf8. This builds the same reply as raw bytes
// instead, for the binary-safe channel names, and returns a Buffer to write to
// the socket directly (RESP3 push frame or RESP2 array, as the protocol asks).
export function rawPubSubReply(
protocol: MockServerProtocol,
type: string,
channel: string | Buffer,
value: string | number = 1
): Buffer {
const channelBytes = Buffer.isBuffer(channel)
? channel
: Buffer.from(channel);
const parts = [
Buffer.from(`${protocol === 3 ? ">" : "*"}3\r\n`),
blobString(Buffer.from(type)),
blobString(channelBytes),
typeof value === "number"
? Buffer.from(`:${value}\r\n`)
: blobString(Buffer.from(value)),
];
return Buffer.concat(parts);
}

function blobString(payload: Buffer): Buffer {
return Buffer.concat([
Buffer.from(`$${payload.length}\r\n`),
payload,
Buffer.from("\r\n"),
]);
}
Loading
Loading