@@ -343,6 +343,8 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
343
343
topology ?: Topology ;
344
344
/** @internal */
345
345
readonly mongoLogger : MongoLogger ;
346
+ /** @internal */
347
+ private connectionLock ?: Promise < this> ;
346
348
347
349
/**
348
350
* The consolidate, parsed, transformed and merged options.
@@ -447,54 +449,66 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
447
449
}
448
450
449
451
return maybeCallback ( async ( ) => {
450
- if ( this . topology && this . topology . isConnected ( ) ) {
452
+ if ( this . connectionLock ) {
453
+ return this . connectionLock ;
454
+ }
455
+ try {
456
+ this . connectionLock = this . _connect ( ) ;
457
+ await this . connectionLock ;
451
458
return this ;
459
+ } finally {
460
+ this . connectionLock = undefined ;
452
461
}
462
+ } , callback ) ;
463
+ }
453
464
454
- const options = this [ kOptions ] ;
465
+ private async _connect ( ) : Promise < this> {
466
+ if ( this . topology && this . topology . isConnected ( ) ) {
467
+ return this ;
468
+ }
455
469
456
- if ( typeof options . srvHost === 'string' ) {
457
- const hosts = await resolveSRVRecord ( options ) ;
470
+ const options = this [ kOptions ] ;
458
471
459
- for ( const [ index , host ] of hosts . entries ( ) ) {
460
- options . hosts [ index ] = host ;
461
- }
472
+ if ( typeof options . srvHost === 'string' ) {
473
+ const hosts = await resolveSRVRecord ( options ) ;
474
+
475
+ for ( const [ index , host ] of hosts . entries ( ) ) {
476
+ options . hosts [ index ] = host ;
462
477
}
478
+ }
463
479
464
- const topology = new Topology ( options . hosts , options ) ;
465
- // Events can be emitted before initialization is complete so we have to
466
- // save the reference to the topology on the client ASAP if the event handlers need to access it
467
- this . topology = topology ;
468
- topology . client = this ;
480
+ const topology = new Topology ( options . hosts , options ) ;
481
+ // Events can be emitted before initialization is complete so we have to
482
+ // save the reference to the topology on the client ASAP if the event handlers need to access it
483
+ this . topology = topology ;
484
+ topology . client = this ;
469
485
470
- topology . once ( Topology . OPEN , ( ) => this . emit ( 'open' , this ) ) ;
486
+ topology . once ( Topology . OPEN , ( ) => this . emit ( 'open' , this ) ) ;
471
487
472
- for ( const event of MONGO_CLIENT_EVENTS ) {
473
- topology . on ( event , ( ...args : any [ ] ) : unknown => this . emit ( event , ...( args as any ) ) ) ;
474
- }
488
+ for ( const event of MONGO_CLIENT_EVENTS ) {
489
+ topology . on ( event , ( ...args : any [ ] ) : unknown => this . emit ( event , ...( args as any ) ) ) ;
490
+ }
475
491
476
- const topologyConnect = async ( ) => {
477
- try {
478
- await promisify ( callback => topology . connect ( options , callback ) ) ( ) ;
479
- } catch ( error ) {
480
- topology . close ( { force : true } ) ;
481
- throw error ;
482
- }
483
- } ;
484
-
485
- if ( this . autoEncrypter ) {
486
- const initAutoEncrypter = promisify ( callback => this . autoEncrypter ?. init ( callback ) ) ;
487
- await initAutoEncrypter ( ) ;
488
- await topologyConnect ( ) ;
489
- await options . encrypter . connectInternalClient ( ) ;
490
- } else {
491
- await topologyConnect ( ) ;
492
+ const topologyConnect = async ( ) => {
493
+ try {
494
+ await promisify ( callback => topology . connect ( options , callback ) ) ( ) ;
495
+ } catch ( error ) {
496
+ topology . close ( { force : true } ) ;
497
+ throw error ;
492
498
}
499
+ } ;
493
500
494
- return this ;
495
- } , callback ) ;
496
- }
501
+ if ( this . autoEncrypter ) {
502
+ const initAutoEncrypter = promisify ( callback => this . autoEncrypter ?. init ( callback ) ) ;
503
+ await initAutoEncrypter ( ) ;
504
+ await topologyConnect ( ) ;
505
+ await options . encrypter . connectInternalClient ( ) ;
506
+ } else {
507
+ await topologyConnect ( ) ;
508
+ }
497
509
510
+ return this ;
511
+ }
498
512
/**
499
513
* Close the db and its underlying connections
500
514
*
0 commit comments