diff --git a/packages/client/lib/cluster/cluster-slots.ts b/packages/client/lib/cluster/cluster-slots.ts index e94ad52002..0b445042b5 100644 --- a/packages/client/lib/cluster/cluster-slots.ts +++ b/packages/client/lib/cluster/cluster-slots.ts @@ -259,7 +259,7 @@ export default class RedisClusterSlots< // switch to `CLUSTER SHARDS` when Redis 7.0 will be the minimum supported version return await client.clusterSlots(); } finally { - await client.disconnect(); + client.destroy(); } } @@ -377,40 +377,66 @@ export default class RedisClusterSlots< return this._discoverWithRootNodes(); } + /** + * @deprecated Use `close` instead. + */ quit(): Promise { return this._destroy(client => client.quit()); } + /** + * @deprecated Use `destroy` instead. + */ disconnect(): Promise { return this._destroy(client => client.disconnect()); } + close() { + return this._destroy(client => client.close()); + } + + destroy() { + this._isOpen = false; + + for (const client of this._clients()) { + this._execOnNodeClient(client, client => client.destroy()); + } + + if (this.pubSubNode) { + this._execOnNodeClient(this.pubSubNode.client, client => client.destroy()); + this.pubSubNode = undefined; + } + + this._resetSlots(); + this.nodeByAddress.clear(); + } + + private *_clients() { + for (const { master, replicas } of this.shards) { + if (master.client) { + yield master.client; + } + + if (master.pubSubClient) { + yield master.pubSubClient; + } + + if (replicas) { + for (const { client } of replicas) { + if (client) { + yield client; + } + } + } + } + } + private async _destroy(fn: (client: RedisClientType) => Promise): Promise { this._isOpen = false; const promises = []; - for (const { master, replicas } of this.shards) { - if (master.client) { - promises.push( - this._execOnNodeClient(master.client, fn) - ); - } - - if (master.pubSubClient) { - promises.push( - this._execOnNodeClient(master.pubSubClient, fn) - ); - } - - if (replicas) { - for (const { client } of replicas) { - if (client) { - promises.push( - this._execOnNodeClient(client, fn) - ); - } - } - } + for (const client of this._clients()) { + promises.push(this._execOnNodeClient(client, fn)); } if (this.pubSubNode) { @@ -424,10 +450,10 @@ export default class RedisClusterSlots< await Promise.allSettled(promises); } - private _execOnNodeClient( + private _execOnNodeClient( client: ClientOrPromise, - fn: (client: RedisClientType) => Promise - ) { + fn: (client: RedisClientType) => T + ): T | Promise { return types.isPromise(client) ? client.then(fn) : fn(client); diff --git a/packages/client/lib/cluster/index.ts b/packages/client/lib/cluster/index.ts index 96560fdaf0..abec4cb7f3 100644 --- a/packages/client/lib/cluster/index.ts +++ b/packages/client/lib/cluster/index.ts @@ -503,14 +503,28 @@ export default class RedisCluster< sUnsubscribe = this.SUNSUBSCRIBE; - // quit(): Promise { - // return this.#slots.quit(); - // } + /** + * @deprecated Use `close` instead. + */ + quit() { + return this._slots.quit(); + } - disconnect(): Promise { + /** + * @deprecated Use `destroy` instead. + */ + disconnect() { return this._slots.disconnect(); } + close() { + return this._slots.close(); + } + + destroy() { + return this._slots.destroy(); + } + nodeClient(node: ShardNode) { return this._slots.nodeClient(node); }