Skip to content

Commit bf000ae

Browse files
refactor(NODE-4689): track checked out connections (#3440)
1 parent 9654442 commit bf000ae

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/cmap/connection_pool.ts

+19-22
Original file line numberDiff line numberDiff line change
@@ -134,38 +134,25 @@ export type ConnectionPoolEvents = {
134134
*/
135135
export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
136136
options: Readonly<ConnectionPoolOptions>;
137-
/** @internal */
138137
[kPoolState]: typeof PoolState[keyof typeof PoolState];
139-
/** @internal */
140138
[kServer]: Server;
141-
/** @internal */
142139
[kLogger]: Logger;
143-
/** @internal */
144140
[kConnections]: Denque<Connection>;
145-
/** @internal */
146141
[kPending]: number;
147-
/** @internal */
148-
[kCheckedOut]: number;
149-
/** @internal */
142+
[kCheckedOut]: Set<Connection>;
150143
[kMinPoolSizeTimer]?: NodeJS.Timeout;
151144
/**
152145
* An integer representing the SDAM generation of the pool
153-
* @internal
154146
*/
155147
[kGeneration]: number;
156-
/** A map of generations to service ids
157-
* @internal
148+
/**
149+
* A map of generations to service ids
158150
*/
159151
[kServiceGenerations]: Map<string, number>;
160-
/** @internal */
161152
[kConnectionCounter]: Generator<number>;
162-
/** @internal */
163153
[kCancellationToken]: CancellationToken;
164-
/** @internal */
165154
[kWaitQueue]: Denque<WaitQueueMember>;
166-
/** @internal */
167155
[kMetrics]: ConnectionPoolMetrics;
168-
/** @internal */
169156
[kProcessingWaitQueue]: boolean;
170157

171158
/**
@@ -224,7 +211,6 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
224211
*/
225212
static readonly CONNECTION_CHECKED_IN = CONNECTION_CHECKED_IN;
226213

227-
/** @internal */
228214
constructor(server: Server, options: ConnectionPoolOptions) {
229215
super();
230216

@@ -252,7 +238,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
252238
this[kLogger] = new Logger('ConnectionPool');
253239
this[kConnections] = new Denque();
254240
this[kPending] = 0;
255-
this[kCheckedOut] = 0;
241+
this[kCheckedOut] = new Set();
256242
this[kMinPoolSizeTimer] = undefined;
257243
this[kGeneration] = 0;
258244
this[kServiceGenerations] = new Map();
@@ -304,7 +290,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
304290
}
305291

306292
get currentCheckedOutCount(): number {
307-
return this[kCheckedOut];
293+
return this[kCheckedOut].size;
308294
}
309295

310296
get waitQueueSize(): number {
@@ -323,6 +309,17 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
323309
return this[kServer].description.error;
324310
}
325311

312+
/**
313+
* This is exposed ONLY for use in mongosh, to enable
314+
* killing all connections if a user quits the shell with
315+
* operations in progress.
316+
*
317+
* This property may be removed as a part of NODE-3263.
318+
*/
319+
get checkedOutConnections() {
320+
return this[kCheckedOut];
321+
}
322+
326323
/**
327324
* Get the metrics information for the pool when a wait queue timeout occurs.
328325
*/
@@ -395,7 +392,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
395392
this[kConnections].unshift(connection);
396393
}
397394

398-
this[kCheckedOut]--;
395+
this[kCheckedOut].delete(connection);
399396
this.emit(ConnectionPool.CONNECTION_CHECKED_IN, new ConnectionCheckedInEvent(this, connection));
400397

401398
if (willDestroy) {
@@ -744,7 +741,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
744741
}
745742

746743
if (!this.connectionIsPerished(connection)) {
747-
this[kCheckedOut]++;
744+
this[kCheckedOut].add(connection);
748745
this.emit(
749746
ConnectionPool.CONNECTION_CHECKED_OUT,
750747
new ConnectionCheckedOutEvent(this, connection)
@@ -780,7 +777,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
780777
new ConnectionCheckOutFailedEvent(this, 'connectionError')
781778
);
782779
} else if (connection) {
783-
this[kCheckedOut]++;
780+
this[kCheckedOut].add(connection);
784781
this.emit(
785782
ConnectionPool.CONNECTION_CHECKED_OUT,
786783
new ConnectionCheckedOutEvent(this, connection)

0 commit comments

Comments
 (0)