You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
move from TypeScript privates to "#"
This commit is contained in:
@@ -100,83 +100,83 @@ export default class RedisClusterSlots<
|
||||
RESP extends RespVersions,
|
||||
TYPE_MAPPING extends TypeMapping
|
||||
> {
|
||||
private static _SLOTS = 16384;
|
||||
static #SLOTS = 16384;
|
||||
|
||||
private readonly _options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING>;
|
||||
private readonly _clientFactory: ReturnType<typeof RedisClient.factory<M, F, S, RESP>>;
|
||||
private readonly _emit: EventEmitter['emit'];
|
||||
slots = new Array<Shard<M, F, S, RESP, TYPE_MAPPING>>(RedisClusterSlots._SLOTS);
|
||||
readonly #options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING>;
|
||||
readonly #clientFactory: ReturnType<typeof RedisClient.factory<M, F, S, RESP>>;
|
||||
readonly #emit: EventEmitter['emit'];
|
||||
slots = new Array<Shard<M, F, S, RESP, TYPE_MAPPING>>(RedisClusterSlots.#SLOTS);
|
||||
masters = new Array<MasterNode<M, F, S, RESP, TYPE_MAPPING>>();
|
||||
replicas = new Array<ShardNode<M, F, S, RESP, TYPE_MAPPING>>();
|
||||
readonly nodeByAddress = new Map<string, MasterNode<M, F, S, RESP, TYPE_MAPPING> | ShardNode<M, F, S, RESP, TYPE_MAPPING>>();
|
||||
pubSubNode?: PubSubNode<M, F, S, RESP, TYPE_MAPPING>;
|
||||
|
||||
private _isOpen = false;
|
||||
#isOpen = false;
|
||||
|
||||
get isOpen() {
|
||||
return this._isOpen;
|
||||
return this.#isOpen;
|
||||
}
|
||||
|
||||
constructor(
|
||||
options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING>,
|
||||
emit: EventEmitter['emit']
|
||||
) {
|
||||
this._options = options;
|
||||
this._clientFactory = RedisClient.factory(options);
|
||||
this._emit = emit;
|
||||
this.#options = options;
|
||||
this.#clientFactory = RedisClient.factory(options);
|
||||
this.#emit = emit;
|
||||
}
|
||||
|
||||
async connect() {
|
||||
if (this._isOpen) {
|
||||
if (this.#isOpen) {
|
||||
throw new Error('Cluster already open');
|
||||
}
|
||||
|
||||
this._isOpen = true;
|
||||
this.#isOpen = true;
|
||||
try {
|
||||
await this._discoverWithRootNodes();
|
||||
await this.#discoverWithRootNodes();
|
||||
} catch (err) {
|
||||
this._isOpen = false;
|
||||
this.#isOpen = false;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
private async _discoverWithRootNodes() {
|
||||
let start = Math.floor(Math.random() * this._options.rootNodes.length);
|
||||
for (let i = start; i < this._options.rootNodes.length; i++) {
|
||||
if (!this._isOpen) throw new Error('Cluster closed');
|
||||
if (await this._discover(this._options.rootNodes[i])) return;
|
||||
async #discoverWithRootNodes() {
|
||||
let start = Math.floor(Math.random() * this.#options.rootNodes.length);
|
||||
for (let i = start; i < this.#options.rootNodes.length; i++) {
|
||||
if (!this.#isOpen) throw new Error('Cluster closed');
|
||||
if (await this.#discover(this.#options.rootNodes[i])) return;
|
||||
}
|
||||
|
||||
for (let i = 0; i < start; i++) {
|
||||
if (!this._isOpen) throw new Error('Cluster closed');
|
||||
if (await this._discover(this._options.rootNodes[i])) return;
|
||||
if (!this.#isOpen) throw new Error('Cluster closed');
|
||||
if (await this.#discover(this.#options.rootNodes[i])) return;
|
||||
}
|
||||
|
||||
throw new RootNodesUnavailableError();
|
||||
}
|
||||
|
||||
private _resetSlots() {
|
||||
this.slots = new Array(RedisClusterSlots._SLOTS);
|
||||
#resetSlots() {
|
||||
this.slots = new Array(RedisClusterSlots.#SLOTS);
|
||||
this.masters = [];
|
||||
this.replicas = [];
|
||||
this._randomNodeIterator = undefined;
|
||||
}
|
||||
|
||||
private async _discover(rootNode: RedisClusterClientOptions) {
|
||||
this._resetSlots();
|
||||
async #discover(rootNode: RedisClusterClientOptions) {
|
||||
this.#resetSlots();
|
||||
try {
|
||||
const addressesInUse = new Set<string>(),
|
||||
promises: Array<Promise<unknown>> = [],
|
||||
eagerConnect = this._options.minimizeConnections !== true;
|
||||
eagerConnect = this.#options.minimizeConnections !== true;
|
||||
|
||||
for (const { from, to, master, replicas } of await this._getShards(rootNode)) {
|
||||
for (const { from, to, master, replicas } of await this.#getShards(rootNode)) {
|
||||
const shard: Shard<M, F, S, RESP, TYPE_MAPPING> = {
|
||||
master: this._initiateSlotNode(master, false, eagerConnect, addressesInUse, promises)
|
||||
master: this.#initiateSlotNode(master, false, eagerConnect, addressesInUse, promises)
|
||||
};
|
||||
|
||||
if (this._options.useReplicas) {
|
||||
if (this.#options.useReplicas) {
|
||||
shard.replicas = replicas.map(replica =>
|
||||
this._initiateSlotNode(replica, true, eagerConnect, addressesInUse, promises)
|
||||
this.#initiateSlotNode(replica, true, eagerConnect, addressesInUse, promises)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -193,7 +193,7 @@ export default class RedisClusterSlots<
|
||||
|
||||
if (channelsListeners.size || patternsListeners.size) {
|
||||
promises.push(
|
||||
this._initiatePubSubClient({
|
||||
this.#initiatePubSubClient({
|
||||
[PubSubType.CHANNELS]: channelsListeners,
|
||||
[PubSubType.PATTERNS]: patternsListeners
|
||||
})
|
||||
@@ -220,21 +220,21 @@ export default class RedisClusterSlots<
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
this._emit('error', err);
|
||||
this.#emit('error', err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async _getShards(rootNode: RedisClusterClientOptions) {
|
||||
const options = this._clientOptionsDefaults(rootNode)!;
|
||||
async #getShards(rootNode: RedisClusterClientOptions) {
|
||||
const options = this.#clientOptionsDefaults(rootNode)!;
|
||||
options.socket ??= {};
|
||||
options.socket.reconnectStrategy = false;
|
||||
options.RESP = this._options.RESP;
|
||||
options.RESP = this.#options.RESP;
|
||||
options.commandOptions = undefined;
|
||||
|
||||
// TODO: find a way to avoid type casting
|
||||
const client = await this._clientFactory(options as RedisClientOptions<M, F, S, RESP, {}>)
|
||||
.on('error', err => this._emit('error', err))
|
||||
const client = await this.#clientFactory(options as RedisClientOptions<M, F, S, RESP, {}>)
|
||||
.on('error', err => this.#emit('error', err))
|
||||
.connect();
|
||||
|
||||
try {
|
||||
@@ -245,37 +245,37 @@ export default class RedisClusterSlots<
|
||||
}
|
||||
}
|
||||
|
||||
private _getNodeAddress(address: string): NodeAddress | undefined {
|
||||
switch (typeof this._options.nodeAddressMap) {
|
||||
#getNodeAddress(address: string): NodeAddress | undefined {
|
||||
switch (typeof this.#options.nodeAddressMap) {
|
||||
case 'object':
|
||||
return this._options.nodeAddressMap[address];
|
||||
return this.#options.nodeAddressMap[address];
|
||||
|
||||
case 'function':
|
||||
return this._options.nodeAddressMap(address);
|
||||
return this.#options.nodeAddressMap(address);
|
||||
}
|
||||
}
|
||||
|
||||
private _clientOptionsDefaults(options?: RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>) {
|
||||
if (!this._options.defaults) return options;
|
||||
#clientOptionsDefaults(options?: RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>) {
|
||||
if (!this.#options.defaults) return options;
|
||||
|
||||
let socket;
|
||||
if (this._options.defaults.socket) {
|
||||
if (this.#options.defaults.socket) {
|
||||
socket = options?.socket ? {
|
||||
...this._options.defaults.socket,
|
||||
...this.#options.defaults.socket,
|
||||
...options.socket
|
||||
} : this._options.defaults.socket;
|
||||
} : this.#options.defaults.socket;
|
||||
} else {
|
||||
socket = options?.socket;
|
||||
}
|
||||
|
||||
return {
|
||||
...this._options.defaults,
|
||||
...this.#options.defaults,
|
||||
...options,
|
||||
socket
|
||||
};
|
||||
}
|
||||
|
||||
private _initiateSlotNode(
|
||||
#initiateSlotNode(
|
||||
shard: NodeAddress & { id: string; },
|
||||
readonly: boolean,
|
||||
eagerConnent: boolean,
|
||||
@@ -295,7 +295,7 @@ export default class RedisClusterSlots<
|
||||
};
|
||||
|
||||
if (eagerConnent) {
|
||||
promises.push(this._createNodeClient(node));
|
||||
promises.push(this.#createNodeClient(node));
|
||||
}
|
||||
|
||||
this.nodeByAddress.set(address, node);
|
||||
@@ -309,21 +309,21 @@ export default class RedisClusterSlots<
|
||||
return node;
|
||||
}
|
||||
|
||||
private _createClient(node: ShardNode<M, F, S, RESP, TYPE_MAPPING>, readonly = node.readonly) {
|
||||
return this._clientFactory(
|
||||
this._clientOptionsDefaults({
|
||||
socket: this._getNodeAddress(node.address) ?? {
|
||||
#createClient(node: ShardNode<M, F, S, RESP, TYPE_MAPPING>, readonly = node.readonly) {
|
||||
return this.#clientFactory(
|
||||
this.#clientOptionsDefaults({
|
||||
socket: this.#getNodeAddress(node.address) ?? {
|
||||
host: node.host,
|
||||
port: node.port
|
||||
},
|
||||
readonly,
|
||||
RESP: this._options.RESP
|
||||
RESP: this.#options.RESP
|
||||
})
|
||||
).on('error', err => console.error(err));
|
||||
}
|
||||
|
||||
private _createNodeClient(node: ShardNode<M, F, S, RESP, TYPE_MAPPING>, readonly?: boolean) {
|
||||
const client = node.client = this._createClient(node, readonly);
|
||||
#createNodeClient(node: ShardNode<M, F, S, RESP, TYPE_MAPPING>, readonly?: boolean) {
|
||||
const client = node.client = this.#createClient(node, readonly);
|
||||
return node.connectPromise = client.connect()
|
||||
.finally(() => node.connectPromise = undefined);
|
||||
}
|
||||
@@ -332,46 +332,46 @@ export default class RedisClusterSlots<
|
||||
return (
|
||||
node.connectPromise ?? // if the node is connecting
|
||||
node.client ?? // if the node is connected
|
||||
this._createNodeClient(node) // if the not is disconnected
|
||||
this.#createNodeClient(node) // if the not is disconnected
|
||||
);
|
||||
}
|
||||
|
||||
private _runningRediscoverPromise?: Promise<void>;
|
||||
#runningRediscoverPromise?: Promise<void>;
|
||||
|
||||
async rediscover(startWith: RedisClientType<M, F, S, RESP>): Promise<void> {
|
||||
this._runningRediscoverPromise ??= this._rediscover(startWith)
|
||||
.finally(() => this._runningRediscoverPromise = undefined);
|
||||
return this._runningRediscoverPromise;
|
||||
this.#runningRediscoverPromise ??= this.#rediscover(startWith)
|
||||
.finally(() => this.#runningRediscoverPromise = undefined);
|
||||
return this.#runningRediscoverPromise;
|
||||
}
|
||||
|
||||
private async _rediscover(startWith: RedisClientType<M, F, S, RESP>): Promise<void> {
|
||||
if (await this._discover(startWith.options!)) return;
|
||||
async #rediscover(startWith: RedisClientType<M, F, S, RESP>): Promise<void> {
|
||||
if (await this.#discover(startWith.options!)) return;
|
||||
|
||||
return this._discoverWithRootNodes();
|
||||
return this.#discoverWithRootNodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `close` instead.
|
||||
*/
|
||||
quit(): Promise<void> {
|
||||
return this._destroy(client => client.quit());
|
||||
return this.#destroy(client => client.quit());
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `destroy` instead.
|
||||
*/
|
||||
disconnect(): Promise<void> {
|
||||
return this._destroy(client => client.disconnect());
|
||||
return this.#destroy(client => client.disconnect());
|
||||
}
|
||||
|
||||
close() {
|
||||
return this._destroy(client => client.close());
|
||||
return this.#destroy(client => client.close());
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this._isOpen = false;
|
||||
this.#isOpen = false;
|
||||
|
||||
for (const client of this._clients()) {
|
||||
for (const client of this.#clients()) {
|
||||
client.destroy();
|
||||
}
|
||||
|
||||
@@ -380,11 +380,11 @@ export default class RedisClusterSlots<
|
||||
this.pubSubNode = undefined;
|
||||
}
|
||||
|
||||
this._resetSlots();
|
||||
this.#resetSlots();
|
||||
this.nodeByAddress.clear();
|
||||
}
|
||||
|
||||
private *_clients() {
|
||||
*#clients() {
|
||||
for (const master of this.masters) {
|
||||
if (master.client) {
|
||||
yield master.client;
|
||||
@@ -402,11 +402,11 @@ export default class RedisClusterSlots<
|
||||
}
|
||||
}
|
||||
|
||||
private async _destroy(fn: (client: RedisClientType<M, F, S, RESP>) => Promise<unknown>): Promise<void> {
|
||||
this._isOpen = false;
|
||||
async #destroy(fn: (client: RedisClientType<M, F, S, RESP>) => Promise<unknown>): Promise<void> {
|
||||
this.#isOpen = false;
|
||||
|
||||
const promises = [];
|
||||
for (const client of this._clients()) {
|
||||
for (const client of this.#clients()) {
|
||||
promises.push(fn(client));
|
||||
}
|
||||
|
||||
@@ -415,7 +415,7 @@ export default class RedisClusterSlots<
|
||||
this.pubSubNode = undefined;
|
||||
}
|
||||
|
||||
this._resetSlots();
|
||||
this.#resetSlots();
|
||||
this.nodeByAddress.clear();
|
||||
|
||||
await Promise.allSettled(promises);
|
||||
@@ -437,7 +437,7 @@ export default class RedisClusterSlots<
|
||||
return this.nodeClient(this.getSlotRandomNode(slotNumber));
|
||||
}
|
||||
|
||||
private *_iterateAllNodes() {
|
||||
*#iterateAllNodes() {
|
||||
let i = Math.floor(Math.random() * (this.masters.length + this.replicas.length));
|
||||
if (i < this.masters.length) {
|
||||
do {
|
||||
@@ -468,11 +468,11 @@ export default class RedisClusterSlots<
|
||||
_randomNodeIterator?: IterableIterator<ShardNode<M, F, S, RESP, TYPE_MAPPING>>;
|
||||
|
||||
getRandomNode() {
|
||||
this._randomNodeIterator ??= this._iterateAllNodes();
|
||||
this._randomNodeIterator ??= this.#iterateAllNodes();
|
||||
return this._randomNodeIterator.next().value as ShardNode<M, F, S, RESP, TYPE_MAPPING>;
|
||||
}
|
||||
|
||||
private *_slotNodesIterator(slot: ShardWithReplicas<M, F, S, RESP, TYPE_MAPPING>) {
|
||||
*#slotNodesIterator(slot: ShardWithReplicas<M, F, S, RESP, TYPE_MAPPING>) {
|
||||
let i = Math.floor(Math.random() * (1 + slot.replicas.length));
|
||||
if (i < slot.replicas.length) {
|
||||
do {
|
||||
@@ -495,7 +495,7 @@ export default class RedisClusterSlots<
|
||||
return slot.master;
|
||||
}
|
||||
|
||||
slot.nodesIterator ??= this._slotNodesIterator(slot as ShardWithReplicas<M, F, S, RESP, TYPE_MAPPING>);
|
||||
slot.nodesIterator ??= this.#slotNodesIterator(slot as ShardWithReplicas<M, F, S, RESP, TYPE_MAPPING>);
|
||||
return slot.nodesIterator.next().value as ShardNode<M, F, S, RESP, TYPE_MAPPING>;
|
||||
}
|
||||
|
||||
@@ -507,17 +507,17 @@ export default class RedisClusterSlots<
|
||||
}
|
||||
|
||||
getPubSubClient() {
|
||||
if (!this.pubSubNode) return this._initiatePubSubClient();
|
||||
if (!this.pubSubNode) return this.#initiatePubSubClient();
|
||||
|
||||
return this.pubSubNode.connectPromise ?? this.pubSubNode.client;
|
||||
}
|
||||
|
||||
private async _initiatePubSubClient(toResubscribe?: PubSubToResubscribe) {
|
||||
async #initiatePubSubClient(toResubscribe?: PubSubToResubscribe) {
|
||||
const index = Math.floor(Math.random() * (this.masters.length + this.replicas.length)),
|
||||
node = index < this.masters.length ?
|
||||
this.masters[index] :
|
||||
this.replicas[index - this.masters.length],
|
||||
client = this._createClient(node, true);
|
||||
client = this.#createClient(node, true);
|
||||
|
||||
this.pubSubNode = {
|
||||
address: node.address,
|
||||
@@ -557,12 +557,12 @@ export default class RedisClusterSlots<
|
||||
|
||||
getShardedPubSubClient(channel: string) {
|
||||
const { master } = this.slots[calculateSlot(channel)];
|
||||
if (!master.pubSub) return this._initiateShardedPubSubClient(master);
|
||||
if (!master.pubSub) return this.#initiateShardedPubSubClient(master);
|
||||
return master.pubSub.connectPromise ?? master.pubSub.client;
|
||||
}
|
||||
|
||||
private async _initiateShardedPubSubClient(master: MasterNode<M, F, S, RESP, TYPE_MAPPING>) {
|
||||
const client = this._createClient(master, true)
|
||||
async #initiateShardedPubSubClient(master: MasterNode<M, F, S, RESP, TYPE_MAPPING>) {
|
||||
const client = this.#createClient(master, true)
|
||||
.on('server-sunsubscribe', async (channel, listeners) => {
|
||||
try {
|
||||
await this.rediscover(client);
|
||||
@@ -573,7 +573,7 @@ export default class RedisClusterSlots<
|
||||
listeners
|
||||
);
|
||||
} catch (err) {
|
||||
this._emit('sharded-shannel-moved-error', err, channel, listeners);
|
||||
this.#emit('sharded-shannel-moved-error', err, channel, listeners);
|
||||
}
|
||||
});
|
||||
|
||||
|
@@ -132,7 +132,7 @@ export interface ClusterCommandOptions<
|
||||
|
||||
type ProxyCluster = RedisCluster<any, any, any, any, any/*, any*/>;
|
||||
|
||||
type NamespaceProxyCluster = { self: ProxyCluster };
|
||||
type NamespaceProxyCluster = { _self: ProxyCluster };
|
||||
|
||||
export default class RedisCluster<
|
||||
M extends RedisModules,
|
||||
@@ -166,7 +166,7 @@ export default class RedisCluster<
|
||||
return key;
|
||||
}
|
||||
|
||||
private static _createCommand(command: Command, resp: RespVersions) {
|
||||
static #createCommand(command: Command, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(command, resp);
|
||||
return async function (this: ProxyCluster, ...args: Array<unknown>) {
|
||||
const redisArgs = command.transformArguments(...args),
|
||||
@@ -189,7 +189,7 @@ export default class RedisCluster<
|
||||
};
|
||||
}
|
||||
|
||||
private static _createModuleCommand(command: Command, resp: RespVersions) {
|
||||
static #createModuleCommand(command: Command, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(command, resp);
|
||||
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
|
||||
const redisArgs = command.transformArguments(...args),
|
||||
@@ -198,11 +198,11 @@ export default class RedisCluster<
|
||||
args,
|
||||
redisArgs
|
||||
),
|
||||
reply = await this.self.sendCommand(
|
||||
reply = await this._self.sendCommand(
|
||||
firstKey,
|
||||
command.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
this.self._commandOptions,
|
||||
this._self._commandOptions,
|
||||
// command.POLICIES
|
||||
);
|
||||
|
||||
@@ -212,7 +212,7 @@ export default class RedisCluster<
|
||||
};
|
||||
}
|
||||
|
||||
private static _createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
|
||||
static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
|
||||
const prefix = functionArgumentsPrefix(name, fn),
|
||||
transformReply = getTransformReply(fn, resp);
|
||||
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
|
||||
@@ -223,11 +223,11 @@ export default class RedisCluster<
|
||||
fnArgs
|
||||
),
|
||||
redisArgs = prefix.concat(fnArgs),
|
||||
reply = await this.self.sendCommand(
|
||||
reply = await this._self.sendCommand(
|
||||
firstKey,
|
||||
fn.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
this.self._commandOptions,
|
||||
this._self._commandOptions,
|
||||
// fn.POLICIES
|
||||
);
|
||||
|
||||
@@ -237,7 +237,7 @@ export default class RedisCluster<
|
||||
};
|
||||
}
|
||||
|
||||
private static _createScriptCommand(script: RedisScript, resp: RespVersions) {
|
||||
static #createScriptCommand(script: RedisScript, resp: RespVersions) {
|
||||
const prefix = scriptArgumentsPrefix(script),
|
||||
transformReply = getTransformReply(script, resp);
|
||||
return async function (this: ProxyCluster, ...args: Array<unknown>) {
|
||||
@@ -274,17 +274,17 @@ export default class RedisCluster<
|
||||
const Cluster = attachConfig({
|
||||
BaseClass: RedisCluster,
|
||||
commands: COMMANDS,
|
||||
createCommand: RedisCluster._createCommand,
|
||||
createModuleCommand: RedisCluster._createModuleCommand,
|
||||
createFunctionCommand: RedisCluster._createFunctionCommand,
|
||||
createScriptCommand: RedisCluster._createScriptCommand,
|
||||
createCommand: RedisCluster.#createCommand,
|
||||
createModuleCommand: RedisCluster.#createModuleCommand,
|
||||
createFunctionCommand: RedisCluster.#createFunctionCommand,
|
||||
createScriptCommand: RedisCluster.#createScriptCommand,
|
||||
config
|
||||
});
|
||||
|
||||
Cluster.prototype.Multi = RedisClusterMultiCommand.extend(config);
|
||||
|
||||
return (options?: Omit<RedisClusterOptions, keyof Exclude<typeof config, undefined>>) => {
|
||||
// returning a "proxy" to prevent the namespaces.self to leak between "proxies"
|
||||
// returning a "proxy" to prevent the namespaces._self to leak between "proxies"
|
||||
return Object.create(new Cluster(options)) as RedisClusterType<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>;
|
||||
};
|
||||
}
|
||||
@@ -300,10 +300,11 @@ export default class RedisCluster<
|
||||
return RedisCluster.factory(options)(options);
|
||||
}
|
||||
|
||||
private readonly _options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>;
|
||||
readonly #options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>;
|
||||
|
||||
private readonly _slots: RedisClusterSlots<M, F, S, RESP, TYPE_MAPPING>;
|
||||
readonly #slots: RedisClusterSlots<M, F, S, RESP, TYPE_MAPPING>;
|
||||
|
||||
private _self = this;
|
||||
private _commandOptions?: ClusterCommandOptions<TYPE_MAPPING/*, POLICIES*/>;
|
||||
|
||||
/**
|
||||
@@ -311,7 +312,7 @@ export default class RedisCluster<
|
||||
* Use with {@link RedisCluster.prototype.nodeClient} to get the client for a specific node (master or replica).
|
||||
*/
|
||||
get slots() {
|
||||
return this._slots.slots;
|
||||
return this._self.#slots.slots;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -319,7 +320,7 @@ export default class RedisCluster<
|
||||
* Use with {@link RedisCluster.prototype.nodeClient} to get the client for a specific master node.
|
||||
*/
|
||||
get masters() {
|
||||
return this._slots.masters;
|
||||
return this._self.#slots.masters;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -327,7 +328,7 @@ export default class RedisCluster<
|
||||
* Use with {@link RedisCluster.prototype.nodeClient} to get the client for a specific replica node.
|
||||
*/
|
||||
get replicas() {
|
||||
return this._slots.replicas;
|
||||
return this._self.#slots.replicas;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -335,25 +336,25 @@ export default class RedisCluster<
|
||||
* Use with {@link RedisCluster.prototype.nodeClient} to get the client for a specific node (master or replica).
|
||||
*/
|
||||
get nodeByAddress() {
|
||||
return this._slots.nodeByAddress;
|
||||
return this._self.#slots.nodeByAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* The current pub/sub node.
|
||||
*/
|
||||
get pubSubNode() {
|
||||
return this._slots.pubSubNode;
|
||||
return this._self.#slots.pubSubNode;
|
||||
}
|
||||
|
||||
get isOpen() {
|
||||
return this._slots.isOpen;
|
||||
return this._self.#slots.isOpen;
|
||||
}
|
||||
|
||||
constructor(options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>) {
|
||||
super();
|
||||
|
||||
this._options = options;
|
||||
this._slots = new RedisClusterSlots(options, this.emit.bind(this));
|
||||
this.#options = options;
|
||||
this.#slots = new RedisClusterSlots(options, this.emit.bind(this));
|
||||
|
||||
if (options?.commandOptions) {
|
||||
this._commandOptions = options.commandOptions;
|
||||
@@ -368,14 +369,14 @@ export default class RedisCluster<
|
||||
_TYPE_MAPPING extends TypeMapping = TYPE_MAPPING
|
||||
>(overrides?: Partial<RedisClusterOptions<_M, _F, _S, _RESP, _TYPE_MAPPING>>) {
|
||||
return new (Object.getPrototypeOf(this).constructor)({
|
||||
...this._options,
|
||||
...this._self.#options,
|
||||
commandOptions: this._commandOptions,
|
||||
...overrides
|
||||
}) as RedisClusterType<_M, _F, _S, _RESP, _TYPE_MAPPING>;
|
||||
}
|
||||
|
||||
connect() {
|
||||
return this._slots.connect();
|
||||
return this._self.#slots.connect();
|
||||
}
|
||||
|
||||
withCommandOptions<
|
||||
@@ -430,13 +431,13 @@ export default class RedisCluster<
|
||||
// return this._commandOptionsProxy('policies', policies);
|
||||
// }
|
||||
|
||||
private async _execute<T>(
|
||||
async #execute<T>(
|
||||
firstKey: RedisArgument | undefined,
|
||||
isReadonly: boolean | undefined,
|
||||
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>) => Promise<T>
|
||||
): Promise<T> {
|
||||
const maxCommandRedirections = this._options.maxCommandRedirections ?? 16;
|
||||
let client = await this._slots.getClient(firstKey, isReadonly),
|
||||
const maxCommandRedirections = this.#options.maxCommandRedirections ?? 16;
|
||||
let client = await this.#slots.getClient(firstKey, isReadonly),
|
||||
i = 0;
|
||||
while (true) {
|
||||
try {
|
||||
@@ -449,10 +450,10 @@ export default class RedisCluster<
|
||||
|
||||
if (err.message.startsWith('ASK')) {
|
||||
const address = err.message.substring(err.message.lastIndexOf(' ') + 1);
|
||||
let redirectTo = await this._slots.getMasterByAddress(address);
|
||||
let redirectTo = await this.#slots.getMasterByAddress(address);
|
||||
if (!redirectTo) {
|
||||
await this._slots.rediscover(client);
|
||||
redirectTo = await this._slots.getMasterByAddress(address);
|
||||
await this.#slots.rediscover(client);
|
||||
redirectTo = await this.#slots.getMasterByAddress(address);
|
||||
}
|
||||
|
||||
if (!redirectTo) {
|
||||
@@ -465,8 +466,8 @@ export default class RedisCluster<
|
||||
}
|
||||
|
||||
if (err.message.startsWith('MOVED')) {
|
||||
await this._slots.rediscover(client);
|
||||
client = await this._slots.getClient(firstKey, isReadonly);
|
||||
await this.#slots.rediscover(client);
|
||||
client = await this.#slots.getClient(firstKey, isReadonly);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -482,7 +483,7 @@ export default class RedisCluster<
|
||||
options?: ClusterCommandOptions,
|
||||
// defaultPolicies?: CommandPolicies
|
||||
): Promise<T> {
|
||||
return this._execute(
|
||||
return this._self.#execute(
|
||||
firstKey,
|
||||
isReadonly,
|
||||
client => client.sendCommand(args, options)
|
||||
@@ -496,7 +497,7 @@ export default class RedisCluster<
|
||||
args: Array<RedisArgument>,
|
||||
options?: CommandOptions
|
||||
) {
|
||||
return this._execute(
|
||||
return this._self.#execute(
|
||||
firstKey,
|
||||
isReadonly,
|
||||
client => client.executeScript(script, args, options)
|
||||
@@ -507,11 +508,11 @@ export default class RedisCluster<
|
||||
type Multi = new (...args: ConstructorParameters<typeof RedisClusterMultiCommand>) => RedisClusterMultiCommandType<[], M, F, S, RESP, TYPE_MAPPING>;
|
||||
return new ((this as any).Multi as Multi)(
|
||||
async (firstKey, isReadonly, commands) => {
|
||||
const client = await this._slots.getClient(firstKey, isReadonly);
|
||||
const client = await this._self.#slots.getClient(firstKey, isReadonly);
|
||||
return client._executeMulti(commands);
|
||||
},
|
||||
async (firstKey, isReadonly, commands) => {
|
||||
const client = await this._slots.getClient(firstKey, isReadonly);
|
||||
const client = await this._self.#slots.getClient(firstKey, isReadonly);
|
||||
return client._executePipeline(commands);
|
||||
},
|
||||
routing
|
||||
@@ -525,7 +526,7 @@ export default class RedisCluster<
|
||||
listener: PubSubListener<T>,
|
||||
bufferMode?: T
|
||||
) {
|
||||
return (await this._slots.getPubSubClient())
|
||||
return (await this._self.#slots.getPubSubClient())
|
||||
.SUBSCRIBE(channels, listener, bufferMode);
|
||||
}
|
||||
|
||||
@@ -536,7 +537,7 @@ export default class RedisCluster<
|
||||
listener?: PubSubListener<boolean>,
|
||||
bufferMode?: T
|
||||
) {
|
||||
return this._slots.executeUnsubscribeCommand(client =>
|
||||
return this._self.#slots.executeUnsubscribeCommand(client =>
|
||||
client.UNSUBSCRIBE(channels, listener, bufferMode)
|
||||
);
|
||||
}
|
||||
@@ -548,7 +549,7 @@ export default class RedisCluster<
|
||||
listener: PubSubListener<T>,
|
||||
bufferMode?: T
|
||||
) {
|
||||
return (await this._slots.getPubSubClient())
|
||||
return (await this._self.#slots.getPubSubClient())
|
||||
.PSUBSCRIBE(patterns, listener, bufferMode);
|
||||
}
|
||||
|
||||
@@ -559,7 +560,7 @@ export default class RedisCluster<
|
||||
listener?: PubSubListener<T>,
|
||||
bufferMode?: T
|
||||
) {
|
||||
return this._slots.executeUnsubscribeCommand(client =>
|
||||
return this._self.#slots.executeUnsubscribeCommand(client =>
|
||||
client.PUNSUBSCRIBE(patterns, listener, bufferMode)
|
||||
);
|
||||
}
|
||||
@@ -571,9 +572,9 @@ export default class RedisCluster<
|
||||
listener: PubSubListener<T>,
|
||||
bufferMode?: T
|
||||
) {
|
||||
const maxCommandRedirections = this._options.maxCommandRedirections ?? 16,
|
||||
const maxCommandRedirections = this._self.#options.maxCommandRedirections ?? 16,
|
||||
firstChannel = Array.isArray(channels) ? channels[0] : channels;
|
||||
let client = await this._slots.getShardedPubSubClient(firstChannel);
|
||||
let client = await this._self.#slots.getShardedPubSubClient(firstChannel);
|
||||
for (let i = 0; ; i++) {
|
||||
try {
|
||||
return await client.SSUBSCRIBE(channels, listener, bufferMode);
|
||||
@@ -583,8 +584,8 @@ export default class RedisCluster<
|
||||
}
|
||||
|
||||
if (err.message.startsWith('MOVED')) {
|
||||
await this._slots.rediscover(client);
|
||||
client = await this._slots.getShardedPubSubClient(firstChannel);
|
||||
await this._self.#slots.rediscover(client);
|
||||
client = await this._self.#slots.getShardedPubSubClient(firstChannel);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -600,7 +601,7 @@ export default class RedisCluster<
|
||||
listener: PubSubListener<T>,
|
||||
bufferMode?: T
|
||||
) {
|
||||
return this._slots.executeShardedUnsubscribeCommand(
|
||||
return this._self.#slots.executeShardedUnsubscribeCommand(
|
||||
Array.isArray(channels) ? channels[0] : channels,
|
||||
client => client.SUNSUBSCRIBE(channels, listener, bufferMode)
|
||||
);
|
||||
@@ -612,26 +613,26 @@ export default class RedisCluster<
|
||||
* @deprecated Use `close` instead.
|
||||
*/
|
||||
quit() {
|
||||
return this._slots.quit();
|
||||
return this._self.#slots.quit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `destroy` instead.
|
||||
*/
|
||||
disconnect() {
|
||||
return this._slots.disconnect();
|
||||
return this._self.#slots.disconnect();
|
||||
}
|
||||
|
||||
close() {
|
||||
return this._slots.close();
|
||||
return this._self.#slots.close();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
return this._slots.destroy();
|
||||
return this._self.#slots.destroy();
|
||||
}
|
||||
|
||||
nodeClient(node: ShardNode<M, F, S, RESP, TYPE_MAPPING>) {
|
||||
return this._slots.nodeClient(node);
|
||||
return this._self.#slots.nodeClient(node);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -639,7 +640,7 @@ export default class RedisCluster<
|
||||
* Userful for running "forward" commands (like PUBLISH) on a random node.
|
||||
*/
|
||||
getRandomNode() {
|
||||
return this._slots.getRandomNode();
|
||||
return this._self.#slots.getRandomNode();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -647,7 +648,7 @@ export default class RedisCluster<
|
||||
* Useful for running readonly commands on a slot.
|
||||
*/
|
||||
getSlotRandomNode(slot: number) {
|
||||
return this._slots.getSlotRandomNode(slot);
|
||||
return this._self.#slots.getSlotRandomNode(slot);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -91,7 +91,7 @@ export type ClusterMultiExecute = (
|
||||
) => Promise<Array<unknown>>;
|
||||
|
||||
export default class RedisClusterMultiCommand<REPLIES = []> {
|
||||
private static _createCommand(command: Command, resp: RespVersions) {
|
||||
static #createCommand(command: Command, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(command, resp);
|
||||
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
|
||||
const redisArgs = command.transformArguments(...args),
|
||||
@@ -109,16 +109,16 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
|
||||
};
|
||||
}
|
||||
|
||||
private static _createModuleCommand(command: Command, resp: RespVersions) {
|
||||
static #createModuleCommand(command: Command, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(command, resp);
|
||||
return function (this: { self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
|
||||
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
|
||||
const redisArgs = command.transformArguments(...args),
|
||||
firstKey = RedisCluster.extractFirstKey(
|
||||
command,
|
||||
args,
|
||||
redisArgs
|
||||
);
|
||||
return this.self.addCommand(
|
||||
return this._self.addCommand(
|
||||
firstKey,
|
||||
command.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
@@ -127,10 +127,10 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
|
||||
};
|
||||
}
|
||||
|
||||
private static _createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
|
||||
static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
|
||||
const prefix = functionArgumentsPrefix(name, fn),
|
||||
transformReply = getTransformReply(fn, resp);
|
||||
return function (this: { self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
|
||||
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
|
||||
const fnArgs = fn.transformArguments(...args),
|
||||
redisArgs: CommandArguments = prefix.concat(fnArgs),
|
||||
firstKey = RedisCluster.extractFirstKey(
|
||||
@@ -139,7 +139,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
|
||||
fnArgs
|
||||
);
|
||||
redisArgs.preserve = fnArgs.preserve;
|
||||
return this.self.addCommand(
|
||||
return this._self.addCommand(
|
||||
firstKey,
|
||||
fn.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
@@ -148,11 +148,11 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
|
||||
};
|
||||
}
|
||||
|
||||
private static _createScriptCommand(script: RedisScript, resp: RespVersions) {
|
||||
static #createScriptCommand(script: RedisScript, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(script, resp);
|
||||
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
|
||||
const scriptArgs = script.transformArguments(...args);
|
||||
this._setState(
|
||||
this.#setState(
|
||||
RedisCluster.extractFirstKey(
|
||||
script,
|
||||
args,
|
||||
@@ -160,7 +160,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
|
||||
),
|
||||
script.IS_READ_ONLY
|
||||
);
|
||||
this._multi.addScript(
|
||||
this.#multi.addScript(
|
||||
script,
|
||||
scriptArgs,
|
||||
transformReply
|
||||
@@ -178,36 +178,36 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
|
||||
return attachConfig({
|
||||
BaseClass: RedisClusterMultiCommand,
|
||||
commands: COMMANDS,
|
||||
createCommand: RedisClusterMultiCommand._createCommand,
|
||||
createModuleCommand: RedisClusterMultiCommand._createModuleCommand,
|
||||
createFunctionCommand: RedisClusterMultiCommand._createFunctionCommand,
|
||||
createScriptCommand: RedisClusterMultiCommand._createScriptCommand,
|
||||
createCommand: RedisClusterMultiCommand.#createCommand,
|
||||
createModuleCommand: RedisClusterMultiCommand.#createModuleCommand,
|
||||
createFunctionCommand: RedisClusterMultiCommand.#createFunctionCommand,
|
||||
createScriptCommand: RedisClusterMultiCommand.#createScriptCommand,
|
||||
config
|
||||
});
|
||||
}
|
||||
|
||||
private readonly _multi = new RedisMultiCommand();
|
||||
private readonly _executeMulti: ClusterMultiExecute;
|
||||
private readonly _executePipeline: ClusterMultiExecute;
|
||||
private _firstKey: RedisArgument | undefined;
|
||||
private _isReadonly: boolean | undefined = true;
|
||||
readonly #multi = new RedisMultiCommand();
|
||||
readonly #executeMulti: ClusterMultiExecute;
|
||||
readonly #executePipeline: ClusterMultiExecute;
|
||||
#firstKey: RedisArgument | undefined;
|
||||
#isReadonly: boolean | undefined = true;
|
||||
|
||||
constructor(
|
||||
executeMulti: ClusterMultiExecute,
|
||||
executePipeline: ClusterMultiExecute,
|
||||
routing: RedisArgument | undefined
|
||||
) {
|
||||
this._executeMulti = executeMulti;
|
||||
this._executePipeline = executePipeline;
|
||||
this._firstKey = routing;
|
||||
this.#executeMulti = executeMulti;
|
||||
this.#executePipeline = executePipeline;
|
||||
this.#firstKey = routing;
|
||||
}
|
||||
|
||||
private _setState(
|
||||
#setState(
|
||||
firstKey: RedisArgument | undefined,
|
||||
isReadonly: boolean | undefined,
|
||||
) {
|
||||
this._firstKey ??= firstKey;
|
||||
this._isReadonly &&= isReadonly;
|
||||
this.#firstKey ??= firstKey;
|
||||
this.#isReadonly &&= isReadonly;
|
||||
}
|
||||
|
||||
addCommand(
|
||||
@@ -216,19 +216,19 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
|
||||
args: CommandArguments,
|
||||
transformReply?: TransformReply
|
||||
) {
|
||||
this._setState(firstKey, isReadonly);
|
||||
this._multi.addCommand(args, transformReply);
|
||||
this.#setState(firstKey, isReadonly);
|
||||
this.#multi.addCommand(args, transformReply);
|
||||
return this;
|
||||
}
|
||||
|
||||
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false) {
|
||||
if (execAsPipeline) return this.execAsPipeline<T>();
|
||||
|
||||
return this._multi.transformReplies(
|
||||
await this._executeMulti(
|
||||
this._firstKey,
|
||||
this._isReadonly,
|
||||
this._multi.queue
|
||||
return this.#multi.transformReplies(
|
||||
await this.#executeMulti(
|
||||
this.#firstKey,
|
||||
this.#isReadonly,
|
||||
this.#multi.queue
|
||||
)
|
||||
) as MultiReplyType<T, REPLIES>;
|
||||
}
|
||||
@@ -240,13 +240,13 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
|
||||
}
|
||||
|
||||
async execAsPipeline<T extends MultiReply = MULTI_REPLY['GENERIC']>() {
|
||||
if (this._multi.queue.length === 0) return [] as MultiReplyType<T, REPLIES>;
|
||||
if (this.#multi.queue.length === 0) return [] as MultiReplyType<T, REPLIES>;
|
||||
|
||||
return this._multi.transformReplies(
|
||||
await this._executePipeline(
|
||||
this._firstKey,
|
||||
this._isReadonly,
|
||||
this._multi.queue
|
||||
return this.#multi.transformReplies(
|
||||
await this.#executePipeline(
|
||||
this.#firstKey,
|
||||
this.#isReadonly,
|
||||
this.#multi.queue
|
||||
)
|
||||
) as MultiReplyType<T, REPLIES>;
|
||||
}
|
||||
|
Reference in New Issue
Block a user