Skip to content

Commit e27693f

Browse files
committed
cluster close & destroy
1 parent e332613 commit e27693f

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

packages/client/lib/cluster/cluster-slots.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export default class RedisClusterSlots<
259259
// switch to `CLUSTER SHARDS` when Redis 7.0 will be the minimum supported version
260260
return await client.clusterSlots();
261261
} finally {
262-
await client.disconnect();
262+
client.destroy();
263263
}
264264
}
265265

@@ -377,41 +377,67 @@ export default class RedisClusterSlots<
377377
return this._discoverWithRootNodes();
378378
}
379379

380+
/**
381+
* @deprecated Use `close` instead.
382+
*/
380383
quit(): Promise<void> {
381384
return this._destroy(client => client.quit());
382385
}
383386

387+
/**
388+
* @deprecated Use `destroy` instead.
389+
*/
384390
disconnect(): Promise<void> {
385391
return this._destroy(client => client.disconnect());
386392
}
387393

388-
private async _destroy(fn: (client: RedisClientType<M, F, S, RESP>) => Promise<unknown>): Promise<void> {
394+
close() {
395+
return this._destroy(client => client.close());
396+
}
397+
398+
destroy() {
389399
this._isOpen = false;
390400

391-
const promises = [];
401+
for (const client of this._clients()) {
402+
this._execOnNodeClient(client, client => client.destroy());
403+
}
404+
405+
if (this.pubSubNode) {
406+
this._execOnNodeClient(this.pubSubNode.client, client => client.destroy());
407+
this.pubSubNode = undefined;
408+
}
409+
410+
this._resetSlots();
411+
this.nodeByAddress.clear();
412+
}
413+
414+
private *_clients() {
392415
for (const { master, replicas } of this.shards) {
393416
if (master.client) {
394-
promises.push(
395-
this._execOnNodeClient(master.client, fn)
396-
);
417+
yield master.client;
397418
}
398419

399420
if (master.pubSubClient) {
400-
promises.push(
401-
this._execOnNodeClient(master.pubSubClient, fn)
402-
);
421+
yield master.pubSubClient;
403422
}
404423

405424
if (replicas) {
406425
for (const { client } of replicas) {
407426
if (client) {
408-
promises.push(
409-
this._execOnNodeClient(client, fn)
410-
);
427+
yield client;
411428
}
412429
}
413430
}
414431
}
432+
}
433+
434+
private async _destroy(fn: (client: RedisClientType<M, F, S, RESP>) => Promise<unknown>): Promise<void> {
435+
this._isOpen = false;
436+
437+
const promises = [];
438+
for (const client of this._clients()) {
439+
promises.push(this._execOnNodeClient(client, fn));
440+
}
415441

416442
if (this.pubSubNode) {
417443
promises.push(this._execOnNodeClient(this.pubSubNode.client, fn));
@@ -424,10 +450,10 @@ export default class RedisClusterSlots<
424450
await Promise.allSettled(promises);
425451
}
426452

427-
private _execOnNodeClient(
453+
private _execOnNodeClient<T>(
428454
client: ClientOrPromise<M, F, S, RESP>,
429-
fn: (client: RedisClientType<M, F, S, RESP>) => Promise<unknown>
430-
) {
455+
fn: (client: RedisClientType<M, F, S, RESP>) => T
456+
): T | Promise<T> {
431457
return types.isPromise(client) ?
432458
client.then(fn) :
433459
fn(client);

packages/client/lib/cluster/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,28 @@ export default class RedisCluster<
503503

504504
sUnsubscribe = this.SUNSUBSCRIBE;
505505

506-
// quit(): Promise<void> {
507-
// return this.#slots.quit();
508-
// }
506+
/**
507+
* @deprecated Use `close` instead.
508+
*/
509+
quit() {
510+
return this._slots.quit();
511+
}
509512

510-
disconnect(): Promise<void> {
513+
/**
514+
* @deprecated Use `destroy` instead.
515+
*/
516+
disconnect() {
511517
return this._slots.disconnect();
512518
}
513519

520+
close() {
521+
return this._slots.close();
522+
}
523+
524+
destroy() {
525+
return this._slots.destroy();
526+
}
527+
514528
nodeClient(node: ShardNode<M, F, S, RESP>) {
515529
return this._slots.nodeClient(node);
516530
}

0 commit comments

Comments
 (0)