crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs | LICENSE

target.cc (1600B)


      1 #include "leastconn"
      2 
      3 unsigned Leastconn::target(struct in_addr clientip,
      4 			   BackendVector &targetlist) {
      5 
      6     PROFILE("Leastconn::target");
      7     msg("Starting least-connections dispatcher\n");
      8 
      9     if (config.debug()) {
     10 	ostringstream o;
     11 	o << "Back end target list:";
     12 	for (unsigned i = 0; i < targetlist.size(); i++)
     13 	    o << ' ' << targetlist[i];
     14 	o << '\n';
     15 	debugmsg(o.str());
     16     }
     17     
     18     bool found = false;
     19     unsigned best_weighted = 0, t = 0;
     20 
     21     for (unsigned i = 0; i < targetlist.size(); i++) {
     22 	if (! balancer.backend(targetlist[i]).available()) {
     23 	    debugmsg("Back end " <<
     24 		     balancer.backend(targetlist[i]).description() << 
     25 		     " is NOT available\n");
     26 	    continue;
     27 	}
     28 	unsigned connections = balancer.backend(targetlist[i]).connections();
     29 	unsigned anticipated = IPStore::anticipated(targetlist[i]);
     30 	unsigned adjweight   = balancer.backend(targetlist[i]).adjustedweight();
     31 	unsigned this_weight = (connections + anticipated) * adjweight;
     32 	ostringstream o;
     33 
     34 	if (config.debug())
     35 	    o << "Back end " << balancer.backend(targetlist[i]).description()
     36 	      << ": connections=" << connections
     37 	      << ", anticipated=" << anticipated
     38 	      << ", adjweight=" << adjweight
     39 	      << ", thisweight=" << this_weight;
     40 	
     41 	if (!found || this_weight < best_weighted) {
     42 	    t = targetlist[i];
     43 	    best_weighted = this_weight;
     44 	    found = true;
     45 	    debugmsg(o.str() << " is best so far\n");
     46 	} else
     47 	    debugmsg(o.str() <<  " skipped, got a better one\n");
     48     }
     49 
     50     if (!found)
     51 	throw Error("Least-connections algorithm: no available back ends");
     52     return (t);
     53 }