// protected methods /// <summary> /// Chooses the server instance. /// </summary> /// <param name="connectedInstances">The connected instances.</param> /// <param name="readPreference">The read preference.</param> /// <returns>A MongoServerInstance.</returns> protected override MongoServerInstance ChooseServerInstance(ConnectedInstanceCollection connectedInstances, ReadPreference readPreference) { var instancesWithPingTime = connectedInstances.GetAllInstances(); if (instancesWithPingTime.Count == 0) { return(null); } else if (instancesWithPingTime.Count == 1) { return(instancesWithPingTime[0].Instance); } else { var secondaryAcceptableLatency = Settings.SecondaryAcceptableLatency; var minPingTime = instancesWithPingTime[0].CachedAveragePingTime; var maxPingTime = minPingTime + secondaryAcceptableLatency; var n = instancesWithPingTime.Count(i => i.CachedAveragePingTime <= maxPingTime); lock (_randomLock) { var index = _random.Next(n); return(instancesWithPingTime[index].Instance); // return random instance } } }
// protected methods /// <summary> /// Chooses the server instance. /// </summary> /// <param name="connectedInstances">The connected instances.</param> /// <param name="readPreference">The read preference.</param> /// <returns>A MongoServerInstance.</returns> protected override MongoServerInstance ChooseServerInstance(ConnectedInstanceCollection connectedInstances, ReadPreference readPreference) { var instancesWithPingTime = connectedInstances.GetAllInstances(); if (instancesWithPingTime.Count == 0) { return null; } else if (instancesWithPingTime.Count == 1) { return instancesWithPingTime[0].Instance; } else { var minPingTime = instancesWithPingTime[0].CachedAveragePingTime; var maxPingTime = minPingTime + readPreference.SecondaryAcceptableLatency; var n = instancesWithPingTime.Count(i => i.CachedAveragePingTime <= maxPingTime); lock (_randomLock) { var index = _random.Next(n); return instancesWithPingTime[index].Instance; // return random instance } } }