clean up and clarify in docstring when/where None is returned while searching for peers

This commit is contained in:
Kay Kurokawa 2017-08-16 15:04:50 -04:00
parent e0985695ac
commit a9e6c89693
2 changed files with 24 additions and 21 deletions

View file

@ -73,8 +73,13 @@ class BlobRequester(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def get_new_peers_for_next_unavailable(self): 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() 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) peers = yield self._find_peers_for_hash(blob_hash)
defer.returnValue(peers) defer.returnValue(peers)
@ -112,6 +117,10 @@ class BlobRequester(object):
return defer.succeed(sent_request) return defer.succeed(sent_request)
def _get_hash_for_peer_search(self): def _get_hash_for_peer_search(self):
"""
Get next unavailable hash for blob,
returns None if there is nothing left to download
"""
r = None r = None
blobs_to_download = self._blobs_to_download() blobs_to_download = self._blobs_to_download()
if blobs_to_download: if blobs_to_download:
@ -125,26 +134,23 @@ class BlobRequester(object):
return defer.succeed(r) return defer.succeed(r)
def _find_peers_for_hash(self, h): def _find_peers_for_hash(self, h):
if h is None: d = self.peer_finder.find_peers_for_blob(h)
return None
else:
d = self.peer_finder.find_peers_for_blob(h)
def choose_best_peers(peers): def choose_best_peers(peers):
bad_peers = self._get_bad_peers() bad_peers = self._get_bad_peers()
without_bad_peers = [p for p in peers if not p in bad_peers] without_bad_peers = [p for p in peers if not p in bad_peers]
without_maxed_out_peers = [ without_maxed_out_peers = [
p for p in without_bad_peers if p not in self._maxed_out_peers] p for p in without_bad_peers if p not in self._maxed_out_peers]
return without_maxed_out_peers return without_maxed_out_peers
d.addCallback(choose_best_peers) d.addCallback(choose_best_peers)
def lookup_failed(err): def lookup_failed(err):
log.error("An error occurred looking up peers for a hash: %s", err.getTraceback()) log.error("An error occurred looking up peers for a hash: %s", err.getTraceback())
return [] return []
d.addErrback(lookup_failed) d.addErrback(lookup_failed)
return d return d
def _should_send_request_to(self, peer): def _should_send_request_to(self, peer):
if self._peers[peer] < -5.0: if self._peers[peer] < -5.0:

View file

@ -163,9 +163,6 @@ class ConnectionManager(object):
self._next_manage_call = utils.call_later(self.MANAGE_CALL_INTERVAL_SEC, self.manage) 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): 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] out = [peer for peer in peers if peer not in self._peer_connections]
random.shuffle(out) random.shuffle(out)
return out[0:new_conns_needed] return out[0:new_conns_needed]
@ -204,7 +201,7 @@ class ConnectionManager(object):
def _connect_to_peer(self, peer): def _connect_to_peer(self, peer):
if peer is None or self.stopped: if self.stopped:
return return
log.debug("%s Trying to connect to %s", self._get_log_name(), peer) log.debug("%s Trying to connect to %s", self._get_log_name(), peer)