@@ -121,7 +121,17 @@ public MongoConnectionPool ConnectionPool
121
121
/// </summary>
122
122
public IPEndPoint EndPoint
123
123
{
124
- get { return _endPoint ; }
124
+ get {
125
+ // use a lock free algorithm because DNS lookups are rare and concurrent lookups are tolerable
126
+ // the intermediate variable is important to avoid race conditions
127
+ var endPoint = _endPoint ;
128
+ if ( endPoint == null )
129
+ {
130
+ endPoint = _address . ToIPEndPoint ( _server . Settings . AddressFamily ) ;
131
+ _endPoint = endPoint ;
132
+ }
133
+ return endPoint ;
134
+ }
125
135
}
126
136
127
137
/// <summary>
@@ -307,8 +317,6 @@ internal void Connect(bool slaveOk)
307
317
_connectException = null ;
308
318
try
309
319
{
310
- _endPoint = _address . ToIPEndPoint ( _server . Settings . AddressFamily ) ;
311
-
312
320
try
313
321
{
314
322
var connection = _connectionPool . AcquireConnection ( null ) ;
@@ -454,6 +462,38 @@ internal void VerifyState(MongoConnection connection)
454
462
_maxMessageLength = maxMessageLength ;
455
463
_buildInfo = buildInfo ;
456
464
this . SetState ( MongoServerState . Connected , isPrimary , isSecondary , isPassive , isArbiter ) ;
465
+
466
+ // if this is the primary of a replica set check to see if any instances have been added or removed
467
+ if ( isPrimary && _server . Settings . ConnectionMode == ConnectionMode . ReplicaSet )
468
+ {
469
+ var instanceAddresses = new List < MongoServerAddress > ( ) ;
470
+ if ( isMasterResult . Response . Contains ( "hosts" ) )
471
+ {
472
+ foreach ( var hostName in isMasterResult . Response [ "hosts" ] . AsBsonArray )
473
+ {
474
+ var address = MongoServerAddress . Parse ( hostName . AsString ) ;
475
+ instanceAddresses . Add ( address ) ;
476
+ }
477
+ }
478
+ if ( isMasterResult . Response . Contains ( "passives" ) )
479
+ {
480
+ foreach ( var hostName in isMasterResult . Response [ "passives" ] . AsBsonArray )
481
+ {
482
+ var address = MongoServerAddress . Parse ( hostName . AsString ) ;
483
+ instanceAddresses . Add ( address ) ;
484
+ }
485
+ }
486
+ if ( isMasterResult . Response . Contains ( "arbiters" ) )
487
+ {
488
+ foreach ( var hostName in isMasterResult . Response [ "arbiters" ] . AsBsonArray )
489
+ {
490
+ var address = MongoServerAddress . Parse ( hostName . AsString ) ;
491
+ instanceAddresses . Add ( address ) ;
492
+ }
493
+ }
494
+ _server . VerifyInstances ( instanceAddresses ) ;
495
+ }
496
+
457
497
ok = true ;
458
498
}
459
499
finally
0 commit comments