mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
fix: format code to pass CI lint
This commit is contained in:
@ -32,16 +32,12 @@ async def example_basic_enhanced_swarm() -> None:
|
||||
default_connection = ConnectionConfig()
|
||||
|
||||
logger.info(f"Swarm created with peer ID: {swarm.get_peer_id()}")
|
||||
logger.info(
|
||||
f"Retry config: max_retries={default_retry.max_retries}"
|
||||
)
|
||||
logger.info(f"Retry config: max_retries={default_retry.max_retries}")
|
||||
logger.info(
|
||||
f"Connection config: max_connections_per_peer="
|
||||
f"{default_connection.max_connections_per_peer}"
|
||||
)
|
||||
logger.info(
|
||||
f"Connection pool enabled: {default_connection.enable_connection_pool}"
|
||||
)
|
||||
logger.info(f"Connection pool enabled: {default_connection.enable_connection_pool}")
|
||||
|
||||
await swarm.close()
|
||||
logger.info("Basic enhanced Swarm example completed")
|
||||
@ -53,32 +49,22 @@ async def example_custom_retry_config() -> None:
|
||||
|
||||
# Custom retry configuration for aggressive retry behavior
|
||||
retry_config = RetryConfig(
|
||||
max_retries=5, # More retries
|
||||
initial_delay=0.05, # Faster initial retry
|
||||
max_delay=10.0, # Lower max delay
|
||||
max_retries=5, # More retries
|
||||
initial_delay=0.05, # Faster initial retry
|
||||
max_delay=10.0, # Lower max delay
|
||||
backoff_multiplier=1.5, # Less aggressive backoff
|
||||
jitter_factor=0.2 # More jitter
|
||||
jitter_factor=0.2, # More jitter
|
||||
)
|
||||
|
||||
# Create swarm with custom retry config
|
||||
swarm = new_swarm(retry_config=retry_config)
|
||||
|
||||
logger.info("Custom retry config applied:")
|
||||
logger.info(
|
||||
f" Max retries: {retry_config.max_retries}"
|
||||
)
|
||||
logger.info(
|
||||
f" Initial delay: {retry_config.initial_delay}s"
|
||||
)
|
||||
logger.info(
|
||||
f" Max delay: {retry_config.max_delay}s"
|
||||
)
|
||||
logger.info(
|
||||
f" Backoff multiplier: {retry_config.backoff_multiplier}"
|
||||
)
|
||||
logger.info(
|
||||
f" Jitter factor: {retry_config.jitter_factor}"
|
||||
)
|
||||
logger.info(f" Max retries: {retry_config.max_retries}")
|
||||
logger.info(f" Initial delay: {retry_config.initial_delay}s")
|
||||
logger.info(f" Max delay: {retry_config.max_delay}s")
|
||||
logger.info(f" Backoff multiplier: {retry_config.backoff_multiplier}")
|
||||
logger.info(f" Jitter factor: {retry_config.jitter_factor}")
|
||||
|
||||
await swarm.close()
|
||||
logger.info("Custom retry config example completed")
|
||||
@ -90,10 +76,10 @@ async def example_custom_connection_config() -> None:
|
||||
|
||||
# Custom connection configuration for high-performance scenarios
|
||||
connection_config = ConnectionConfig(
|
||||
max_connections_per_peer=5, # More connections per peer
|
||||
connection_timeout=60.0, # Longer timeout
|
||||
enable_connection_pool=True, # Enable connection pooling
|
||||
load_balancing_strategy="least_loaded" # Use least loaded strategy
|
||||
max_connections_per_peer=5, # More connections per peer
|
||||
connection_timeout=60.0, # Longer timeout
|
||||
enable_connection_pool=True, # Enable connection pooling
|
||||
load_balancing_strategy="least_loaded", # Use least loaded strategy
|
||||
)
|
||||
|
||||
# Create swarm with custom connection config
|
||||
@ -101,19 +87,14 @@ async def example_custom_connection_config() -> None:
|
||||
|
||||
logger.info("Custom connection config applied:")
|
||||
logger.info(
|
||||
f" Max connections per peer: "
|
||||
f"{connection_config.max_connections_per_peer}"
|
||||
f" Max connections per peer: {connection_config.max_connections_per_peer}"
|
||||
)
|
||||
logger.info(f" Connection timeout: {connection_config.connection_timeout}s")
|
||||
logger.info(
|
||||
f" Connection pool enabled: {connection_config.enable_connection_pool}"
|
||||
)
|
||||
logger.info(
|
||||
f" Connection timeout: {connection_config.connection_timeout}s"
|
||||
)
|
||||
logger.info(
|
||||
f" Connection pool enabled: "
|
||||
f"{connection_config.enable_connection_pool}"
|
||||
)
|
||||
logger.info(
|
||||
f" Load balancing strategy: "
|
||||
f"{connection_config.load_balancing_strategy}"
|
||||
f" Load balancing strategy: {connection_config.load_balancing_strategy}"
|
||||
)
|
||||
|
||||
await swarm.close()
|
||||
@ -134,12 +115,8 @@ async def example_backward_compatibility() -> None:
|
||||
logger.info(
|
||||
f" Connection pool enabled: {connection_config.enable_connection_pool}"
|
||||
)
|
||||
logger.info(
|
||||
f" Connections dict type: {type(swarm.connections)}"
|
||||
)
|
||||
logger.info(
|
||||
" Retry logic still available: 3 max retries"
|
||||
)
|
||||
logger.info(f" Connections dict type: {type(swarm.connections)}")
|
||||
logger.info(" Retry logic still available: 3 max retries")
|
||||
|
||||
await swarm.close()
|
||||
logger.info("Backward compatibility example completed")
|
||||
@ -151,38 +128,31 @@ async def example_production_ready_config() -> None:
|
||||
|
||||
# Production-ready retry configuration
|
||||
retry_config = RetryConfig(
|
||||
max_retries=3, # Reasonable retry limit
|
||||
initial_delay=0.1, # Quick initial retry
|
||||
max_delay=30.0, # Cap exponential backoff
|
||||
max_retries=3, # Reasonable retry limit
|
||||
initial_delay=0.1, # Quick initial retry
|
||||
max_delay=30.0, # Cap exponential backoff
|
||||
backoff_multiplier=2.0, # Standard exponential backoff
|
||||
jitter_factor=0.1 # Small jitter to prevent thundering herd
|
||||
jitter_factor=0.1, # Small jitter to prevent thundering herd
|
||||
)
|
||||
|
||||
# Production-ready connection configuration
|
||||
connection_config = ConnectionConfig(
|
||||
max_connections_per_peer=3, # Balance between performance and resource usage
|
||||
connection_timeout=30.0, # Reasonable timeout
|
||||
enable_connection_pool=True, # Enable for better performance
|
||||
load_balancing_strategy="round_robin" # Simple, predictable strategy
|
||||
connection_timeout=30.0, # Reasonable timeout
|
||||
enable_connection_pool=True, # Enable for better performance
|
||||
load_balancing_strategy="round_robin", # Simple, predictable strategy
|
||||
)
|
||||
|
||||
# Create swarm with production config
|
||||
swarm = new_swarm(
|
||||
retry_config=retry_config,
|
||||
connection_config=connection_config
|
||||
)
|
||||
swarm = new_swarm(retry_config=retry_config, connection_config=connection_config)
|
||||
|
||||
logger.info("Production-ready configuration applied:")
|
||||
logger.info(
|
||||
f" Retry: {retry_config.max_retries} retries, "
|
||||
f"{retry_config.max_delay}s max delay"
|
||||
)
|
||||
logger.info(
|
||||
f" Connections: {connection_config.max_connections_per_peer} per peer"
|
||||
)
|
||||
logger.info(
|
||||
f" Load balancing: {connection_config.load_balancing_strategy}"
|
||||
)
|
||||
logger.info(f" Connections: {connection_config.max_connections_per_peer} per peer")
|
||||
logger.info(f" Load balancing: {connection_config.load_balancing_strategy}")
|
||||
|
||||
await swarm.close()
|
||||
logger.info("Production-ready configuration example completed")
|
||||
|
||||
@ -196,8 +196,7 @@ async def test_enhanced_swarm_constructor():
|
||||
# Test with custom config
|
||||
custom_retry = RetryConfig(max_retries=5, initial_delay=0.5)
|
||||
custom_conn = ConnectionConfig(
|
||||
max_connections_per_peer=5,
|
||||
enable_connection_pool=False
|
||||
max_connections_per_peer=5, enable_connection_pool=False
|
||||
)
|
||||
|
||||
swarm = Swarm(peer_id, peerstore, upgrader, transport, custom_retry, custom_conn)
|
||||
@ -216,10 +215,7 @@ async def test_swarm_backoff_calculation():
|
||||
transport = Mock()
|
||||
|
||||
retry_config = RetryConfig(
|
||||
initial_delay=0.1,
|
||||
max_delay=1.0,
|
||||
backoff_multiplier=2.0,
|
||||
jitter_factor=0.1
|
||||
initial_delay=0.1, max_delay=1.0, backoff_multiplier=2.0, jitter_factor=0.1
|
||||
)
|
||||
|
||||
swarm = Swarm(peer_id, peerstore, upgrader, transport, retry_config)
|
||||
@ -254,7 +250,7 @@ async def test_swarm_retry_logic():
|
||||
retry_config = RetryConfig(
|
||||
max_retries=2,
|
||||
initial_delay=0.01, # Very short for testing
|
||||
max_delay=0.1
|
||||
max_delay=0.1,
|
||||
)
|
||||
|
||||
swarm = Swarm(peer_id, peerstore, upgrader, transport, retry_config)
|
||||
@ -294,15 +290,11 @@ async def test_swarm_multi_connection_support():
|
||||
connection_config = ConnectionConfig(
|
||||
max_connections_per_peer=3,
|
||||
enable_connection_pool=True,
|
||||
load_balancing_strategy="round_robin"
|
||||
load_balancing_strategy="round_robin",
|
||||
)
|
||||
|
||||
swarm = Swarm(
|
||||
peer_id,
|
||||
peerstore,
|
||||
upgrader,
|
||||
transport,
|
||||
connection_config=connection_config
|
||||
peer_id, peerstore, upgrader, transport, connection_config=connection_config
|
||||
)
|
||||
|
||||
# Mock connection pool methods
|
||||
@ -330,8 +322,7 @@ async def test_swarm_backward_compatibility():
|
||||
# Create swarm with connection pool disabled
|
||||
connection_config = ConnectionConfig(enable_connection_pool=False)
|
||||
swarm = Swarm(
|
||||
peer_id, peerstore, upgrader, transport,
|
||||
connection_config=connection_config
|
||||
peer_id, peerstore, upgrader, transport, connection_config=connection_config
|
||||
)
|
||||
|
||||
# Should behave like original swarm
|
||||
@ -355,13 +346,11 @@ async def test_swarm_connection_pool_integration():
|
||||
transport = Mock()
|
||||
|
||||
connection_config = ConnectionConfig(
|
||||
max_connections_per_peer=2,
|
||||
enable_connection_pool=True
|
||||
max_connections_per_peer=2, enable_connection_pool=True
|
||||
)
|
||||
|
||||
swarm = Swarm(
|
||||
peer_id, peerstore, upgrader, transport,
|
||||
connection_config=connection_config
|
||||
peer_id, peerstore, upgrader, transport, connection_config=connection_config
|
||||
)
|
||||
|
||||
# Mock successful connection creation
|
||||
@ -394,8 +383,7 @@ async def test_swarm_connection_cleanup():
|
||||
|
||||
connection_config = ConnectionConfig(enable_connection_pool=True)
|
||||
swarm = Swarm(
|
||||
peer_id, peerstore, upgrader, transport,
|
||||
connection_config=connection_config
|
||||
peer_id, peerstore, upgrader, transport, connection_config=connection_config
|
||||
)
|
||||
|
||||
# Add a connection
|
||||
|
||||
Reference in New Issue
Block a user