add accumulate_peers to Node

simplifies collecting peers during a download by giving a queue of hashes to search for and a queue or peers as they are found
This commit is contained in:
Jack Robison 2019-01-30 14:53:39 -05:00
parent 64dec821b7
commit fcda9500fe
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2

View file

@ -236,3 +236,19 @@ class Node:
distance = Distance(node_id) distance = Distance(node_id)
accumulated.sort(key=lambda peer: distance(peer.node_id)) accumulated.sort(key=lambda peer: distance(peer.node_id))
return accumulated[:count] return accumulated[:count]
async def _accumulate_search_junction(self, search_queue: asyncio.Queue,
result_queue: asyncio.Queue):
try:
async with self.stream_peer_search_junction(search_queue) as search_junction:
async for peers in search_junction:
if peers:
result_queue.put_nowait(peers)
except asyncio.CancelledError:
return
def accumulate_peers(self, search_queue: asyncio.Queue,
peer_queue: typing.Optional[asyncio.Queue] = None) -> typing.Tuple[
asyncio.Queue, asyncio.Task]:
q = peer_queue or asyncio.Queue()
return q, asyncio.create_task(self._accumulate_search_junction(search_queue, q))