@@ -572,6 +572,7 @@ function prepareConnectionOptions(conn, _options) {
572
572
options = { port : conn . port , host : conn . host } ;
573
573
}
574
574
}
575
+
575
576
return options ;
576
577
}
577
578
@@ -598,19 +599,13 @@ function makeConnection(conn, options, callback) {
598
599
599
600
// Add handlers for events
600
601
connection . once ( 'error' , err => callback ( err , null ) ) ;
601
-
602
- return ;
603
602
}
604
603
605
604
function normalConnect ( conn , family , _options , callback ) {
606
605
const options = prepareConnectionOptions ( conn , _options ) ;
607
606
makeConnection ( conn , Object . assign ( { } , { family : family } , options ) , ( err , connection ) => {
608
- if ( connection ) {
609
- return callback ( null , connection ) ;
610
- }
611
- if ( err ) {
612
- return callback ( err , null ) ;
613
- }
607
+ if ( err ) return callback ( err , null ) ;
608
+ callback ( null , connection ) ;
614
609
} ) ;
615
610
}
616
611
@@ -620,27 +615,28 @@ function fastFallbackConnect(conn, _options, callback) {
620
615
let errors = [ ] ;
621
616
let connection ;
622
617
const connectionHandler = ( err , _connection ) => {
623
- if ( _connection ) {
624
- if ( connection ) {
625
- _connection . removeAllListeners ( 'error' ) ;
626
- _connection . unref ( ) ;
627
- return ;
628
- }
629
- connection = _connection ;
630
- return callback ( null , connection ) ;
631
- }
632
618
if ( err ) {
633
619
if ( errors . length > 0 ) {
634
620
// an error occurred for the second time, we have officially failed
635
-
636
621
// return mongo error to be emitted
637
622
return callback ( err , null ) ;
638
623
}
624
+
639
625
// otherwise push the error, and wait for subsequent connects
640
- // if (connectionAccounting) deleteConnection(conn.id);
641
626
errors . push ( err ) ;
642
627
return ;
643
628
}
629
+
630
+ if ( _connection ) {
631
+ if ( connection ) {
632
+ _connection . removeAllListeners ( 'error' ) ;
633
+ _connection . unref ( ) ;
634
+ return ;
635
+ }
636
+
637
+ connection = _connection ;
638
+ return callback ( null , connection ) ;
639
+ }
644
640
} ;
645
641
646
642
makeConnection ( conn , Object . assign ( { } , { family : 6 } , options ) , connectionHandler ) ;
@@ -667,13 +663,15 @@ Connection.prototype.connect = function(_options) {
667
663
}
668
664
669
665
const connectHandler = ( err , connection ) => {
666
+ const connectionErrorHandler = errorHandler ( this ) ;
667
+
670
668
if ( err ) {
671
- const _errorHandler = errorHandler ( this ) ;
672
- _errorHandler ( err ) ;
669
+ connectionErrorHandler ( err ) ;
673
670
return ;
674
671
}
672
+
675
673
// Add handlers for events
676
- connection . once ( 'error' , errorHandler ( this ) ) ;
674
+ connection . once ( 'error' , connectionErrorHandler ) ;
677
675
connection . once ( 'timeout' , timeoutHandler ( this ) ) ;
678
676
connection . once ( 'close' , closeHandler ( this ) ) ;
679
677
connection . on ( 'data' , dataHandler ( this ) ) ;
0 commit comments