]> git.saurik.com Git - apt.git/commitdiff
WIP start randomizing
authorMichael Vogt <mvo@ubuntu.com>
Fri, 23 May 2014 15:24:17 +0000 (17:24 +0200)
committerMichael Vogt <mvo@ubuntu.com>
Fri, 23 May 2014 15:24:17 +0000 (17:24 +0200)
apt-pkg/contrib/srvrec.cc
apt-pkg/contrib/srvrec.h

index c473192fc8a8a7dfd42fc920337b2139993378d9..70247c2aec9e286355dff23526d19928e5a53248 100644 (file)
@@ -105,10 +105,37 @@ bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result)
       Result.push_back(rec);
    }
 
-   // sort them
+   // implement load balancing as specified in RFC-2782
+
+   // sort them by priority
    std::stable_sort(Result.begin(), Result.end());
 
-   // FIXME: implement weight selection as specified in RFC-2782
+   // assign random number ranges
+   int prev_weight = 0;
+   int prev_priority = 0;
+   for(std::vector<SrvRec>::iterator I = Result.begin();
+      I != Result.end(); ++I)
+   {
+      if(prev_priority != I->priority)
+         prev_weight = 0;
+      I->random_number_range_start = prev_weight;
+      I->random_number_range_end = prev_weight + I->weight;
+      prev_weight = I->random_number_range_end;
+      prev_priority = I->priority;
+   }
+
+   // go over the code in reverse order and note the max random range
+   int max = 0;
+   prev_priority = 0;
+   for(std::vector<SrvRec>::iterator I = Result.end();
+      I != Result.begin(); --I)
+   {
+      if(prev_priority != I->priority)
+         max = I->random_number_range_end;
+      I->random_number_range_max = max;
+   }
+
+   // now shuffle 
 
    return true;
 }
index fd71e697f1eff67605d217176a463c55d9686aa9..79b318b48e23c0657b308fe8edfa179934a086ec 100644 (file)
@@ -21,6 +21,11 @@ class SrvRec
    u_int16_t weight;
    u_int16_t port;
 
+   // each server is assigned a interval [start, end] in the space of [0, max]
+   int random_number_range_start;
+   int random_number_range_end;
+   int random_number_range_max;
+
    bool operator<(SrvRec const &other) const { 
       return this->priority < other.priority; 
    }