Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Nov 18, 2022
1 parent 2582654 commit 09d141a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
45 changes: 11 additions & 34 deletions src/cmap/connection_pool.ts
Expand Up @@ -415,7 +415,6 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
*/
clear(options: { serviceId?: ObjectId; interruptInUseConnections?: boolean } = {}): void {
const { serviceId } = options;
const interruptInUseConnections = options.interruptInUseConnections ?? false;
if (this.closed) {
return;
}
Expand All @@ -438,10 +437,9 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
);
return;
}

const oldGeneration = this[kGeneration];

// handle non load-balanced case
const interruptInUseConnections = options.interruptInUseConnections ?? false;
const oldGeneration = this[kGeneration];
this[kGeneration] += 1;
const alreadyPaused = this[kPoolState] === PoolState.paused;
this[kPoolState] = PoolState.paused;
Expand All @@ -454,9 +452,9 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
);
}

process.nextTick(() =>
this.pruneConnections({ minGeneration: oldGeneration, interruptInUseConnections })
);
if (interruptInUseConnections) {
process.nextTick(() => this.interruptInUseConnections(oldGeneration));
}

this.processWaitQueue();
}
Expand All @@ -468,41 +466,20 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
* Only connections where `connection.generation <= minGeneration` are killed. Connections are closed with a
* resumable PoolClearedOnNetworkTimeoutError.
*/
private pruneConnections({
interruptInUseConnections,
minGeneration
}: {
interruptInUseConnections: boolean;
minGeneration: number;
}) {
this[kConnections].prune(connection => {
private interruptInUseConnections(minGeneration: number) {
for (const connection of this[kCheckedOut]) {
if (connection.generation <= minGeneration) {
this[kCheckedOut].delete(connection);
connection.onError(new PoolClearedOnNetworkError(this));
this.emit(
ConnectionPool.CONNECTION_CLOSED,
new ConnectionClosedEvent(this, connection, 'stale')
);

return true;
}
return false;
});

if (interruptInUseConnections) {
for (const connection of this[kCheckedOut]) {
if (connection.generation <= minGeneration) {
this[kCheckedOut].delete(connection);
connection.onError(new PoolClearedOnNetworkError(this));
this.emit(
ConnectionPool.CONNECTION_CLOSED,
new ConnectionClosedEvent(this, connection, 'stale')
);
}
}

// TODO(NODE-4784): track pending connections and cancel
// this[kCancellationToken].emit('cancel');
}

// TODO(NODE-4784): track pending connections and cancel
// this[kCancellationToken].emit('cancel');
}

/** Close the pool */
Expand Down
Expand Up @@ -24,6 +24,13 @@ const INTERRUPT_IN_USE_SKIPPED_TESTS: SkipDescription[] = [
description: 'clear with interruptInUseConnections = true closes pending connections',
skipIfCondition: 'always',
skipReason: 'TODO(NODE-4784): track and kill pending connections'
},
{
description:
'Pool clear SHOULD schedule the next background thread run immediately (interruptInUseConnections: false)',
skipIfCondition: 'always',
skipReason:
'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.'
}
];

Expand Down

0 comments on commit 09d141a

Please sign in to comment.