private Routee SelectNext(Routee[] routees)
{
var winningScore = long.MaxValue;
// round robin fallback
var winner = routees[(Interlocked.Increment(ref _next) & int.MaxValue) % routees.Length];
for (int i = 0; i < routees.Length; i++)
{
var routee = routees[i];
var cell = TryGetActorCell(routee);
if (cell != null)
{
// routee can be reasoned about it's mailbox size
var score = cell.NumberOfMessages;
if (score == 0)
{
// no messages => instant win
return routee;
}
if (winningScore > score)
{
winningScore = score;
winner = routee;
}
}
}
return winner;
}