@@ -96,30 +96,91 @@ Production Configuration
96
96
97
97
For production use, consider these settings:
98
98
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
+
99
122
.. code-block :: python
100
123
101
124
from libp2p.network.swarm import ConnectionConfig, RetryConfig
102
125
103
126
# Production-ready configuration
104
127
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
110
133
)
111
134
112
135
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
116
139
)
117
140
118
141
swarm = new_swarm(
119
142
retry_config = retry_config,
120
143
connection_config = connection_config
121
144
)
122
145
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
+
123
184
Architecture
124
185
------------
125
186
0 commit comments