@@ -87,14 +87,14 @@ protected override MongoServerInstance ChooseServerInstance(ConnectedInstanceCol
87
87
}
88
88
else
89
89
{
90
- return GetMatchingInstance ( connectedInstances , readPreference , secondaryAcceptableLatency ) ;
90
+ return GetMatchingInstance ( connectedInstances . GetSecondaries ( ) , readPreference , secondaryAcceptableLatency ) ;
91
91
}
92
92
93
93
case ReadPreferenceMode . Secondary :
94
- return GetMatchingInstance ( connectedInstances , readPreference , secondaryAcceptableLatency ) ;
94
+ return GetMatchingInstance ( connectedInstances . GetSecondaries ( ) , readPreference , secondaryAcceptableLatency ) ;
95
95
96
96
case ReadPreferenceMode . SecondaryPreferred :
97
- var secondary = GetMatchingInstance ( connectedInstances , readPreference , secondaryAcceptableLatency ) ;
97
+ var secondary = GetMatchingInstance ( connectedInstances . GetSecondaries ( ) , readPreference , secondaryAcceptableLatency ) ;
98
98
if ( secondary != null )
99
99
{
100
100
return secondary ;
@@ -105,7 +105,7 @@ protected override MongoServerInstance ChooseServerInstance(ConnectedInstanceCol
105
105
}
106
106
107
107
case ReadPreferenceMode . Nearest :
108
- return GetMatchingInstance ( connectedInstances , readPreference , secondaryAcceptableLatency ) ;
108
+ return GetMatchingInstance ( connectedInstances . GetPrimaryAndSecondaries ( ) , readPreference , secondaryAcceptableLatency ) ;
109
109
110
110
default :
111
111
throw new MongoInternalException ( "Invalid ReadPreferenceMode." ) ;
@@ -203,13 +203,12 @@ protected override void ProcessConnectedInstanceStateChange(MongoServerInstance
203
203
/// <summary>
204
204
/// Gets a randomly selected matching instance.
205
205
/// </summary>
206
+ /// <param name="instancesWithPingTime">A list of instances from which to find a matching instance.</param>
206
207
/// <param name="readPreference">The read preference that must be matched.</param>
207
208
/// <param name="secondaryAcceptableLatency">The maximum acceptable secondary latency.</param>
208
209
/// <returns>A randomly selected matching instance.</returns>
209
- public MongoServerInstance GetMatchingInstance ( ConnectedInstanceCollection connectedInstances , ReadPreference readPreference , TimeSpan secondaryAcceptableLatency )
210
+ public MongoServerInstance GetMatchingInstance ( List < ConnectedInstanceCollection . InstanceWithPingTime > instancesWithPingTime , ReadPreference readPreference , TimeSpan secondaryAcceptableLatency )
210
211
{
211
- var instancesWithPingTime = connectedInstances . GetInstancesWithPingTime ( ) ;
212
-
213
212
var tagSets = readPreference . TagSets ?? new ReplicaSetTagSet [ ] { new ReplicaSetTagSet ( ) } ;
214
213
foreach ( var tagSet in tagSets )
215
214
{
@@ -224,15 +223,12 @@ public MongoServerInstance GetMatchingInstance(ConnectedInstanceCollection conne
224
223
}
225
224
226
225
var instance = instanceWithPingTime . Instance ;
227
- if ( instance . IsSecondary || ( readPreference . ReadPreferenceMode == ReadPreferenceMode . Nearest && instance . IsPrimary ) )
226
+ if ( tagSet . MatchesInstance ( instance ) )
228
227
{
229
- if ( tagSet . MatchesInstance ( instance ) )
228
+ matchingInstances . Add ( instance ) ;
229
+ if ( maxPingTime == TimeSpan . MaxValue )
230
230
{
231
- matchingInstances . Add ( instance ) ;
232
- if ( maxPingTime == TimeSpan . MaxValue )
233
- {
234
- maxPingTime = instanceWithPingTime . CachedAveragePingTime + secondaryAcceptableLatency ;
235
- }
231
+ maxPingTime = instanceWithPingTime . CachedAveragePingTime + secondaryAcceptableLatency ;
236
232
}
237
233
}
238
234
}
0 commit comments