From: Michael Tremer Date: Wed, 25 Oct 2017 16:56:08 +0000 (+0100) Subject: mirrors: Shorten composing the mirror list X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9eca094b552d16c7be6ca7f89c1819ae8afb658a;p=pbs.git mirrors: Shorten composing the mirror list Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/mirrors.py b/src/buildservice/mirrors.py index f3eb0419..e54c8d44 100644 --- a/src/buildservice/mirrors.py +++ b/src/buildservice/mirrors.py @@ -35,6 +35,14 @@ class Mirrors(base.Object): return iter(mirrors) + @property + def random(self): + """ + Returns all mirrors in a random order + """ + return self._get_mirrors("SELECT * FROM mirrors \ + WHERE deleted IS FALSE ORDER BY RANDOM()") + def create(self, hostname, path="", owner=None, contact=None, user=None): mirror = self._get_mirror("INSERT INTO mirrors(hostname, path, owner, contact) \ VALUES(%s, %s, %s, %s) RETURNING *", hostname, path, owner, contact) @@ -51,22 +59,19 @@ class Mirrors(base.Object): return self._get_mirror("SELECT * FROM mirrors \ WHERE hostname = %s AND deleted IS FALSE", hostname) - def get_for_location(self, address): - country_code = self.backend.geoip.guess_from_address(address) - - # Cannot return any good mirrors if location is unknown - if not country_code: - return [] + def make_mirrorlist(self, client_address=None): + country_code = self.backend.geoip.guess_from_address(client_address) + # Walk through all mirrors in a random order + # and put all preferred mirrors to the front of the list + # and everything else at the end mirrors = [] - - # Walk through all mirrors - for mirror in self: - if mirror.country_code == country_code: + for mirror in self.random: + if mirror.is_preferred_for_country(country_code): + mirrors.insert(0, mirror) + else: mirrors.append(mirror) - # XXX needs to search for nearby countries - return mirrors def get_history(self, limit=None, offset=None, mirror=None, user=None): @@ -258,6 +263,10 @@ class Mirror(base.DataObject): def address(self): return socket.gethostbyname(self.hostname) + def is_preferred_for_country(self, country_code): + if country_code and self.country_code: + return self.country_code == country_code + @lazy_property def country_code(self): return self.backend.geoip.guess_from_address(self.address) or "UNKNOWN" diff --git a/src/web/handlers.py b/src/web/handlers.py index b6313dd6..270e6799 100644 --- a/src/web/handlers.py +++ b/src/web/handlers.py @@ -218,26 +218,11 @@ class RepositoryMirrorlistHandler(BaseHandler): "version" : 1, } - # A list with mirrors that are sent to the user. mirrors = [] - - # Select a list of preferred mirrors - for mirror in self.mirrors.get_for_location(self.current_address): - mirrors.append({ - "url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch)), - "location" : mirror.country_code, - "preferred" : 1, - }) - - # Add all other mirrors at the end in a random order - remaining_mirrors = [m for m in self.backend.mirrors if not m in mirrors] - random.shuffle(remaining_mirrors) - - for mirror in remaining_mirrors: + for mirror in self.mirrors.make_mirrorlist(self.current_address): mirrors.append({ "url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch)), "location" : mirror.country_code, - "preferred" : 0, }) ret["mirrors"] = mirrors