mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 23:20:55 +00:00
fixed lint
This commit is contained in:
committed by
Paul Robinson
parent
cd810d26c2
commit
25b35dee1a
@ -26,13 +26,13 @@ async def handle_ping(stream: INetStream) -> None:
|
|||||||
try:
|
try:
|
||||||
payload = await stream.read(PING_LENGTH)
|
payload = await stream.read(PING_LENGTH)
|
||||||
peer_id = stream.muxed_conn.peer_id
|
peer_id = stream.muxed_conn.peer_id
|
||||||
if payload != None:
|
if payload is not None:
|
||||||
print(f"received ping from {peer_id}")
|
print(f"received ping from {peer_id}")
|
||||||
|
|
||||||
await stream.write(payload)
|
await stream.write(payload)
|
||||||
print(f"responded with pong to {peer_id}")
|
print(f"responded with pong to {peer_id}")
|
||||||
|
|
||||||
except:
|
except Exception:
|
||||||
await stream.reset()
|
await stream.reset()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,15 @@
|
|||||||
import logging
|
import logging
|
||||||
import time
|
|
||||||
import trio
|
|
||||||
import secrets
|
import secrets
|
||||||
|
import time
|
||||||
|
from typing import (
|
||||||
|
List,
|
||||||
|
)
|
||||||
|
|
||||||
|
import trio
|
||||||
|
|
||||||
|
from libp2p.host.host_interface import (
|
||||||
|
IHost,
|
||||||
|
)
|
||||||
from libp2p.network.stream.exceptions import (
|
from libp2p.network.stream.exceptions import (
|
||||||
StreamClosed,
|
StreamClosed,
|
||||||
StreamEOF,
|
StreamEOF,
|
||||||
@ -16,11 +23,6 @@ from libp2p.typing import (
|
|||||||
TProtocol,
|
TProtocol,
|
||||||
)
|
)
|
||||||
|
|
||||||
from libp2p.host.host_interface import (
|
|
||||||
IHost,
|
|
||||||
)
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
ID = TProtocol("/ipfs/ping/1.0.0")
|
ID = TProtocol("/ipfs/ping/1.0.0")
|
||||||
PING_LENGTH = 32
|
PING_LENGTH = 32
|
||||||
RESP_TIMEOUT = 60
|
RESP_TIMEOUT = 60
|
||||||
@ -71,7 +73,7 @@ async def handle_ping(stream: INetStream) -> None:
|
|||||||
try:
|
try:
|
||||||
should_continue = await _handle_ping(stream, peer_id)
|
should_continue = await _handle_ping(stream, peer_id)
|
||||||
if not should_continue:
|
if not should_continue:
|
||||||
stream.close()
|
await stream.close()
|
||||||
return
|
return
|
||||||
except Exception:
|
except Exception:
|
||||||
await stream.reset()
|
await stream.reset()
|
||||||
@ -79,7 +81,7 @@ async def handle_ping(stream: INetStream) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def _ping(stream: INetStream) -> int:
|
async def _ping(stream: INetStream) -> int:
|
||||||
"""
|
"""
|
||||||
Helper function to perform a single ping operation on a given stream,
|
Helper function to perform a single ping operation on a given stream,
|
||||||
returns integer value rtt - which denotes round trip time for a ping request in ms
|
returns integer value rtt - which denotes round trip time for a ping request in ms
|
||||||
"""
|
"""
|
||||||
@ -87,14 +89,15 @@ async def _ping(stream: INetStream) -> int:
|
|||||||
before = time.time()
|
before = time.time()
|
||||||
await stream.write(ping_bytes)
|
await stream.write(ping_bytes)
|
||||||
pong_bytes = await stream.read(PING_LENGTH)
|
pong_bytes = await stream.read(PING_LENGTH)
|
||||||
rtt = int((time.time() - before) * (10 ** 6))
|
rtt = int((time.time() - before) * (10**6))
|
||||||
if ping_bytes != pong_bytes:
|
if ping_bytes != pong_bytes:
|
||||||
logger.debug("invalid pong response")
|
logger.debug("invalid pong response")
|
||||||
raise
|
raise
|
||||||
return rtt
|
return rtt
|
||||||
|
|
||||||
|
|
||||||
class PingService:
|
class PingService:
|
||||||
""" PingService executes pings and returns RTT in miliseconds. """
|
"""PingService executes pings and returns RTT in miliseconds."""
|
||||||
|
|
||||||
def __init__(self, host: IHost):
|
def __init__(self, host: IHost):
|
||||||
self._host = host
|
self._host = host
|
||||||
@ -103,11 +106,9 @@ class PingService:
|
|||||||
stream = await self._host.new_stream(peer_id, [ID])
|
stream = await self._host.new_stream(peer_id, [ID])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rtts = [
|
rtts = [await _ping(stream) for _ in range(ping_amt)]
|
||||||
await _ping(stream) for _ in range(ping_amt)
|
|
||||||
]
|
|
||||||
await stream.close()
|
await stream.close()
|
||||||
return rtts
|
return rtts
|
||||||
except Exception:
|
except Exception:
|
||||||
await stream.close()
|
await stream.close()
|
||||||
raise
|
raise
|
||||||
|
|||||||
Reference in New Issue
Block a user