@@ -259,7 +259,7 @@ export default class RedisClusterSlots<
259
259
// switch to `CLUSTER SHARDS` when Redis 7.0 will be the minimum supported version
260
260
return await client . clusterSlots ( ) ;
261
261
} finally {
262
- await client . disconnect ( ) ;
262
+ client . destroy ( ) ;
263
263
}
264
264
}
265
265
@@ -377,41 +377,67 @@ export default class RedisClusterSlots<
377
377
return this . _discoverWithRootNodes ( ) ;
378
378
}
379
379
380
+ /**
381
+ * @deprecated Use `close` instead.
382
+ */
380
383
quit ( ) : Promise < void > {
381
384
return this . _destroy ( client => client . quit ( ) ) ;
382
385
}
383
386
387
+ /**
388
+ * @deprecated Use `destroy` instead.
389
+ */
384
390
disconnect ( ) : Promise < void > {
385
391
return this . _destroy ( client => client . disconnect ( ) ) ;
386
392
}
387
393
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 ( ) {
389
399
this . _isOpen = false ;
390
400
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 ( ) {
392
415
for ( const { master, replicas } of this . shards ) {
393
416
if ( master . client ) {
394
- promises . push (
395
- this . _execOnNodeClient ( master . client , fn )
396
- ) ;
417
+ yield master . client ;
397
418
}
398
419
399
420
if ( master . pubSubClient ) {
400
- promises . push (
401
- this . _execOnNodeClient ( master . pubSubClient , fn )
402
- ) ;
421
+ yield master . pubSubClient ;
403
422
}
404
423
405
424
if ( replicas ) {
406
425
for ( const { client } of replicas ) {
407
426
if ( client ) {
408
- promises . push (
409
- this . _execOnNodeClient ( client , fn )
410
- ) ;
427
+ yield client ;
411
428
}
412
429
}
413
430
}
414
431
}
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
+ }
415
441
416
442
if ( this . pubSubNode ) {
417
443
promises . push ( this . _execOnNodeClient ( this . pubSubNode . client , fn ) ) ;
@@ -424,10 +450,10 @@ export default class RedisClusterSlots<
424
450
await Promise . allSettled ( promises ) ;
425
451
}
426
452
427
- private _execOnNodeClient (
453
+ private _execOnNodeClient < T > (
428
454
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 > {
431
457
return types . isPromise ( client ) ?
432
458
client . then ( fn ) :
433
459
fn ( client ) ;
0 commit comments