summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib/srvrec.cc
diff options
context:
space:
mode:
authorMichael Vogt <mvo@debian.org>2015-08-20 10:40:45 +0200
committerMichael Vogt <mvo@debian.org>2015-08-20 11:41:51 +0200
commitc29dbdffcb6f67812f823f1f844b87320cf6b437 (patch)
tree41a98cd8af9886a4cf68b68a4034d635b94737b2 /apt-pkg/contrib/srvrec.cc
parent9b70edba6796ebff3935af1cfb5c9bbc98d020b4 (diff)
Add basic (non weight adjusted) shuffling for SrvRecords selection
Also add "Debug::Acquire::SrvRecs" debug option and the option "Acquire::EnableSrvRecods" to allow disabling this lookup.
Diffstat (limited to 'apt-pkg/contrib/srvrec.cc')
-rw-r--r--apt-pkg/contrib/srvrec.cc59
1 files changed, 56 insertions, 3 deletions
diff --git a/apt-pkg/contrib/srvrec.cc b/apt-pkg/contrib/srvrec.cc
index 85241c1d7..b4a3d97d2 100644
--- a/apt-pkg/contrib/srvrec.cc
+++ b/apt-pkg/contrib/srvrec.cc
@@ -13,13 +13,18 @@
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
+#include <chrono>
#include <algorithm>
-#include <apt-pkg/strutl.h>
+#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
+#include <apt-pkg/strutl.h>
+
+
#include "srvrec.h"
+
bool GetSrvRecords(std::string host, int port, std::vector<SrvRec> &Result)
{
std::string target;
@@ -112,6 +117,29 @@ bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result)
// sort them by priority
std::stable_sort(Result.begin(), Result.end());
+ for(std::vector<SrvRec>::iterator I = Result.begin();
+ I != Result.end(); ++I)
+ {
+ if (_config->FindB("Debug::Acquire::SrvRecs", false) == true)
+ {
+ std::cerr << "SrvRecs: got " << I->target
+ << " prio: " << I->priority
+ << " weight: " << I->weight
+ << std::endl;
+ }
+ }
+
+ return true;
+}
+
+SrvRec PopFromSrvRecs(std::vector<SrvRec> &Recs)
+{
+ // FIXME: instead of the simplistic shuffle below use the algorithm
+ // described in rfc2782 (with weights)
+ // and figure out how the weights need to be adjusted if
+ // a host refuses connections
+
+#if 0 // all code below is only needed for the weight adjusted selection
// assign random number ranges
int prev_weight = 0;
int prev_priority = 0;
@@ -124,6 +152,12 @@ bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result)
I->random_number_range_end = prev_weight + I->weight;
prev_weight = I->random_number_range_end;
prev_priority = I->priority;
+
+ if (_config->FindB("Debug::Acquire::SrvRecs", false) == true)
+ std::cerr << "SrvRecs: got " << I->target
+ << " prio: " << I->priority
+ << " weight: " << I->weight
+ << std::endl;
}
// go over the code in reverse order and note the max random range
@@ -136,8 +170,27 @@ bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result)
max = I->random_number_range_end;
I->random_number_range_max = max;
}
+#endif
- // FIXME: now shuffle
+ // shuffle in a very simplistic way for now (equal weights)
+ std::vector<SrvRec>::iterator I, J;
+ I = J = Recs.begin();
+ for(;I != Recs.end(); ++I)
+ {
+ if(I->priority != J->priority)
+ break;
+ }
- return true;
+ // FIXME: meeeeh, where to init this properly
+ unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
+ std::shuffle(J, I, std::default_random_engine(seed));
+
+ // meh, no pop_front() in std::vector?
+ SrvRec selected = *Recs.begin();
+ Recs.erase(Recs.begin());
+
+ if (_config->FindB("Debug::Acquire::SrvRecs", false) == true)
+ std::cerr << "PopFromSrvRecs: selecting " << selected.target << std::endl;
+
+ return selected;
}