OPTIM: lb-chash: limit the scan to few consecutive nodes
Strongly imbalanced configs such as one server of weight 256 and another
one of weight 1 can result in high CPU usage with consistent hashing when
the first attempted server is the one to be avoided, or when affinity is
set to maxconn or maxqueue, because in this case, the frequent server may
appear a lot of consecutive times, on average 256 after a node before
meeting the other server, and if terribly unlucky, 4096 times.
Also in properly balanced farms, all servers are more or less well
distributed, so that after visiting a number of them we've seen them
all.
This patch takes this into account to avoid too long scans of the whole
tree caused by such imbalances. What it does is:
- limit the search of another server to 3 consecutive nodes of the
same server. If this happens, the tree is severely imbalanced so
there's no point looking for another server: not only will this
take a lot of time, but it may even result in a lot of traffic
being transferred to it at ratios well above the weights ratios ;
- limit the search of an eligible server to twice the number of servers
in the farm, considering that once we've witnessed 2N server changes,
it's fairly unlikely that a remaining server is worth being considered
beyond that point (as a reminder, each server is present 16 times).
For the last point we could also have decided to stop based on the
distance, e.g. 1/8 of the whole spectrum, but it would be more
probabilistic and there it remains understandable.