mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Merge branch 'master' into fix/refactor-mplex-swarm-host
This commit is contained in:
5
libp2p/network/connection/exceptions.py
Normal file
5
libp2p/network/connection/exceptions.py
Normal file
@ -0,0 +1,5 @@
|
||||
from libp2p.io.exceptions import IOException
|
||||
|
||||
|
||||
class RawConnError(IOException):
|
||||
pass
|
||||
@ -1,5 +1,6 @@
|
||||
import asyncio
|
||||
|
||||
from .exceptions import RawConnError
|
||||
from .raw_connection_interface import IRawConnection
|
||||
|
||||
|
||||
@ -23,19 +24,33 @@ class RawConnection(IRawConnection):
|
||||
self._drain_lock = asyncio.Lock()
|
||||
|
||||
async def write(self, data: bytes) -> None:
|
||||
self.writer.write(data)
|
||||
"""
|
||||
Raise `RawConnError` if the underlying connection breaks
|
||||
"""
|
||||
try:
|
||||
self.writer.write(data)
|
||||
except ConnectionResetError as error:
|
||||
raise RawConnError(error)
|
||||
# Reference: https://github.com/ethereum/lahja/blob/93610b2eb46969ff1797e0748c7ac2595e130aef/lahja/asyncio/endpoint.py#L99-L102 # noqa: E501
|
||||
# Use a lock to serialize drain() calls. Circumvents this bug:
|
||||
# https://bugs.python.org/issue29930
|
||||
async with self._drain_lock:
|
||||
await self.writer.drain()
|
||||
try:
|
||||
await self.writer.drain()
|
||||
except ConnectionResetError:
|
||||
raise RawConnError()
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
"""
|
||||
Read up to ``n`` bytes from the underlying stream.
|
||||
This call is delegated directly to the underlying ``self.reader``.
|
||||
|
||||
Raise `RawConnError` if the underlying connection breaks
|
||||
"""
|
||||
return await self.reader.read(n)
|
||||
try:
|
||||
return await self.reader.read(n)
|
||||
except ConnectionResetError as error:
|
||||
raise RawConnError(error)
|
||||
|
||||
async def close(self) -> None:
|
||||
self.writer.close()
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
from libp2p.exceptions import BaseLibp2pError
|
||||
from libp2p.io.exceptions import IOException
|
||||
|
||||
|
||||
class StreamError(BaseLibp2pError):
|
||||
class StreamError(IOException):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@ -99,7 +99,13 @@ class Swarm(INetwork):
|
||||
multiaddr = self.router.find_peer(peer_id)
|
||||
# Dial peer (connection to peer does not yet exist)
|
||||
# Transport dials peer (gets back a raw conn)
|
||||
raw_conn = await self.transport.dial(multiaddr)
|
||||
try:
|
||||
raw_conn = await self.transport.dial(multiaddr)
|
||||
except OpenConnectionError as error:
|
||||
logger.debug("fail to dial peer %s over base transport", peer_id)
|
||||
raise SwarmException(
|
||||
"fail to open connection to peer %s", peer_id
|
||||
) from error
|
||||
|
||||
logger.debug("dialed peer %s over base transport", peer_id)
|
||||
|
||||
@ -137,6 +143,7 @@ class Swarm(INetwork):
|
||||
"""
|
||||
:param peer_id: peer_id of destination
|
||||
:param protocol_id: protocol id
|
||||
:raises SwarmException: raised when an error occurs
|
||||
:return: net stream instance
|
||||
"""
|
||||
logger.debug(
|
||||
|
||||
Reference in New Issue
Block a user