Skip to content

Commit 53c783f

Browse files
moved checkout time logic to waitQueueMember
1 parent 5f51f6a commit 53c783f

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/cmap/connection_pool.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export interface WaitQueueMember {
104104
reject: (err: AnyError) => void;
105105
timeout: Timeout;
106106
[kCancelled]?: boolean;
107+
checkoutTime: number;
107108
}
108109

109110
/** @internal */
@@ -162,7 +163,6 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
162163
[kWaitQueue]: List<WaitQueueMember>;
163164
[kMetrics]: ConnectionPoolMetrics;
164165
[kProcessingWaitQueue]: boolean;
165-
checkOutTime: undefined | number;
166166

167167
/**
168168
* Emitted when the connection pool is created.
@@ -356,7 +356,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
356356
* explicitly destroyed by the new owner.
357357
*/
358358
async checkOut(): Promise<Connection> {
359-
this.checkOutTime = Date.now();
359+
const checkoutTime = Date.now();
360360
this.emitAndLog(
361361
ConnectionPool.CONNECTION_CHECK_OUT_STARTED,
362362
new ConnectionCheckOutStartedEvent(this)
@@ -371,7 +371,8 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
371371
const waitQueueMember: WaitQueueMember = {
372372
resolve,
373373
reject,
374-
timeout
374+
timeout,
375+
checkoutTime
375376
};
376377

377378
this[kWaitQueue].push(waitQueueMember);
@@ -387,7 +388,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
387388

388389
this.emitAndLog(
389390
ConnectionPool.CONNECTION_CHECK_OUT_FAILED,
390-
new ConnectionCheckOutFailedEvent(this, 'timeout')
391+
new ConnectionCheckOutFailedEvent(this, 'timeout', waitQueueMember.checkoutTime)
391392
);
392393
const timeoutError = new WaitQueueTimeoutError(
393394
this.loadBalanced
@@ -762,7 +763,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
762763
const error = this.closed ? new PoolClosedError(this) : new PoolClearedError(this);
763764
this.emitAndLog(
764765
ConnectionPool.CONNECTION_CHECK_OUT_FAILED,
765-
new ConnectionCheckOutFailedEvent(this, reason, error)
766+
new ConnectionCheckOutFailedEvent(this, reason, waitQueueMember.checkoutTime, error)
766767
);
767768
waitQueueMember.timeout.clear();
768769
this[kWaitQueue].shift();
@@ -783,7 +784,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
783784
this[kCheckedOut].add(connection);
784785
this.emitAndLog(
785786
ConnectionPool.CONNECTION_CHECKED_OUT,
786-
new ConnectionCheckedOutEvent(this, connection)
787+
new ConnectionCheckedOutEvent(this, connection, waitQueueMember.checkoutTime)
787788
);
788789
waitQueueMember.timeout.clear();
789790

@@ -812,14 +813,19 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
812813
this.emitAndLog(
813814
ConnectionPool.CONNECTION_CHECK_OUT_FAILED,
814815
// TODO(NODE-5192): Remove this cast
815-
new ConnectionCheckOutFailedEvent(this, 'connectionError', err as MongoError)
816+
new ConnectionCheckOutFailedEvent(
817+
this,
818+
'connectionError',
819+
waitQueueMember.checkoutTime,
820+
err as MongoError
821+
)
816822
);
817823
waitQueueMember.reject(err);
818824
} else if (connection) {
819825
this[kCheckedOut].add(connection);
820826
this.emitAndLog(
821827
ConnectionPool.CONNECTION_CHECKED_OUT,
822-
new ConnectionCheckedOutEvent(this, connection)
828+
new ConnectionCheckedOutEvent(this, connection, waitQueueMember.checkoutTime)
823829
);
824830
waitQueueMember.resolve(connection);
825831
}

src/cmap/connection_pool_events.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,11 @@ export class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent
220220
constructor(
221221
pool: ConnectionPool,
222222
reason: 'poolClosed' | 'timeout' | 'connectionError',
223+
checkoutTime: number,
223224
error?: MongoError
224225
) {
225226
super(pool);
226-
this.durationMS = Date.now() - (pool.checkOutTime ?? 0);
227+
this.durationMS = Date.now() - checkoutTime;
227228
this.reason = reason;
228229
this.error = error;
229230
}
@@ -249,9 +250,9 @@ export class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent {
249250
durationMS: number;
250251

251252
/** @internal */
252-
constructor(pool: ConnectionPool, connection: Connection) {
253+
constructor(pool: ConnectionPool, connection: Connection, checkoutTime: number) {
253254
super(pool);
254-
this.durationMS = Date.now() - (pool.checkOutTime ?? 0);
255+
this.durationMS = Date.now() - checkoutTime;
255256
this.connectionId = connection.id;
256257
}
257258
}

0 commit comments

Comments
 (0)