Skip to content

Commit 09d141a

Browse files
address comments
1 parent 2582654 commit 09d141a

File tree

2 files changed

+18
-34
lines changed

2 files changed

+18
-34
lines changed

src/cmap/connection_pool.ts

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
@@ -454,9 +452,9 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
454452
);
455453
}
456454

457-
process.nextTick(() =>
458-
this.pruneConnections({ minGeneration: oldGeneration, interruptInUseConnections })
459-
);
455+
if (interruptInUseConnections) {
456+
process.nextTick(() => this.interruptInUseConnections(oldGeneration));
457+
}
460458

461459
this.processWaitQueue();
462460
}
@@ -468,41 +466,20 @@ 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({
472-
interruptInUseConnections,
473-
minGeneration
474-
}: {
475-
interruptInUseConnections: boolean;
476-
minGeneration: number;
477-
}) {
478-
this[kConnections].prune(connection => {
469+
private interruptInUseConnections(minGeneration: number) {
470+
for (const connection of this[kCheckedOut]) {
479471
if (connection.generation <= minGeneration) {
472+
this[kCheckedOut].delete(connection);
480473
connection.onError(new PoolClearedOnNetworkError(this));
481474
this.emit(
482475
ConnectionPool.CONNECTION_CLOSED,
483476
new ConnectionClosedEvent(this, connection, 'stale')
484477
);
485-
486-
return true;
487478
}
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');
505479
}
480+
481+
// TODO(NODE-4784): track pending connections and cancel
482+
// this[kCancellationToken].emit('cancel');
506483
}
507484

508485
/** Close the pool */

test/integration/connection-monitoring-and-pooling/connection_monitoring_and_pooling.spec.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ const INTERRUPT_IN_USE_SKIPPED_TESTS: SkipDescription[] = [
2424
description: 'clear with interruptInUseConnections = true closes pending connections',
2525
skipIfCondition: 'always',
2626
skipReason: 'TODO(NODE-4784): track and kill pending connections'
27+
},
28+
{
29+
description:
30+
'Pool clear SHOULD schedule the next background thread run immediately (interruptInUseConnections: false)',
31+
skipIfCondition: 'always',
32+
skipReason:
33+
'NodeJS does not have a background thread responsible for managing connections, and so already checked in connections are not pruned when in-use connections are interrupted.'
2734
}
2835
];
2936

0 commit comments

Comments
 (0)