/// /// /// import * as EventEmitter from "events"; import ShardedSubscriber from "./ShardedSubscriber"; import { ClusterOptions } from "./ClusterOptions"; /** * Redis distinguishes between "normal" and sharded PubSub. When using the normal PubSub feature, * exactly one subscriber exists per cluster instance because the Redis cluster bus forwards * messages between shards. Sharded PubSub removes this limitation by making each shard * responsible for its own messages. * * This class coordinates one ShardedSubscriber per master node in the cluster, providing * sharded PubSub support while keeping the public API backward compatible. */ export default class ClusterSubscriberGroup { private readonly subscriberGroupEmitter; private readonly options; private shardedSubscribers; private clusterSlots; private subscriberToSlotsIndex; private channels; private failedAttemptsByNode; private unsubscribedSlots; private resubscribeAttempts; private resubscribeRetryTimer; private isResubscribeRetryDue; private subscribersAwaitingReady; private isResetting; private pendingReset; private static readonly MAX_RETRY_ATTEMPTS; private static readonly MAX_BACKOFF_MS; private static readonly BASE_BACKOFF_MS; /** * Register callbacks * * @param cluster */ constructor(subscriberGroupEmitter: EventEmitter, options: ClusterOptions); /** * Get the responsible subscriber. * * @param slot */ getResponsibleSubscriber(slot: number): ShardedSubscriber | undefined; /** * Adds a channel for which this subscriber group is responsible * * @param channels */ addChannels(channels: (string | Buffer)[]): number; /** * Removes channels for which the subscriber group is responsible by optionally unsubscribing * @param channels */ removeChannels(channels: (string | Buffer)[]): number; /** * Unsubscribe every sharded subscriber from all of its shard channels. * * A zero-argument `SUNSUBSCRIBE` carries no channel to compute a target slot * from, so it can't be routed to a single subscriber. Redis unsubscribes the * connection from all of its shard channels in that case; we mirror that by * fanning the command out to every subscriber that currently holds channels. * * @returns the number of remaining shard subscriptions (always 0) */ sunsubscribeAll(): Promise; /** * Disconnect all subscribers and clear some of the internal state. */ stop(): void; /** * Start all not yet started subscribers */ start(): Promise; /** * Resets the subscriber group by disconnecting all subscribers that are no longer needed and connecting new ones. */ reset(clusterSlots: string[][], clusterNodes: any[]): Promise; /** * Refreshes the subscriber-related slot ranges * * Returns false if no refresh was needed * * @param targetSlots */ private _refreshSlots; /** * Resubscribes to the previous channels * * A subscriber that is still connecting gets a single "ready" listener, no * matter how many resets run before it is ready. Its slots and channels are * read when it becomes ready, so they reflect the latest state. * * @private */ private _resubscribe; /** * Sends SSUBSCRIBE for every slot of the subscriber that has channels. */ private resubscribeSubscriber; /** * Deep equality of the cluster slots objects * * @param other * @private */ private _slotsAreEqual; /** * Checks if any subscribers are in an unhealthy state. * * A subscriber is considered unhealthy if: * - It exists but is not started (failed/disconnected) * - It's missing entirely for a node that should have one * * @returns true if any subscribers need to be recreated */ private hasUnhealthySubscribers; private backoffDelay; private markSlotResubscribed; private clearResubscribeRetry; /** * Handles a failed resubscribe. The slot stays marked until a later * resubscribe succeeds, and a slots refresh is requested after a backoff so * that reset() runs again. A node can answer MOVED while still listing * itself as the slot owner, so the refresh may need several attempts. * * An error reply that a retry cannot fix (for example NOPERM) means the * slot's channels stay unsubscribed, so it is reported to the application * instead of being retried. * * A local error is not retried. A closed connection ends the subscriber, * and the reset that follows replaces it and resubscribes its slots. A * timed-out SSUBSCRIBE still runs if the node is alive, and if the node is * stuck, a resend would only queue behind it. Finding a stuck connection is * the job of the health check. */ private handleResubscribeFailed; /** * Handles failed subscriber connections by emitting an event to refresh the slots cache * after a backoff period. * * @param error * @param nodeKey */ private handleSubscriberConnectFailed; /** * Handles successful subscriber connections by resetting the failed attempts counter. * A start that stop() cut short also settles, but it never connected. * * @param sub */ private handleSubscriberConnectSucceeded; private shouldStartSubscriber; /** * Whether the subscriber owning `nodeKey` currently holds any active channels. */ private hasSubscribedChannels; }