mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 15:10:54 +00:00
Refactor P2PWebSocketConnection and WebsocketTransport constructors for improved readability. Clean up whitespace and enhance logging for connection management.
This commit is contained in:
@ -18,7 +18,12 @@ class P2PWebSocketConnection(ReadWriteCloser):
|
|||||||
as recommended in the libp2p WebSocket specification.
|
as recommended in the libp2p WebSocket specification.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, ws_connection: Any, ws_context: Any = None, max_buffered_amount: int = 4 * 1024 * 1024) -> None:
|
def __init__(
|
||||||
|
self,
|
||||||
|
ws_connection: Any,
|
||||||
|
ws_context: Any = None,
|
||||||
|
max_buffered_amount: int = 4 * 1024 * 1024,
|
||||||
|
) -> None:
|
||||||
self._ws_connection = ws_connection
|
self._ws_connection = ws_connection
|
||||||
self._ws_context = ws_context
|
self._ws_context = ws_context
|
||||||
self._read_buffer = b""
|
self._read_buffer = b""
|
||||||
@ -37,11 +42,12 @@ class P2PWebSocketConnection(ReadWriteCloser):
|
|||||||
logger.debug(f"WebSocket writing {len(data)} bytes")
|
logger.debug(f"WebSocket writing {len(data)} bytes")
|
||||||
|
|
||||||
# Check buffer amount for flow control
|
# Check buffer amount for flow control
|
||||||
if hasattr(self._ws_connection, 'bufferedAmount'):
|
if hasattr(self._ws_connection, "bufferedAmount"):
|
||||||
buffered = self._ws_connection.bufferedAmount
|
buffered = self._ws_connection.bufferedAmount
|
||||||
if buffered > self._max_buffered_amount:
|
if buffered > self._max_buffered_amount:
|
||||||
logger.warning(f"WebSocket buffer full: {buffered} bytes")
|
logger.warning(f"WebSocket buffer full: {buffered} bytes")
|
||||||
# In production, you might want to wait or implement backpressure
|
# In production, you might want to
|
||||||
|
# wait or implement backpressure
|
||||||
# For now, we'll continue but log the warning
|
# For now, we'll continue but log the warning
|
||||||
|
|
||||||
# Send as a binary WebSocket message
|
# Send as a binary WebSocket message
|
||||||
|
|||||||
@ -25,7 +25,9 @@ class WebsocketTransport(ITransport):
|
|||||||
- Support for both WS and WSS protocols
|
- Support for both WS and WSS protocols
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, upgrader: TransportUpgrader, max_buffered_amount: int = 4 * 1024 * 1024):
|
def __init__(
|
||||||
|
self, upgrader: TransportUpgrader, max_buffered_amount: int = 4 * 1024 * 1024
|
||||||
|
):
|
||||||
self._upgrader = upgrader
|
self._upgrader = upgrader
|
||||||
self._max_buffered_amount = max_buffered_amount
|
self._max_buffered_amount = max_buffered_amount
|
||||||
self._connection_count = 0
|
self._connection_count = 0
|
||||||
@ -57,20 +59,20 @@ class WebsocketTransport(ITransport):
|
|||||||
|
|
||||||
# Check connection limits
|
# Check connection limits
|
||||||
if self._connection_count >= self._max_connections:
|
if self._connection_count >= self._max_connections:
|
||||||
raise OpenConnectionError(f"Maximum connections reached: {self._max_connections}")
|
raise OpenConnectionError(
|
||||||
|
f"Maximum connections reached: {self._max_connections}"
|
||||||
|
)
|
||||||
|
|
||||||
# Use the context manager but don't exit it immediately
|
# Use the context manager but don't exit it immediately
|
||||||
# The connection will be closed when the RawConnection is closed
|
# The connection will be closed when the RawConnection is closed
|
||||||
ws_context = open_websocket_url(ws_url)
|
ws_context = open_websocket_url(ws_url)
|
||||||
ws = await ws_context.__aenter__()
|
ws = await ws_context.__aenter__()
|
||||||
conn = P2PWebSocketConnection(
|
conn = P2PWebSocketConnection(
|
||||||
ws,
|
ws, ws_context, max_buffered_amount=self._max_buffered_amount
|
||||||
ws_context,
|
|
||||||
max_buffered_amount=self._max_buffered_amount
|
|
||||||
) # type: ignore[attr-defined]
|
) # type: ignore[attr-defined]
|
||||||
|
|
||||||
self._connection_count += 1
|
self._connection_count += 1
|
||||||
logger.debug(f"WebSocket connection established. Total connections: {self._connection_count}")
|
logger.debug(f"Total connections: {self._connection_count}")
|
||||||
|
|
||||||
return RawConnection(conn, initiator=True)
|
return RawConnection(conn, initiator=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@ -16,8 +16,6 @@ from libp2p.peer.id import ID
|
|||||||
from libp2p.peer.peerinfo import PeerInfo
|
from libp2p.peer.peerinfo import PeerInfo
|
||||||
from libp2p.peer.peerstore import PeerStore
|
from libp2p.peer.peerstore import PeerStore
|
||||||
from libp2p.security.insecure.transport import InsecureTransport
|
from libp2p.security.insecure.transport import InsecureTransport
|
||||||
from libp2p.security.noise.transport import Transport as NoiseTransport
|
|
||||||
from libp2p.crypto.ed25519 import create_new_key_pair as create_ed25519_key_pair
|
|
||||||
from libp2p.stream_muxer.yamux.yamux import Yamux
|
from libp2p.stream_muxer.yamux.yamux import Yamux
|
||||||
from libp2p.transport.upgrader import TransportUpgrader
|
from libp2p.transport.upgrader import TransportUpgrader
|
||||||
from libp2p.transport.websocket.transport import WebsocketTransport
|
from libp2p.transport.websocket.transport import WebsocketTransport
|
||||||
|
|||||||
Reference in New Issue
Block a user