mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
fix: type assertion for config class
This commit is contained in:
@ -42,10 +42,12 @@ from libp2p.host.routed_host import (
|
|||||||
RoutedHost,
|
RoutedHost,
|
||||||
)
|
)
|
||||||
from libp2p.network.swarm import (
|
from libp2p.network.swarm import (
|
||||||
ConnectionConfig,
|
|
||||||
RetryConfig,
|
|
||||||
Swarm,
|
Swarm,
|
||||||
)
|
)
|
||||||
|
from libp2p.network.config import (
|
||||||
|
ConnectionConfig,
|
||||||
|
RetryConfig
|
||||||
|
)
|
||||||
from libp2p.peer.id import (
|
from libp2p.peer.id import (
|
||||||
ID,
|
ID,
|
||||||
)
|
)
|
||||||
@ -169,7 +171,7 @@ def new_swarm(
|
|||||||
listen_addrs: Sequence[multiaddr.Multiaddr] | None = None,
|
listen_addrs: Sequence[multiaddr.Multiaddr] | None = None,
|
||||||
enable_quic: bool = False,
|
enable_quic: bool = False,
|
||||||
retry_config: Optional["RetryConfig"] = None,
|
retry_config: Optional["RetryConfig"] = None,
|
||||||
connection_config: "ConnectionConfig" | QUICTransportConfig | None = None,
|
connection_config: ConnectionConfig | QUICTransportConfig | None = None,
|
||||||
) -> INetworkService:
|
) -> INetworkService:
|
||||||
"""
|
"""
|
||||||
Create a swarm instance based on the parameters.
|
Create a swarm instance based on the parameters.
|
||||||
|
|||||||
54
libp2p/network/config.py
Normal file
54
libp2p/network/config.py
Normal file
@ -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"
|
||||||
@ -2,7 +2,6 @@ from collections.abc import (
|
|||||||
Awaitable,
|
Awaitable,
|
||||||
Callable,
|
Callable,
|
||||||
)
|
)
|
||||||
from dataclasses import dataclass
|
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
from typing import cast
|
from typing import cast
|
||||||
@ -28,6 +27,7 @@ from libp2p.custom_types import (
|
|||||||
from libp2p.io.abc import (
|
from libp2p.io.abc import (
|
||||||
ReadWriteCloser,
|
ReadWriteCloser,
|
||||||
)
|
)
|
||||||
|
from libp2p.network.config import ConnectionConfig, RetryConfig
|
||||||
from libp2p.peer.id import (
|
from libp2p.peer.id import (
|
||||||
ID,
|
ID,
|
||||||
)
|
)
|
||||||
@ -65,59 +65,6 @@ from .exceptions import (
|
|||||||
logger = logging.getLogger("libp2p.network.swarm")
|
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:
|
def create_default_stream_handler(network: INetworkService) -> StreamHandlerFn:
|
||||||
async def stream_handler(stream: INetStream) -> None:
|
async def stream_handler(stream: INetStream) -> None:
|
||||||
await network.get_manager().wait_finished()
|
await network.get_manager().wait_finished()
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import ssl
|
|||||||
from typing import Any, Literal, TypedDict
|
from typing import Any, Literal, TypedDict
|
||||||
|
|
||||||
from libp2p.custom_types import TProtocol
|
from libp2p.custom_types import TProtocol
|
||||||
|
from libp2p.network.config import ConnectionConfig
|
||||||
|
|
||||||
|
|
||||||
class QUICTransportKwargs(TypedDict, total=False):
|
class QUICTransportKwargs(TypedDict, total=False):
|
||||||
@ -47,12 +48,10 @@ class QUICTransportKwargs(TypedDict, total=False):
|
|||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class QUICTransportConfig:
|
class QUICTransportConfig(ConnectionConfig):
|
||||||
"""Configuration for QUIC transport."""
|
"""Configuration for QUIC transport."""
|
||||||
|
|
||||||
# Connection settings
|
# 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.
|
idle_timeout: float = 30.0 # Seconds before an idle connection is closed.
|
||||||
max_datagram_size: int = (
|
max_datagram_size: int = (
|
||||||
1200 # Maximum size of UDP datagrams to avoid IP fragmentation.
|
1200 # Maximum size of UDP datagrams to avoid IP fragmentation.
|
||||||
|
|||||||
Reference in New Issue
Block a user