forked from LBRYCommunity/lbry-sdk
Wrap "async for" over IterativeXXXFinder in try/finally ensuring aclose().
This commit is contained in:
parent
82d7f81f41
commit
4767bb9dee
1 changed files with 39 additions and 32 deletions
|
@ -217,9 +217,13 @@ class Node:
|
|||
shortlist: typing.Optional[typing.List['KademliaPeer']] = None
|
||||
) -> typing.List['KademliaPeer']:
|
||||
peers = []
|
||||
async for iteration_peers in self.get_iterative_node_finder(
|
||||
node_id, shortlist=shortlist, max_results=max_results):
|
||||
node_finder = self.get_iterative_node_finder(
|
||||
node_id, shortlist=shortlist, max_results=max_results)
|
||||
try:
|
||||
async for iteration_peers in node_finder:
|
||||
peers.extend(iteration_peers)
|
||||
finally:
|
||||
await node_finder.aclose()
|
||||
distance = Distance(node_id)
|
||||
peers.sort(key=lambda peer: distance(peer.node_id))
|
||||
return peers[:count]
|
||||
|
@ -245,8 +249,9 @@ class Node:
|
|||
|
||||
# prioritize peers who reply to a dht ping first
|
||||
# this minimizes attempting to make tcp connections that won't work later to dead or unreachable peers
|
||||
|
||||
async for results in self.get_iterative_value_finder(bytes.fromhex(blob_hash)):
|
||||
value_finder = self.get_iterative_value_finder(bytes.fromhex(blob_hash))
|
||||
try:
|
||||
async for results in value_finder:
|
||||
to_put = []
|
||||
for peer in results:
|
||||
if peer.address == self.protocol.external_ip and self.protocol.peer_port == peer.tcp_port:
|
||||
|
@ -275,6 +280,8 @@ class Node:
|
|||
log.debug("skip bad peer %s:%i for %s", peer.address, peer.tcp_port, blob_hash)
|
||||
if to_put:
|
||||
result_queue.put_nowait(to_put)
|
||||
finally:
|
||||
await value_finder.aclose()
|
||||
|
||||
def accumulate_peers(self, search_queue: asyncio.Queue,
|
||||
peer_queue: typing.Optional[asyncio.Queue] = None
|
||||
|
|
Loading…
Reference in a new issue