Skip to content

Commit aad87f9

Browse files
committed
Adress documentation comment
1 parent 9a06ee4 commit aad87f9

File tree

2 files changed

+104
-10
lines changed

2 files changed

+104
-10
lines changed

docs/examples.multiple_connections.rst

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,91 @@ Production Configuration
9696

9797
For production use, consider these settings:
9898

99+
**RetryConfig Parameters**
100+
101+
The `RetryConfig` class controls connection retry behavior with exponential backoff:
102+
103+
- **max_retries**: Maximum number of retry attempts before giving up (default: 3)
104+
- **initial_delay**: Initial delay in seconds before the first retry (default: 0.1s)
105+
- **max_delay**: Maximum delay cap to prevent excessive wait times (default: 30.0s)
106+
- **backoff_multiplier**: Exponential backoff multiplier - each retry multiplies delay by this factor (default: 2.0)
107+
- **jitter_factor**: Random jitter (0.0-1.0) to prevent synchronized retries (default: 0.1)
108+
109+
**ConnectionConfig Parameters**
110+
111+
The `ConnectionConfig` class manages multi-connection behavior:
112+
113+
- **max_connections_per_peer**: Maximum connections allowed to a single peer (default: 3)
114+
- **connection_timeout**: Timeout for establishing new connections in seconds (default: 30.0s)
115+
- **load_balancing_strategy**: Strategy for distributing streams ("round_robin" or "least_loaded")
116+
117+
**Load Balancing Strategies Explained**
118+
119+
- **round_robin**: Cycles through connections in order, distributing load evenly. Simple and predictable.
120+
- **least_loaded**: Selects the connection with the fewest active streams. Better for performance but more complex.
121+
99122
.. code-block:: python
100123
101124
from libp2p.network.swarm import ConnectionConfig, RetryConfig
102125
103126
# Production-ready configuration
104127
retry_config = RetryConfig(
105-
max_retries=3,
106-
initial_delay=0.1,
107-
max_delay=30.0,
108-
backoff_multiplier=2.0,
109-
jitter_factor=0.1
128+
max_retries=3, # Maximum retry attempts before giving up
129+
initial_delay=0.1, # Start with 100ms delay
130+
max_delay=30.0, # Cap exponential backoff at 30 seconds
131+
backoff_multiplier=2.0, # Double delay each retry (100ms -> 200ms -> 400ms)
132+
jitter_factor=0.1 # Add 10% random jitter to prevent thundering herd
110133
)
111134
112135
connection_config = ConnectionConfig(
113-
max_connections_per_peer=3, # Balance performance and resources
114-
connection_timeout=30.0, # Reasonable timeout
115-
load_balancing_strategy="round_robin" # Predictable behavior
136+
max_connections_per_peer=3, # Allow up to 3 connections per peer
137+
connection_timeout=30.0, # 30 second timeout for new connections
138+
load_balancing_strategy="round_robin" # Simple, predictable load distribution
116139
)
117140
118141
swarm = new_swarm(
119142
retry_config=retry_config,
120143
connection_config=connection_config
121144
)
122145
146+
**How RetryConfig Works in Practice**
147+
148+
With the configuration above, connection retries follow this pattern:
149+
150+
1. **Attempt 1**: Immediate connection attempt
151+
2. **Attempt 2**: Wait 100ms ± 10ms jitter, then retry
152+
3. **Attempt 3**: Wait 200ms ± 20ms jitter, then retry
153+
4. **Attempt 4**: Wait 400ms ± 40ms jitter, then retry
154+
5. **Attempt 5**: Wait 800ms ± 80ms jitter, then retry
155+
6. **Attempt 6**: Wait 1.6s ± 160ms jitter, then retry
156+
7. **Attempt 7**: Wait 3.2s ± 320ms jitter, then retry
157+
8. **Attempt 8**: Wait 6.4s ± 640ms jitter, then retry
158+
9. **Attempt 9**: Wait 12.8s ± 1.28s jitter, then retry
159+
10. **Attempt 10**: Wait 25.6s ± 2.56s jitter, then retry
160+
11. **Attempt 11**: Wait 30.0s (capped) ± 3.0s jitter, then retry
161+
12. **Attempt 12**: Wait 30.0s (capped) ± 3.0s jitter, then retry
162+
13. **Give up**: After 12 retries (3 initial + 9 retries), connection fails
163+
164+
The jitter prevents multiple clients from retrying simultaneously, reducing server load.
165+
166+
**Parameter Tuning Guidelines**
167+
168+
**For Development/Testing:**
169+
- Use lower `max_retries` (1-2) and shorter delays for faster feedback
170+
- Example: `RetryConfig(max_retries=2, initial_delay=0.01, max_delay=0.1)`
171+
172+
**For Production:**
173+
- Use moderate `max_retries` (3-5) with reasonable delays for reliability
174+
- Example: `RetryConfig(max_retries=5, initial_delay=0.1, max_delay=60.0)`
175+
176+
**For High-Latency Networks:**
177+
- Use higher `max_retries` (5-10) with longer delays
178+
- Example: `RetryConfig(max_retries=8, initial_delay=0.5, max_delay=120.0)`
179+
180+
**For Load Balancing:**
181+
- Use `round_robin` for simple, predictable behavior
182+
- Use `least_loaded` when you need optimal performance and can handle complexity
183+
123184
Architecture
124185
------------
125186

libp2p/network/swarm.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,26 @@
6363

6464
@dataclass
6565
class RetryConfig:
66-
"""Configuration for retry logic with exponential backoff."""
66+
"""
67+
Configuration for retry logic with exponential backoff.
68+
69+
This configuration controls how connection attempts are retried when they fail.
70+
The retry mechanism uses exponential backoff with jitter to prevent thundering
71+
herd problems in distributed systems.
72+
73+
Attributes:
74+
max_retries: Maximum number of retry attempts before giving up.
75+
Default: 3 attempts
76+
initial_delay: Initial delay in seconds before the first retry.
77+
Default: 0.1 seconds (100ms)
78+
max_delay: Maximum delay cap in seconds to prevent excessive wait times.
79+
Default: 30.0 seconds
80+
backoff_multiplier: Multiplier for exponential backoff (each retry multiplies
81+
the delay by this factor). Default: 2.0 (doubles each time)
82+
jitter_factor: Random jitter factor (0.0-1.0) to add randomness to delays
83+
and prevent synchronized retries. Default: 0.1 (10% jitter)
84+
85+
"""
6786

6887
max_retries: int = 3
6988
initial_delay: float = 0.1
@@ -74,7 +93,21 @@ class RetryConfig:
7493

7594
@dataclass
7695
class ConnectionConfig:
77-
"""Configuration for multi-connection support."""
96+
"""
97+
Configuration for multi-connection support.
98+
99+
This configuration controls how multiple connections per peer are managed,
100+
including connection limits, timeouts, and load balancing strategies.
101+
102+
Attributes:
103+
max_connections_per_peer: Maximum number of connections allowed to a single
104+
peer. Default: 3 connections
105+
connection_timeout: Timeout in seconds for establishing new connections.
106+
Default: 30.0 seconds
107+
load_balancing_strategy: Strategy for distributing streams across connections.
108+
Options: "round_robin" (default) or "least_loaded"
109+
110+
"""
78111

79112
max_connections_per_peer: int = 3
80113
connection_timeout: float = 30.0

0 commit comments

Comments
 (0)