@@ -415,7 +415,6 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
415415 */
416416 clear ( options : { serviceId ?: ObjectId ; interruptInUseConnections ?: boolean } = { } ) : void {
417417 const { serviceId } = options ;
418- const interruptInUseConnections = options . interruptInUseConnections ?? false ;
419418 if ( this . closed ) {
420419 return ;
421420 }
@@ -438,10 +437,9 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
438437 ) ;
439438 return ;
440439 }
441-
442- const oldGeneration = this [ kGeneration ] ;
443-
444440 // handle non load-balanced case
441+ const interruptInUseConnections = options . interruptInUseConnections ?? false ;
442+ const oldGeneration = this [ kGeneration ] ;
445443 this [ kGeneration ] += 1 ;
446444 const alreadyPaused = this [ kPoolState ] === PoolState . paused ;
447445 this [ kPoolState ] = PoolState . paused ;
@@ -455,7 +453,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
455453 }
456454
457455 process . nextTick ( ( ) =>
458- this . pruneConnections ( { minGeneration : oldGeneration , interruptInUseConnections } )
456+ this . interruptInUseConnections ( { interruptInUseConnections , minGeneration : oldGeneration } )
459457 ) ;
460458
461459 this . processWaitQueue ( ) ;
@@ -468,41 +466,29 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
468466 * Only connections where `connection.generation <= minGeneration` are killed. Connections are closed with a
469467 * resumable PoolClearedOnNetworkTimeoutError.
470468 */
471- private pruneConnections ( {
469+ private interruptInUseConnections ( {
472470 interruptInUseConnections,
473471 minGeneration
474472 } : {
475473 interruptInUseConnections : boolean ;
476474 minGeneration : number ;
477475 } ) {
478- this [ kConnections ] . prune ( connection => {
476+ if ( ! interruptInUseConnections ) {
477+ return ;
478+ }
479+ for ( const connection of this [ kCheckedOut ] ) {
479480 if ( connection . generation <= minGeneration ) {
481+ this [ kCheckedOut ] . delete ( connection ) ;
480482 connection . onError ( new PoolClearedOnNetworkError ( this ) ) ;
481483 this . emit (
482484 ConnectionPool . CONNECTION_CLOSED ,
483485 new ConnectionClosedEvent ( this , connection , 'stale' )
484486 ) ;
485-
486- return true ;
487487 }
488- return false ;
489- } ) ;
490-
491- if ( interruptInUseConnections ) {
492- for ( const connection of this [ kCheckedOut ] ) {
493- if ( connection . generation <= minGeneration ) {
494- this [ kCheckedOut ] . delete ( connection ) ;
495- connection . onError ( new PoolClearedOnNetworkError ( this ) ) ;
496- this . emit (
497- ConnectionPool . CONNECTION_CLOSED ,
498- new ConnectionClosedEvent ( this , connection , 'stale' )
499- ) ;
500- }
501- }
502-
503- // TODO(NODE-4784): track pending connections and cancel
504- // this[kCancellationToken].emit('cancel');
505488 }
489+
490+ // TODO(NODE-4784): track pending connections and cancel
491+ // this[kCancellationToken].emit('cancel');
506492 }
507493
508494 /** Close the pool */
0 commit comments