]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
When peer downloads fail, try direct downloads.
authorCameron Dale <camrdale@gmail.com>
Fri, 11 Jan 2008 04:34:21 +0000 (20:34 -0800)
committerCameron Dale <camrdale@gmail.com>
Fri, 11 Jan 2008 04:34:21 +0000 (20:34 -0800)
Also don't cache failed download results, just return them.

apt_dht/MirrorManager.py
apt_dht/apt_dht.py

index 70138e69e22ed6823f64e8d9ffa53651f28f2993..8bf197f9d40203749bb4c34ae33938d290e94c51 100644 (file)
@@ -208,6 +208,10 @@ class MirrorManager:
     
     def save_file(self, response, hash, url):
         """Save a downloaded file to the cache and stream it."""
+        if response.code != 200:
+            log.msg('File was not found (%r): %s' % (response, url))
+            return response
+        
         log.msg('Returning file: %s' % url)
         
         parsed = urlparse(url)
index b4aaff0d0905a100fb68f514a58450894cdcde63..2af1df92d4aeb555f8e33f73ce5a462d338d7cdc 100644 (file)
@@ -157,7 +157,7 @@ class AptDHT:
     def findHash_done(self, hash, path, d):
         if hash.expected() is None:
             log.msg('Hash for %s was not found' % path)
-            self.download_file([path], hash, path, d)
+            self.lookupHash_done([], hash, path, d)
         else:
             log.msg('Found hash %s for %s' % (hash.hexexpected(), path))
             # Lookup hash from DHT
@@ -168,17 +168,25 @@ class AptDHT:
     def lookupHash_done(self, locations, hash, path, d):
         if not locations:
             log.msg('Peers for %s were not found' % path)
-            self.download_file([path], hash, path, d)
+            getDefer = self.peers.get([path])
+            getDefer.addCallback(self.mirrors.save_file, hash, path)
+            getDefer.addErrback(self.mirrors.save_error, path)
+            getDefer.addCallbacks(d.callback, d.errback)
         else:
             log.msg('Found peers for %s: %r' % (path, locations))
             # Download from the found peers
-            self.download_file(locations, hash, path, d)
+            getDefer = self.peers.get(locations)
+            getDefer.addCallback(self.check_response, hash, path)
+            getDefer.addCallback(self.mirrors.save_file, hash, path)
+            getDefer.addErrback(self.mirrors.save_error, path)
+            getDefer.addCallbacks(d.callback, d.errback)
             
-    def download_file(self, locations, hash, path, d):
-        getDefer = self.peers.get(locations)
-        getDefer.addCallback(self.mirrors.save_file, hash, path)
-        getDefer.addErrback(self.mirrors.save_error, path)
-        getDefer.addCallbacks(d.callback, d.errback)
+    def check_response(self, response, hash, path):
+        if response.code < 200 or response.code >= 300:
+            log.msg('Download from peers failed, going to direct download: %s' % path)
+            getDefer = self.peers.get([path])
+            return getDefer
+        return response
         
     def download_complete(self, hash, url, file_path):
         assert file_path.startswith(config.get('DEFAULT', 'cache_dir'))