~ubuntu-branches/ubuntu/precise/crossroads/precise

« back to all changes in this revision

Viewing changes to xr/DispatchAlgorithms/hashedip/target.cc

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ritter
  • Date: 2010-07-05 16:27:00 UTC
  • Revision ID: james.westby@ubuntu.com-20100705162700-0g08tfav8ee9y51u
Tags: upstream-2.65
ImportĀ upstreamĀ versionĀ 2.65

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "hashedip"
 
2
 
 
3
unsigned HashedIp::target(struct in_addr clientip,
 
4
                     BackendVector const &targetlist) {
 
5
 
 
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");
 
9
    
 
10
    // Hash the client's IP into an index
 
11
    unsigned h = 0;
 
12
    for (char *cp = (char*)&clientip;
 
13
         unsigned(cp - (char*)&clientip) < sizeof(struct in_addr);
 
14
         cp++) {
 
15
        h += *cp;
 
16
        h %= targetlist.size();
 
17
    }
 
18
    unsigned index = targetlist[h];
 
19
 
 
20
    msg ((Mstr("Client IP ") + static_cast<string>(inet_ntoa(clientip))) +
 
21
         (Mstr(" hashes to ") + index) +
 
22
         ", back end " + balancer.backend(index).description() +
 
23
         "\n");
 
24
 
 
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() +
 
31
                        " unavailable");
 
32
        else {
 
33
            msg ("Hashed-IP algorithm: target back end " +
 
34
                 balancer.backend(index).description() + " unavailable, "
 
35
                 "falling back to least-connections\n");
 
36
            Leastconn l;
 
37
            index = l.target(clientip, targetlist);
 
38
        }
 
39
    }
 
40
 
 
41
    // Got it
 
42
    return (index);
 
43
}