@@ -27,7 +27,7 @@ import {
2727} from '../error' ;
2828import { CancellationToken , TypedEventEmitter } from '../mongo_types' ;
2929import type { Server } from '../sdam/server' ;
30- import { type Callback , eachAsync , List , makeCounter } from '../utils' ;
30+ import { type Callback , eachAsync , List , makeCounter , TimeoutController } from '../utils' ;
3131import { AUTH_PROVIDERS , connect } from './connect' ;
3232import { Connection , type ConnectionEvents , type ConnectionOptions } from './connection' ;
3333import {
@@ -101,7 +101,7 @@ export interface ConnectionPoolOptions extends Omit<ConnectionOptions, 'id' | 'g
101101/** @internal */
102102export interface WaitQueueMember {
103103 callback : Callback < Connection > ;
104- timer ?: NodeJS . Timeout ;
104+ timeoutController : TimeoutController ;
105105 [ kCancelled ] ?: boolean ;
106106}
107107
@@ -356,27 +356,29 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
356356 new ConnectionCheckOutStartedEvent ( this )
357357 ) ;
358358
359- const waitQueueMember : WaitQueueMember = { callback } ;
360359 const waitQueueTimeoutMS = this . options . waitQueueTimeoutMS ;
361- if ( waitQueueTimeoutMS ) {
362- waitQueueMember . timer = setTimeout ( ( ) => {
363- waitQueueMember [ kCancelled ] = true ;
364- waitQueueMember . timer = undefined ;
365360
366- this . emitAndLog (
367- ConnectionPool . CONNECTION_CHECK_OUT_FAILED ,
368- new ConnectionCheckOutFailedEvent ( this , 'timeout' )
369- ) ;
370- waitQueueMember . callback (
371- new WaitQueueTimeoutError (
372- this . loadBalanced
373- ? this . waitQueueErrorMetrics ( )
374- : 'Timed out while checking out a connection from connection pool' ,
375- this . address
376- )
377- ) ;
378- } , waitQueueTimeoutMS ) ;
379- }
361+ const waitQueueMember : WaitQueueMember = {
362+ callback,
363+ timeoutController : new TimeoutController ( waitQueueTimeoutMS )
364+ } ;
365+ waitQueueMember . timeoutController . signal . addEventListener ( 'abort' , ( ) => {
366+ waitQueueMember [ kCancelled ] = true ;
367+ waitQueueMember . timeoutController . clear ( ) ;
368+
369+ this . emitAndLog (
370+ ConnectionPool . CONNECTION_CHECK_OUT_FAILED ,
371+ new ConnectionCheckOutFailedEvent ( this , 'timeout' )
372+ ) ;
373+ waitQueueMember . callback (
374+ new WaitQueueTimeoutError (
375+ this . loadBalanced
376+ ? this . waitQueueErrorMetrics ( )
377+ : 'Timed out while checking out a connection from connection pool' ,
378+ this . address
379+ )
380+ ) ;
381+ } ) ;
380382
381383 this [ kWaitQueue ] . push ( waitQueueMember ) ;
382384 process . nextTick ( ( ) => this . processWaitQueue ( ) ) ;
@@ -831,9 +833,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
831833 ConnectionPool . CONNECTION_CHECK_OUT_FAILED ,
832834 new ConnectionCheckOutFailedEvent ( this , reason , error )
833835 ) ;
834- if ( waitQueueMember . timer ) {
835- clearTimeout ( waitQueueMember . timer ) ;
836- }
836+ waitQueueMember . timeoutController . clear ( ) ;
837837 this [ kWaitQueue ] . shift ( ) ;
838838 waitQueueMember . callback ( error ) ;
839839 continue ;
@@ -854,9 +854,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
854854 ConnectionPool . CONNECTION_CHECKED_OUT ,
855855 new ConnectionCheckedOutEvent ( this , connection )
856856 ) ;
857- if ( waitQueueMember . timer ) {
858- clearTimeout ( waitQueueMember . timer ) ;
859- }
857+ waitQueueMember . timeoutController . clear ( ) ;
860858
861859 this [ kWaitQueue ] . shift ( ) ;
862860 waitQueueMember . callback ( undefined , connection ) ;
@@ -893,9 +891,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
893891 ) ;
894892 }
895893
896- if ( waitQueueMember . timer ) {
897- clearTimeout ( waitQueueMember . timer ) ;
898- }
894+ waitQueueMember . timeoutController . clear ( ) ;
899895 waitQueueMember . callback ( err , connection ) ;
900896 }
901897 process . nextTick ( ( ) => this . processWaitQueue ( ) ) ;
0 commit comments