summaryrefslogtreecommitdiff
path: root/methods
diff options
context:
space:
mode:
authorMichael Vogt <michael.vogt@ubuntu.com>2011-03-14 15:44:08 +0100
committerMichael Vogt <michael.vogt@ubuntu.com>2011-03-14 15:44:08 +0100
commit01a695e260429b4ed834a52fb55ba49a8cb42e6a (patch)
treeb4e1d389778f0e8d4f9dba5691c40fad2e5874f5 /methods
parent8d6c583900b81bf996afd104571580c6f3e55e1c (diff)
randomize mirror list to ensure more even load
Diffstat (limited to 'methods')
-rw-r--r--methods/mirror.cc30
-rw-r--r--methods/mirror.h1
2 files changed, 31 insertions, 0 deletions
diff --git a/methods/mirror.cc b/methods/mirror.cc
index 5a53d3c81..08a603207 100644
--- a/methods/mirror.cc
+++ b/methods/mirror.cc
@@ -17,6 +17,7 @@
#include <apt-pkg/sourcelist.h>
#include <fstream>
+#include <algorithm>
#include <iostream>
#include <stdarg.h>
#include <sys/stat.h>
@@ -147,6 +148,34 @@ bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str)
return res;
}
+// Randomizes the lines in the mirror file, this is used so that
+// we spread the load on the mirrors evenly
+bool MirrorMethod::RandomizeMirrorFile(string mirror_file)
+{
+ vector<string> content;
+ string line;
+
+ // read
+ ifstream in(mirror_file.c_str());
+ while ( ! in.eof() ) {
+ getline(in, line);
+ content.push_back(line);
+ }
+
+ // randomize
+ random_shuffle(content.begin(), content.end());
+
+ // write
+ ofstream out(mirror_file.c_str());
+ while ( !content.empty()) {
+ line = content.back();
+ content.pop_back();
+ out << line << "\n";
+ }
+
+ return true;
+}
+
/* convert a the Queue->Uri back to the mirror base uri and look
* at all mirrors we have for this, this is needed as queue->uri
* may point to different mirrors (if TryNextMirror() was run)
@@ -313,6 +342,7 @@ bool MirrorMethod::Fetch(FetchItem *Itm)
{
Clean(_config->FindDir("Dir::State::mirrors"));
DownloadMirrorFile(Itm->Uri);
+ RandomizeMirrorFile(MirrorFile);
}
if(AllMirrors.empty()) {
diff --git a/methods/mirror.h b/methods/mirror.h
index 0a3ea6e92..bd807e122 100644
--- a/methods/mirror.h
+++ b/methods/mirror.h
@@ -34,6 +34,7 @@ class MirrorMethod : public HttpMethod
protected:
bool DownloadMirrorFile(string uri);
+ bool RandomizeMirrorFile(string file);
string GetMirrorFileName(string uri);
bool InitMirrors();
bool TryNextMirror();