Skip to content

Commit

Permalink
moved checkout time logic to waitQueueMember
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Jul 3, 2024
1 parent 5f51f6a commit 53c783f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
22 changes: 14 additions & 8 deletions src/cmap/connection_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export interface WaitQueueMember {
reject: (err: AnyError) => void;
timeout: Timeout;
[kCancelled]?: boolean;
checkoutTime: number;
}

/** @internal */
Expand Down Expand Up @@ -162,7 +163,6 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
[kWaitQueue]: List<WaitQueueMember>;
[kMetrics]: ConnectionPoolMetrics;
[kProcessingWaitQueue]: boolean;
checkOutTime: undefined | number;

/**
* Emitted when the connection pool is created.
Expand Down Expand Up @@ -356,7 +356,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
* explicitly destroyed by the new owner.
*/
async checkOut(): Promise<Connection> {
this.checkOutTime = Date.now();
const checkoutTime = Date.now();
this.emitAndLog(
ConnectionPool.CONNECTION_CHECK_OUT_STARTED,
new ConnectionCheckOutStartedEvent(this)
Expand All @@ -371,7 +371,8 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
const waitQueueMember: WaitQueueMember = {
resolve,
reject,
timeout
timeout,
checkoutTime
};

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

this.emitAndLog(
ConnectionPool.CONNECTION_CHECK_OUT_FAILED,
new ConnectionCheckOutFailedEvent(this, 'timeout')
new ConnectionCheckOutFailedEvent(this, 'timeout', waitQueueMember.checkoutTime)
);
const timeoutError = new WaitQueueTimeoutError(
this.loadBalanced
Expand Down Expand Up @@ -762,7 +763,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
const error = this.closed ? new PoolClosedError(this) : new PoolClearedError(this);
this.emitAndLog(
ConnectionPool.CONNECTION_CHECK_OUT_FAILED,
new ConnectionCheckOutFailedEvent(this, reason, error)
new ConnectionCheckOutFailedEvent(this, reason, waitQueueMember.checkoutTime, error)
);
waitQueueMember.timeout.clear();
this[kWaitQueue].shift();
Expand All @@ -783,7 +784,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
this[kCheckedOut].add(connection);
this.emitAndLog(
ConnectionPool.CONNECTION_CHECKED_OUT,
new ConnectionCheckedOutEvent(this, connection)
new ConnectionCheckedOutEvent(this, connection, waitQueueMember.checkoutTime)
);
waitQueueMember.timeout.clear();

Expand Down Expand Up @@ -812,14 +813,19 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
this.emitAndLog(
ConnectionPool.CONNECTION_CHECK_OUT_FAILED,
// TODO(NODE-5192): Remove this cast
new ConnectionCheckOutFailedEvent(this, 'connectionError', err as MongoError)
new ConnectionCheckOutFailedEvent(
this,
'connectionError',
waitQueueMember.checkoutTime,
err as MongoError
)
);
waitQueueMember.reject(err);
} else if (connection) {
this[kCheckedOut].add(connection);
this.emitAndLog(
ConnectionPool.CONNECTION_CHECKED_OUT,
new ConnectionCheckedOutEvent(this, connection)
new ConnectionCheckedOutEvent(this, connection, waitQueueMember.checkoutTime)
);
waitQueueMember.resolve(connection);
}
Expand Down
7 changes: 4 additions & 3 deletions src/cmap/connection_pool_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,11 @@ export class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent
constructor(
pool: ConnectionPool,
reason: 'poolClosed' | 'timeout' | 'connectionError',
checkoutTime: number,
error?: MongoError
) {
super(pool);
this.durationMS = Date.now() - (pool.checkOutTime ?? 0);
this.durationMS = Date.now() - checkoutTime;
this.reason = reason;
this.error = error;
}
Expand All @@ -249,9 +250,9 @@ export class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent {
durationMS: number;

/** @internal */
constructor(pool: ConnectionPool, connection: Connection) {
constructor(pool: ConnectionPool, connection: Connection, checkoutTime: number) {
super(pool);
this.durationMS = Date.now() - (pool.checkOutTime ?? 0);
this.durationMS = Date.now() - checkoutTime;
this.connectionId = connection.id;
}
}
Expand Down

0 comments on commit 53c783f

Please sign in to comment.