3
unsigned HashedIp::target(struct in_addr clientip,
4
BackendVector const &targetlist) {
6
// Nothing to do if we don't have targets.
7
if (!targetlist.size())
8
throw Error("Hashed-ip algorithm: no back ends to dispatch to");
10
// Hash the client's IP into an index
12
for (char *cp = (char*)&clientip;
13
unsigned(cp - (char*)&clientip) < sizeof(struct in_addr);
16
h %= targetlist.size();
18
unsigned index = targetlist[h];
20
msg ((Mstr("Client IP ") + static_cast<string>(inet_ntoa(clientip))) +
21
(Mstr(" hashes to ") + index) +
22
", back end " + balancer.backend(index).description() +
25
// In strict mode, back end must be available, or don't proceed
26
// In lax mode, fall back to least-connections dispatching
27
if (! balancer.backend(index).available()) {
28
if (config.dispatchmode() == Dispatchmode::m_strict_hashed_ip)
29
throw Error("Hashed-IP algorithm: target back end " +
30
balancer.backend(index).description() +
33
msg ("Hashed-IP algorithm: target back end " +
34
balancer.backend(index).description() + " unavailable, "
35
"falling back to least-connections\n");
37
index = l.target(clientip, targetlist);