From a9e6c896931d5ece6f9d1f51e9717cbc2166b80e Mon Sep 17 00:00:00 2001 From: Kay Kurokawa Date: Wed, 16 Aug 2017 15:04:50 -0400 Subject: [PATCH] clean up and clarify in docstring when/where None is returned while searching for peers --- lbrynet/core/client/BlobRequester.py | 40 ++++++++++++++---------- lbrynet/core/client/ConnectionManager.py | 5 +-- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lbrynet/core/client/BlobRequester.py b/lbrynet/core/client/BlobRequester.py index d3d11163c..95ffaa327 100644 --- a/lbrynet/core/client/BlobRequester.py +++ b/lbrynet/core/client/BlobRequester.py @@ -73,8 +73,13 @@ class BlobRequester(object): @defer.inlineCallbacks def get_new_peers_for_next_unavailable(self): - """ look for peers for the next unavailable blob """ + """ + Look for peers for the next unavailable blob, if we have + all blobs, return an empty list + """ blob_hash = yield self._get_hash_for_peer_search() + if blob_hash is None: + defer.returnValue([]) peers = yield self._find_peers_for_hash(blob_hash) defer.returnValue(peers) @@ -112,6 +117,10 @@ class BlobRequester(object): return defer.succeed(sent_request) def _get_hash_for_peer_search(self): + """ + Get next unavailable hash for blob, + returns None if there is nothing left to download + """ r = None blobs_to_download = self._blobs_to_download() if blobs_to_download: @@ -125,26 +134,23 @@ class BlobRequester(object): return defer.succeed(r) def _find_peers_for_hash(self, h): - if h is None: - return None - else: - d = self.peer_finder.find_peers_for_blob(h) + d = self.peer_finder.find_peers_for_blob(h) - def choose_best_peers(peers): - bad_peers = self._get_bad_peers() - without_bad_peers = [p for p in peers if not p in bad_peers] - without_maxed_out_peers = [ - p for p in without_bad_peers if p not in self._maxed_out_peers] - return without_maxed_out_peers + def choose_best_peers(peers): + bad_peers = self._get_bad_peers() + without_bad_peers = [p for p in peers if not p in bad_peers] + without_maxed_out_peers = [ + p for p in without_bad_peers if p not in self._maxed_out_peers] + return without_maxed_out_peers - d.addCallback(choose_best_peers) + d.addCallback(choose_best_peers) - def lookup_failed(err): - log.error("An error occurred looking up peers for a hash: %s", err.getTraceback()) - return [] + def lookup_failed(err): + log.error("An error occurred looking up peers for a hash: %s", err.getTraceback()) + return [] - d.addErrback(lookup_failed) - return d + d.addErrback(lookup_failed) + return d def _should_send_request_to(self, peer): if self._peers[peer] < -5.0: diff --git a/lbrynet/core/client/ConnectionManager.py b/lbrynet/core/client/ConnectionManager.py index 594536006..ae04e8355 100644 --- a/lbrynet/core/client/ConnectionManager.py +++ b/lbrynet/core/client/ConnectionManager.py @@ -163,9 +163,6 @@ class ConnectionManager(object): self._next_manage_call = utils.call_later(self.MANAGE_CALL_INTERVAL_SEC, self.manage) def return_shuffled_peers_not_connected_to(self, peers, new_conns_needed): - if peers is None: - # can happen if there is some error in the lookup - return [] out = [peer for peer in peers if peer not in self._peer_connections] random.shuffle(out) return out[0:new_conns_needed] @@ -204,7 +201,7 @@ class ConnectionManager(object): def _connect_to_peer(self, peer): - if peer is None or self.stopped: + if self.stopped: return log.debug("%s Trying to connect to %s", self._get_log_name(), peer)