@@ -27,7 +27,7 @@ import {
27
27
import { CancellationToken , TypedEventEmitter } from '../mongo_types' ;
28
28
import type { Server } from '../sdam/server' ;
29
29
import { Timeout , TimeoutError } from '../timeout' ;
30
- import { type Callback , List , makeCounter , promiseWithResolvers } from '../utils' ;
30
+ import { type Callback , List , makeCounter , now , promiseWithResolvers } from '../utils' ;
31
31
import { connect } from './connect' ;
32
32
import { Connection , type ConnectionEvents , type ConnectionOptions } from './connection' ;
33
33
import {
@@ -104,6 +104,7 @@ export interface WaitQueueMember {
104
104
reject : ( err : AnyError ) => void ;
105
105
timeout : Timeout ;
106
106
[ kCancelled ] ?: boolean ;
107
+ checkoutTime : number ;
107
108
}
108
109
109
110
/** @internal */
@@ -355,6 +356,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
355
356
* explicitly destroyed by the new owner.
356
357
*/
357
358
async checkOut ( ) : Promise < Connection > {
359
+ const checkoutTime = now ( ) ;
358
360
this . emitAndLog (
359
361
ConnectionPool . CONNECTION_CHECK_OUT_STARTED ,
360
362
new ConnectionCheckOutStartedEvent ( this )
@@ -369,7 +371,8 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
369
371
const waitQueueMember : WaitQueueMember = {
370
372
resolve,
371
373
reject,
372
- timeout
374
+ timeout,
375
+ checkoutTime
373
376
} ;
374
377
375
378
this [ kWaitQueue ] . push ( waitQueueMember ) ;
@@ -385,7 +388,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
385
388
386
389
this . emitAndLog (
387
390
ConnectionPool . CONNECTION_CHECK_OUT_FAILED ,
388
- new ConnectionCheckOutFailedEvent ( this , 'timeout' )
391
+ new ConnectionCheckOutFailedEvent ( this , 'timeout' , waitQueueMember . checkoutTime )
389
392
) ;
390
393
const timeoutError = new WaitQueueTimeoutError (
391
394
this . loadBalanced
@@ -629,6 +632,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
629
632
630
633
this [ kPending ] ++ ;
631
634
// This is our version of a "virtual" no-I/O connection as the spec requires
635
+ const connectionCreatedTime = now ( ) ;
632
636
this . emitAndLog (
633
637
ConnectionPool . CONNECTION_CREATED ,
634
638
new ConnectionCreatedEvent ( this , { id : connectOptions . id } )
@@ -670,7 +674,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
670
674
connection . markAvailable ( ) ;
671
675
this . emitAndLog (
672
676
ConnectionPool . CONNECTION_READY ,
673
- new ConnectionReadyEvent ( this , connection )
677
+ new ConnectionReadyEvent ( this , connection , connectionCreatedTime )
674
678
) ;
675
679
676
680
this [ kPending ] -- ;
@@ -759,7 +763,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
759
763
const error = this . closed ? new PoolClosedError ( this ) : new PoolClearedError ( this ) ;
760
764
this . emitAndLog (
761
765
ConnectionPool . CONNECTION_CHECK_OUT_FAILED ,
762
- new ConnectionCheckOutFailedEvent ( this , reason , error )
766
+ new ConnectionCheckOutFailedEvent ( this , reason , waitQueueMember . checkoutTime , error )
763
767
) ;
764
768
waitQueueMember . timeout . clear ( ) ;
765
769
this [ kWaitQueue ] . shift ( ) ;
@@ -780,7 +784,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
780
784
this [ kCheckedOut ] . add ( connection ) ;
781
785
this . emitAndLog (
782
786
ConnectionPool . CONNECTION_CHECKED_OUT ,
783
- new ConnectionCheckedOutEvent ( this , connection )
787
+ new ConnectionCheckedOutEvent ( this , connection , waitQueueMember . checkoutTime )
784
788
) ;
785
789
waitQueueMember . timeout . clear ( ) ;
786
790
@@ -809,14 +813,19 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
809
813
this . emitAndLog (
810
814
ConnectionPool . CONNECTION_CHECK_OUT_FAILED ,
811
815
// TODO(NODE-5192): Remove this cast
812
- new ConnectionCheckOutFailedEvent ( this , 'connectionError' , err as MongoError )
816
+ new ConnectionCheckOutFailedEvent (
817
+ this ,
818
+ 'connectionError' ,
819
+ waitQueueMember . checkoutTime ,
820
+ err as MongoError
821
+ )
813
822
) ;
814
823
waitQueueMember . reject ( err ) ;
815
824
} else if ( connection ) {
816
825
this [ kCheckedOut ] . add ( connection ) ;
817
826
this . emitAndLog (
818
827
ConnectionPool . CONNECTION_CHECKED_OUT ,
819
- new ConnectionCheckedOutEvent ( this , connection )
828
+ new ConnectionCheckedOutEvent ( this , connection , waitQueueMember . checkoutTime )
820
829
) ;
821
830
waitQueueMember . resolve ( connection ) ;
822
831
}
0 commit comments