File tree Expand file tree Collapse file tree 2 files changed +19
-5
lines changed Expand file tree Collapse file tree 2 files changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -234,10 +234,17 @@ private MongoServerInstance GetMatchingInstance(List<ConnectedInstanceCollection
234
234
}
235
235
236
236
// stop looking at tagSets if this one yielded any matching instances
237
- if ( matchingInstances . Count != 0 )
237
+ if ( matchingInstances . Count == 1 )
238
238
{
239
- var index = _random . Next ( matchingInstances . Count ) ;
240
- return matchingInstances [ index ] ; // randomly selected matching instance
239
+ return matchingInstances [ 0 ] ;
240
+ }
241
+ else if ( matchingInstances . Count != 0 )
242
+ {
243
+ lock ( _randomLock )
244
+ {
245
+ var index = _random . Next ( matchingInstances . Count ) ;
246
+ return matchingInstances [ index ] ; // randomly selected matching instance
247
+ }
241
248
}
242
249
}
243
250
Original file line number Diff line number Diff line change @@ -64,14 +64,21 @@ protected override MongoServerInstance ChooseServerInstance(ConnectedInstanceCol
64
64
{
65
65
return null ;
66
66
}
67
+ else if ( instancesWithPingTime . Count == 1 )
68
+ {
69
+ return instancesWithPingTime [ 0 ] . Instance ;
70
+ }
67
71
else
68
72
{
69
73
var secondaryAcceptableLatency = Server . Settings . SecondaryAcceptableLatency ;
70
74
var minPingTime = instancesWithPingTime [ 0 ] . CachedAveragePingTime ;
71
75
var maxPingTime = minPingTime + secondaryAcceptableLatency ;
72
76
var n = instancesWithPingTime . Count ( i => i . CachedAveragePingTime <= maxPingTime ) ;
73
- var index = _random . Next ( n ) ;
74
- return instancesWithPingTime [ index ] . Instance ; // return random instance
77
+ lock ( _randomLock )
78
+ {
79
+ var index = _random . Next ( n ) ;
80
+ return instancesWithPingTime [ index ] . Instance ; // return random instance
81
+ }
75
82
}
76
83
}
77
84
You can’t perform that action at this time.
0 commit comments