crossroads

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

target.cc (1443B)


      1 #include "weightedload"
      2 
      3 unsigned Weightedload::target(struct in_addr clientip,
      4 			   BackendVector &targetlist) {
      5     // First loop thru and add up the weights.
      6     double total_load = 0;
      7     for (unsigned i = 0; i < targetlist.size(); i++) {
      8 	if (balancer.backend(targetlist[i]).loadavg() == 0)
      9 	    total_load += 1 / 0.01;
     10 	else
     11 	    total_load += 1 / balancer.backend(targetlist[i]).loadavg();
     12     }
     13 
     14     // Now pick a random number from 0 to total_load
     15     // 4294967295 = 2^32 - 1
     16     double pick_load = total_load * mt_rand() * (1.0 / 4294967295.0);
     17 
     18     msg ("Weighted by Load Average; load-range is " << total_load <<
     19 	 ", and the selected load-range is " << pick_load << '\n');
     20 
     21     // Now see which server that means!
     22     total_load = 0;
     23     for (unsigned i = 0; i < targetlist.size(); i++) {
     24 	if (balancer.backend(targetlist[i]).loadavg() == 0)
     25 	    total_load += 1 / 0.01;
     26 	else
     27 	    total_load += 1 / balancer.backend(targetlist[i]).loadavg();
     28 	if (total_load >= pick_load) {
     29 // 	    if (config.verbose()) {
     30 // 		ostringstream o;
     31 // 		o << balancer.backend(targetlist[i]).loadavg();
     32 // 		msg ("Weighted by Load Average chose backend " +
     33 // 		 (string)balancer.backend(i).description() + " which has a " +
     34 // 		 "load average of " + o.str() + "\n");
     35 // 	    }
     36 	    return targetlist[i];
     37 	}
     38     }
     39 
     40 
     41     throw Error("Weighted-load algorithm: no available back ends ");
     42     return targetlist[0]; // We need some kind of default...
     43 }