From 33730bdc48313b5c63d5092dd9f39e230124681c Mon Sep 17 00:00:00 2001 From: Akash Mondal Date: Tue, 2 Sep 2025 16:39:38 +0000 Subject: [PATCH] fix: type assertion for config class --- libp2p/__init__.py | 8 +++-- libp2p/network/config.py | 54 ++++++++++++++++++++++++++++++++ libp2p/network/swarm.py | 55 +-------------------------------- libp2p/transport/quic/config.py | 5 ++- 4 files changed, 62 insertions(+), 60 deletions(-) create mode 100644 libp2p/network/config.py diff --git a/libp2p/__init__.py b/libp2p/__init__.py index 10989f17..32f3b31d 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -42,10 +42,12 @@ from libp2p.host.routed_host import ( RoutedHost, ) from libp2p.network.swarm import ( - ConnectionConfig, - RetryConfig, Swarm, ) +from libp2p.network.config import ( + ConnectionConfig, + RetryConfig +) from libp2p.peer.id import ( ID, ) @@ -169,7 +171,7 @@ def new_swarm( listen_addrs: Sequence[multiaddr.Multiaddr] | None = None, enable_quic: bool = False, retry_config: Optional["RetryConfig"] = None, - connection_config: "ConnectionConfig" | QUICTransportConfig | None = None, + connection_config: ConnectionConfig | QUICTransportConfig | None = None, ) -> INetworkService: """ Create a swarm instance based on the parameters. diff --git a/libp2p/network/config.py b/libp2p/network/config.py new file mode 100644 index 00000000..33934ed5 --- /dev/null +++ b/libp2p/network/config.py @@ -0,0 +1,54 @@ +from dataclasses import dataclass + + +@dataclass +class RetryConfig: + """ + Configuration for retry logic with exponential backoff. + + This configuration controls how connection attempts are retried when they fail. + The retry mechanism uses exponential backoff with jitter to prevent thundering + herd problems in distributed systems. + + Attributes: + max_retries: Maximum number of retry attempts before giving up. + Default: 3 attempts + initial_delay: Initial delay in seconds before the first retry. + Default: 0.1 seconds (100ms) + max_delay: Maximum delay cap in seconds to prevent excessive wait times. + Default: 30.0 seconds + backoff_multiplier: Multiplier for exponential backoff (each retry multiplies + the delay by this factor). Default: 2.0 (doubles each time) + jitter_factor: Random jitter factor (0.0-1.0) to add randomness to delays + and prevent synchronized retries. Default: 0.1 (10% jitter) + + """ + + max_retries: int = 3 + initial_delay: float = 0.1 + max_delay: float = 30.0 + backoff_multiplier: float = 2.0 + jitter_factor: float = 0.1 + + +@dataclass +class ConnectionConfig: + """ + Configuration for multi-connection support. + + This configuration controls how multiple connections per peer are managed, + including connection limits, timeouts, and load balancing strategies. + + Attributes: + max_connections_per_peer: Maximum number of connections allowed to a single + peer. Default: 3 connections + connection_timeout: Timeout in seconds for establishing new connections. + Default: 30.0 seconds + load_balancing_strategy: Strategy for distributing streams across connections. + Options: "round_robin" (default) or "least_loaded" + + """ + + max_connections_per_peer: int = 3 + connection_timeout: float = 30.0 + load_balancing_strategy: str = "round_robin" # or "least_loaded" diff --git a/libp2p/network/swarm.py b/libp2p/network/swarm.py index 3ceaf08d..800c55b2 100644 --- a/libp2p/network/swarm.py +++ b/libp2p/network/swarm.py @@ -2,7 +2,6 @@ from collections.abc import ( Awaitable, Callable, ) -from dataclasses import dataclass import logging import random from typing import cast @@ -28,6 +27,7 @@ from libp2p.custom_types import ( from libp2p.io.abc import ( ReadWriteCloser, ) +from libp2p.network.config import ConnectionConfig, RetryConfig from libp2p.peer.id import ( ID, ) @@ -65,59 +65,6 @@ from .exceptions import ( logger = logging.getLogger("libp2p.network.swarm") -@dataclass -class RetryConfig: - """ - Configuration for retry logic with exponential backoff. - - This configuration controls how connection attempts are retried when they fail. - The retry mechanism uses exponential backoff with jitter to prevent thundering - herd problems in distributed systems. - - Attributes: - max_retries: Maximum number of retry attempts before giving up. - Default: 3 attempts - initial_delay: Initial delay in seconds before the first retry. - Default: 0.1 seconds (100ms) - max_delay: Maximum delay cap in seconds to prevent excessive wait times. - Default: 30.0 seconds - backoff_multiplier: Multiplier for exponential backoff (each retry multiplies - the delay by this factor). Default: 2.0 (doubles each time) - jitter_factor: Random jitter factor (0.0-1.0) to add randomness to delays - and prevent synchronized retries. Default: 0.1 (10% jitter) - - """ - - max_retries: int = 3 - initial_delay: float = 0.1 - max_delay: float = 30.0 - backoff_multiplier: float = 2.0 - jitter_factor: float = 0.1 - - -@dataclass -class ConnectionConfig: - """ - Configuration for multi-connection support. - - This configuration controls how multiple connections per peer are managed, - including connection limits, timeouts, and load balancing strategies. - - Attributes: - max_connections_per_peer: Maximum number of connections allowed to a single - peer. Default: 3 connections - connection_timeout: Timeout in seconds for establishing new connections. - Default: 30.0 seconds - load_balancing_strategy: Strategy for distributing streams across connections. - Options: "round_robin" (default) or "least_loaded" - - """ - - max_connections_per_peer: int = 3 - connection_timeout: float = 30.0 - load_balancing_strategy: str = "round_robin" # or "least_loaded" - - def create_default_stream_handler(network: INetworkService) -> StreamHandlerFn: async def stream_handler(stream: INetStream) -> None: await network.get_manager().wait_finished() diff --git a/libp2p/transport/quic/config.py b/libp2p/transport/quic/config.py index 8f4231e5..5b70f0e5 100644 --- a/libp2p/transport/quic/config.py +++ b/libp2p/transport/quic/config.py @@ -10,6 +10,7 @@ import ssl from typing import Any, Literal, TypedDict from libp2p.custom_types import TProtocol +from libp2p.network.config import ConnectionConfig class QUICTransportKwargs(TypedDict, total=False): @@ -47,12 +48,10 @@ class QUICTransportKwargs(TypedDict, total=False): @dataclass -class QUICTransportConfig: +class QUICTransportConfig(ConnectionConfig): """Configuration for QUIC transport.""" # Connection settings - max_connections_per_peer: int = 3 - load_balancing_strategy: str = "round_robin" idle_timeout: float = 30.0 # Seconds before an idle connection is closed. max_datagram_size: int = ( 1200 # Maximum size of UDP datagrams to avoid IP fragmentation.