in folsom/src/main/java/com/spotify/folsom/ketama/Continuum.java [59:81]
public RawMemcacheClient findClient(final byte[] key) {
final int keyHash = Hasher.hash(key).asInt();
Entry<Integer, RawMemcacheClient> entry = ringOfFire.ceilingEntry(keyHash);
if (entry == null) {
// wrap around
entry = ringOfFire.firstEntry();
}
for (int i = 0; i < ringOfFire.size(); i++) {
// TODO: maybe loop for fewer rounds - what happens if all clients are disconnected?
final RawMemcacheClient client = findClient(entry);
if (client.isConnected()) {
return client;
}
entry = ringOfFire.higherEntry(entry.getKey());
if (entry == null) {
// wrap around
entry = ringOfFire.firstEntry();
}
}
return ringOfFire.firstEntry().getValue();
}