Skip to content

Commit

Permalink
Rename dht_tester -> target_dht
Browse files Browse the repository at this point in the history
  • Loading branch information
borzunov committed Jan 12, 2023
1 parent 062deb9 commit 5c229c5
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/petals/server/reachability.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def check_direct_reachability(max_peers: int = 5, threshold: float = 0.5, **kwar

async def _check_direct_reachability():
try:
dht_tester = await DHTNode.create(client_mode=True, **kwargs)
protocol = ReachabilityProtocol(dht_tester.protocol.p2p)
await protocol.add_p2p_handlers(dht_tester.protocol.p2p)
target_dht = await DHTNode.create(client_mode=True, **kwargs)
protocol = ReachabilityProtocol(target_dht.protocol.p2p)
await protocol.add_p2p_handlers(target_dht.protocol.p2p)

successes = requests = 0
for remote_peer in list(dht_tester.protocol.routing_table.peer_id_to_uid.keys()):
probe_available = await protocol.call_check(remote_peer=remote_peer, check_peer=dht_tester.peer_id)
for remote_peer in list(target_dht.protocol.routing_table.peer_id_to_uid.keys()):
probe_available = await protocol.call_check(remote_peer=remote_peer, check_peer=target_dht.peer_id)
if probe_available is None:
continue # remote peer failed to check probe
successes += probe_available
Expand All @@ -73,7 +73,7 @@ async def _check_direct_reachability():
logger.debug(f"Direct reachability: observed {successes} successes out of {requests} requests")
return (successes / requests) >= threshold if requests > 0 else None
finally:
await dht_tester.shutdown()
await target_dht.shutdown()

return RemoteExpertWorker.run_coroutine(_check_direct_reachability())

Expand All @@ -89,7 +89,7 @@ def __init__(self, p2p: P2P, *, probe: Optional[P2P] = None, wait_timeout: float
self.p2p, self.probe, self.wait_timeout = p2p, probe, wait_timeout

async def call_check(self, remote_peer: PeerID, *, check_peer: PeerID) -> Optional[bool]:
"""return True if remote_peer can reach check_peer, False if cannot, None means remote_peer did not respond"""
"""Returns True if remote_peer can reach check_peer, False if it cannot, None if it did not respond"""
try:
request = dht_pb2.PingRequest(peer=dht_pb2.NodeInfo(node_id=check_peer.to_bytes()))
timeout = self.wait_timeout if check_peer == remote_peer else self.wait_timeout * 2
Expand All @@ -100,7 +100,7 @@ async def call_check(self, remote_peer: PeerID, *, check_peer: PeerID) -> Option
return None

async def rpc_check(self, request: dht_pb2.PingRequest, context: P2PContext) -> dht_pb2.PingResponse:
"""Another peer wants us to help it check reachability"""
"""Help another peer to check its reachability"""
response = dht_pb2.PingResponse(available=True)
check_peer = PeerID(request.peer.node_id)
if check_peer != context.local_id: # remote peer wants us to check someone other than ourselves
Expand Down

0 comments on commit 5c229c5

Please sign in to comment.