]> git.ipfire.org Git - pbs.git/commitdiff
mirrors: Shorten composing the mirror list
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Oct 2017 16:56:08 +0000 (17:56 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Oct 2017 16:56:08 +0000 (17:56 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/mirrors.py
src/web/handlers.py

index f3eb0419ede208dc571edf398eb6ee3441035011..e54c8d4432f5dcdcab326db1cfdd328d51196080 100644 (file)
@@ -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"
index b6313dd64953108bb53f63e35af3dd647421acaf..270e6799f5db3b41509db3d1ed8b7926243bc166 100644 (file)
@@ -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