fix: format code to pass CI lint

This commit is contained in:
bomanaps
2025-08-28 22:18:33 +01:00
parent c577fd2f71
commit 9fa3afbb04
2 changed files with 42 additions and 84 deletions

View File

@ -32,16 +32,12 @@ async def example_basic_enhanced_swarm() -> None:
default_connection = ConnectionConfig() default_connection = ConnectionConfig()
logger.info(f"Swarm created with peer ID: {swarm.get_peer_id()}") logger.info(f"Swarm created with peer ID: {swarm.get_peer_id()}")
logger.info( logger.info(f"Retry config: max_retries={default_retry.max_retries}")
f"Retry config: max_retries={default_retry.max_retries}"
)
logger.info( logger.info(
f"Connection config: max_connections_per_peer=" f"Connection config: max_connections_per_peer="
f"{default_connection.max_connections_per_peer}" f"{default_connection.max_connections_per_peer}"
) )
logger.info( logger.info(f"Connection pool enabled: {default_connection.enable_connection_pool}")
f"Connection pool enabled: {default_connection.enable_connection_pool}"
)
await swarm.close() await swarm.close()
logger.info("Basic enhanced Swarm example completed") 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 # Custom retry configuration for aggressive retry behavior
retry_config = RetryConfig( retry_config = RetryConfig(
max_retries=5, # More retries max_retries=5, # More retries
initial_delay=0.05, # Faster initial retry initial_delay=0.05, # Faster initial retry
max_delay=10.0, # Lower max delay max_delay=10.0, # Lower max delay
backoff_multiplier=1.5, # Less aggressive backoff 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 # Create swarm with custom retry config
swarm = new_swarm(retry_config=retry_config) swarm = new_swarm(retry_config=retry_config)
logger.info("Custom retry config applied:") logger.info("Custom retry config applied:")
logger.info( logger.info(f" Max retries: {retry_config.max_retries}")
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( logger.info(f" Backoff multiplier: {retry_config.backoff_multiplier}")
f" Initial delay: {retry_config.initial_delay}s" logger.info(f" Jitter factor: {retry_config.jitter_factor}")
)
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() await swarm.close()
logger.info("Custom retry config example completed") 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 # Custom connection configuration for high-performance scenarios
connection_config = ConnectionConfig( connection_config = ConnectionConfig(
max_connections_per_peer=5, # More connections per peer max_connections_per_peer=5, # More connections per peer
connection_timeout=60.0, # Longer timeout connection_timeout=60.0, # Longer timeout
enable_connection_pool=True, # Enable connection pooling enable_connection_pool=True, # Enable connection pooling
load_balancing_strategy="least_loaded" # Use least loaded strategy load_balancing_strategy="least_loaded", # Use least loaded strategy
) )
# Create swarm with custom connection config # 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("Custom connection config applied:")
logger.info( logger.info(
f" Max connections per peer: " f" Max connections per peer: {connection_config.max_connections_per_peer}"
f"{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( logger.info(
f" Connection timeout: {connection_config.connection_timeout}s" f" Load balancing strategy: {connection_config.load_balancing_strategy}"
)
logger.info(
f" Connection pool enabled: "
f"{connection_config.enable_connection_pool}"
)
logger.info(
f" Load balancing strategy: "
f"{connection_config.load_balancing_strategy}"
) )
await swarm.close() await swarm.close()
@ -134,12 +115,8 @@ async def example_backward_compatibility() -> None:
logger.info( logger.info(
f" Connection pool enabled: {connection_config.enable_connection_pool}" f" Connection pool enabled: {connection_config.enable_connection_pool}"
) )
logger.info( logger.info(f" Connections dict type: {type(swarm.connections)}")
f" Connections dict type: {type(swarm.connections)}" logger.info(" Retry logic still available: 3 max retries")
)
logger.info(
" Retry logic still available: 3 max retries"
)
await swarm.close() await swarm.close()
logger.info("Backward compatibility example completed") logger.info("Backward compatibility example completed")
@ -151,38 +128,31 @@ async def example_production_ready_config() -> None:
# Production-ready retry configuration # Production-ready retry configuration
retry_config = RetryConfig( retry_config = RetryConfig(
max_retries=3, # Reasonable retry limit max_retries=3, # Reasonable retry limit
initial_delay=0.1, # Quick initial retry initial_delay=0.1, # Quick initial retry
max_delay=30.0, # Cap exponential backoff max_delay=30.0, # Cap exponential backoff
backoff_multiplier=2.0, # Standard 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 # Production-ready connection configuration
connection_config = ConnectionConfig( connection_config = ConnectionConfig(
max_connections_per_peer=3, # Balance between performance and resource usage max_connections_per_peer=3, # Balance between performance and resource usage
connection_timeout=30.0, # Reasonable timeout connection_timeout=30.0, # Reasonable timeout
enable_connection_pool=True, # Enable for better performance enable_connection_pool=True, # Enable for better performance
load_balancing_strategy="round_robin" # Simple, predictable strategy load_balancing_strategy="round_robin", # Simple, predictable strategy
) )
# Create swarm with production config # Create swarm with production config
swarm = new_swarm( swarm = new_swarm(retry_config=retry_config, connection_config=connection_config)
retry_config=retry_config,
connection_config=connection_config
)
logger.info("Production-ready configuration applied:") logger.info("Production-ready configuration applied:")
logger.info( logger.info(
f" Retry: {retry_config.max_retries} retries, " f" Retry: {retry_config.max_retries} retries, "
f"{retry_config.max_delay}s max delay" f"{retry_config.max_delay}s max delay"
) )
logger.info( logger.info(f" Connections: {connection_config.max_connections_per_peer} per peer")
f" Connections: {connection_config.max_connections_per_peer} per peer" logger.info(f" Load balancing: {connection_config.load_balancing_strategy}")
)
logger.info(
f" Load balancing: {connection_config.load_balancing_strategy}"
)
await swarm.close() await swarm.close()
logger.info("Production-ready configuration example completed") logger.info("Production-ready configuration example completed")

View File

@ -196,8 +196,7 @@ async def test_enhanced_swarm_constructor():
# Test with custom config # Test with custom config
custom_retry = RetryConfig(max_retries=5, initial_delay=0.5) custom_retry = RetryConfig(max_retries=5, initial_delay=0.5)
custom_conn = ConnectionConfig( custom_conn = ConnectionConfig(
max_connections_per_peer=5, max_connections_per_peer=5, enable_connection_pool=False
enable_connection_pool=False
) )
swarm = Swarm(peer_id, peerstore, upgrader, transport, custom_retry, custom_conn) swarm = Swarm(peer_id, peerstore, upgrader, transport, custom_retry, custom_conn)
@ -216,10 +215,7 @@ async def test_swarm_backoff_calculation():
transport = Mock() transport = Mock()
retry_config = RetryConfig( retry_config = RetryConfig(
initial_delay=0.1, initial_delay=0.1, max_delay=1.0, backoff_multiplier=2.0, jitter_factor=0.1
max_delay=1.0,
backoff_multiplier=2.0,
jitter_factor=0.1
) )
swarm = Swarm(peer_id, peerstore, upgrader, transport, retry_config) swarm = Swarm(peer_id, peerstore, upgrader, transport, retry_config)
@ -254,7 +250,7 @@ async def test_swarm_retry_logic():
retry_config = RetryConfig( retry_config = RetryConfig(
max_retries=2, max_retries=2,
initial_delay=0.01, # Very short for testing 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) swarm = Swarm(peer_id, peerstore, upgrader, transport, retry_config)
@ -294,15 +290,11 @@ async def test_swarm_multi_connection_support():
connection_config = ConnectionConfig( connection_config = ConnectionConfig(
max_connections_per_peer=3, max_connections_per_peer=3,
enable_connection_pool=True, enable_connection_pool=True,
load_balancing_strategy="round_robin" load_balancing_strategy="round_robin",
) )
swarm = Swarm( swarm = Swarm(
peer_id, peer_id, peerstore, upgrader, transport, connection_config=connection_config
peerstore,
upgrader,
transport,
connection_config=connection_config
) )
# Mock connection pool methods # Mock connection pool methods
@ -330,8 +322,7 @@ async def test_swarm_backward_compatibility():
# Create swarm with connection pool disabled # Create swarm with connection pool disabled
connection_config = ConnectionConfig(enable_connection_pool=False) connection_config = ConnectionConfig(enable_connection_pool=False)
swarm = Swarm( swarm = Swarm(
peer_id, peerstore, upgrader, transport, peer_id, peerstore, upgrader, transport, connection_config=connection_config
connection_config=connection_config
) )
# Should behave like original swarm # Should behave like original swarm
@ -355,13 +346,11 @@ async def test_swarm_connection_pool_integration():
transport = Mock() transport = Mock()
connection_config = ConnectionConfig( connection_config = ConnectionConfig(
max_connections_per_peer=2, max_connections_per_peer=2, enable_connection_pool=True
enable_connection_pool=True
) )
swarm = Swarm( swarm = Swarm(
peer_id, peerstore, upgrader, transport, peer_id, peerstore, upgrader, transport, connection_config=connection_config
connection_config=connection_config
) )
# Mock successful connection creation # Mock successful connection creation
@ -394,8 +383,7 @@ async def test_swarm_connection_cleanup():
connection_config = ConnectionConfig(enable_connection_pool=True) connection_config = ConnectionConfig(enable_connection_pool=True)
swarm = Swarm( swarm = Swarm(
peer_id, peerstore, upgrader, transport, peer_id, peerstore, upgrader, transport, connection_config=connection_config
connection_config=connection_config
) )
# Add a connection # Add a connection