Skip to content

Commit 3d2752e

Browse files
committed
Add test cases for health monitor
1 parent 5fefc0e commit 3d2752e

File tree

6 files changed

+2388
-20
lines changed

6 files changed

+2388
-20
lines changed

libp2p/__init__.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -365,28 +365,36 @@ def new_host(
365365
effective_connection_config = quic_transport_opt
366366

367367
# If both connection_config and quic_transport_opt are provided,
368-
# merge health monitoring settings
368+
# merge ALL connection and health monitoring settings
369369
if connection_config is not None:
370-
# Merge health monitoring settings from connection_config
371-
# into quic_transport_opt
372-
if hasattr(connection_config, "enable_health_monitoring"):
373-
quic_transport_opt.enable_health_monitoring = (
374-
connection_config.enable_health_monitoring
375-
)
376-
if hasattr(connection_config, "health_check_interval"):
377-
quic_transport_opt.health_check_interval = (
378-
connection_config.health_check_interval
379-
)
380-
if hasattr(connection_config, "load_balancing_strategy"):
381-
quic_transport_opt.load_balancing_strategy = (
382-
connection_config.load_balancing_strategy
383-
)
384-
if hasattr(connection_config, "max_connections_per_peer"):
385-
quic_transport_opt.max_connections_per_peer = (
386-
connection_config.max_connections_per_peer
387-
)
370+
# Merge all ConnectionConfig attributes from connection_config
371+
# into quic_transport_opt (which inherits from ConnectionConfig)
372+
connection_config_attrs = [
373+
"max_connections_per_peer",
374+
"connection_timeout",
375+
"load_balancing_strategy",
376+
"enable_health_monitoring",
377+
"health_initial_delay",
378+
"health_warmup_window",
379+
"health_check_interval",
380+
"ping_timeout",
381+
"min_health_threshold",
382+
"min_connections_per_peer",
383+
"latency_weight",
384+
"success_rate_weight",
385+
"stability_weight",
386+
"max_ping_latency",
387+
"min_ping_success_rate",
388+
"max_failed_streams",
389+
"unhealthy_grace_period",
390+
]
391+
392+
for attr in connection_config_attrs:
393+
if hasattr(connection_config, attr):
394+
setattr(quic_transport_opt, attr, getattr(connection_config, attr))
395+
388396
logger.info(
389-
"Merged health monitoring settings from "
397+
"Merged all connection and health monitoring settings from "
390398
"connection_config into QUIC config"
391399
)
392400
elif connection_config is not None:

newsfragments/915.feature.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Add comprehensive connection health monitoring and intelligent load balancing to libp2p.
2+
3+
**Connection Health Metrics:**
4+
- Implements ConnectionHealth dataclass with latency tracking, success rates, bandwidth monitoring, and error history
5+
- Provides weighted health scoring algorithm with configurable weights (latency, success rate, stability)
6+
- Tracks connection age, idle time, stream lifecycle, and performance trends
7+
- Monitors bandwidth usage with time-windowed tracking and peak/average calculations
8+
9+
**Proactive Monitoring Service:**
10+
- Implements ConnectionHealthMonitor service with periodic health checks and automatic connection replacement
11+
- Performs non-intrusive ping-based health verification with configurable intervals
12+
- Supports warmup windows and grace periods to prevent premature connection replacement
13+
- Automatically maintains minimum connection count per peer while replacing unhealthy connections
14+
15+
**Health-Aware Load Balancing:**
16+
- Adds four connection selection strategies: round_robin, least_loaded, health_based, and latency_based
17+
- Routes traffic to healthiest/lowest-latency connections for optimal performance
18+
- Provides fallback behavior when health data unavailable
19+
20+
**API Consistency Fix:**
21+
- Extends new_host() to accept connection_config parameter, resolving previous API inconsistency
22+
- Maintains full backward compatibility with existing code
23+
- Supports health monitoring configuration through high-level host API
24+
- Properly merges health settings with QUIC transport configuration when both are provided
25+
26+
**Configuration and Integration:**
27+
- Adds comprehensive ConnectionConfig options for health monitoring customization
28+
- Integrates health tracking throughout connection lifecycle (establishment, usage, closure)
29+
- Provides health summary and metrics export through host interface
30+
- Includes extensive test coverage with 80+ new tests covering all components

0 commit comments

Comments
 (0)