public IPAddress Pick(BigInteger?token) { IPAddress endpoint = null; if (0 < _healthyEndpoints.Count) { if (token.HasValue && 0 < _ring.RingSize()) { //Attempt to binary search for key in token ring lock (_lock) { endpoint = _ring.FindReplica(token.Value); } } else { //fallback to round robin when no hint supplied lock (_lock) { _nextCandidate = (_nextCandidate + 1) % _healthyEndpoints.Count; endpoint = _healthyEndpoints[_nextCandidate]; } } } return(endpoint); }
public IPAddress Pick(BigInteger?token) { var currentHealthy = _healthyEndpoints; if (0 < currentHealthy.Length) { if (token.HasValue && 0 < _ring.RingSize()) { //Attempt to binary search for key in token ring lock (_ring) { return(_ring.FindReplica(token.Value)); } } else { //fallback to round robin when no hint supplied switch (currentHealthy.Length) { case 0: return(null); case 1: return(currentHealthy[0]); } int nextCandidate = Interlocked.Increment(ref _nextCandidate); if (nextCandidate < currentHealthy.Length) { return(currentHealthy[nextCandidate]); } var fix_candidate = nextCandidate % currentHealthy.Length; Interlocked.CompareExchange(ref _nextCandidate, fix_candidate, nextCandidate); return(currentHealthy[fix_candidate]); } } return(null); }