@@ -7,7 +7,10 @@ use graph::components::network_provider::ProviderName;
7
7
use graph:: endpoint:: EndpointMetrics ;
8
8
use graph:: firehose:: { AvailableCapacity , SubgraphLimit } ;
9
9
use graph:: prelude:: rand:: {
10
- self , distr:: { weighted:: WeightedIndex , Distribution } , seq:: IteratorRandom , Rng
10
+ self ,
11
+ distr:: { weighted:: WeightedIndex , Distribution } ,
12
+ seq:: IteratorRandom ,
13
+ Rng ,
11
14
} ;
12
15
use itertools:: Itertools ;
13
16
use std:: sync:: Arc ;
@@ -199,11 +202,11 @@ impl EthereumNetworkAdapters {
199
202
200
203
/// Main adapter selection entry point that handles both weight-based distribution
201
204
/// and error retesting logic.
202
- ///
205
+ ///
203
206
/// The selection process:
204
207
/// 1. First selects an adapter based on weights (if enabled) or random selection
205
208
/// 2. Occasionally overrides the selection to retest adapters with errors
206
- ///
209
+ ///
207
210
/// The error retesting happens AFTER weight-based selection to minimize
208
211
/// distribution skew while still allowing periodic health checks of errored endpoints.
209
212
fn cheapest_from (
@@ -213,7 +216,7 @@ impl EthereumNetworkAdapters {
213
216
) -> Result < Arc < EthereumAdapter > , Error > {
214
217
// Select adapter based on weights or random strategy
215
218
let selected_adapter = self . select_best_adapter ( input. clone ( ) , required_capabilities) ?;
216
-
219
+
217
220
// Occasionally override selection to retest errored adapters
218
221
// This happens AFTER weight-based selection to minimize distribution skew
219
222
let retest_rng: f64 = rand:: rng ( ) . random ( ) ;
@@ -227,11 +230,10 @@ impl EthereumNetworkAdapters {
227
230
return Ok ( most_errored. adapter . clone ( ) ) ;
228
231
}
229
232
}
230
-
233
+
231
234
Ok ( selected_adapter)
232
235
}
233
236
234
-
235
237
/// Selects the best adapter based on the configured strategy (weighted or random).
236
238
/// If weighted mode is enabled, uses weight-based probabilistic selection.
237
239
/// Otherwise, falls back to random selection with error count consideration.
@@ -248,11 +250,11 @@ impl EthereumNetworkAdapters {
248
250
}
249
251
250
252
/// Performs weighted random selection of adapters based on their configured weights.
251
- ///
253
+ ///
252
254
/// Weights are relative values between 0.0 and 1.0 that determine the probability
253
255
/// of selecting each adapter. They don't need to sum to 1.0 as they're normalized
254
256
/// internally by the WeightedIndex distribution.
255
- ///
257
+ ///
256
258
/// Falls back to random selection if weights are invalid (e.g., all zeros).
257
259
fn select_weighted_adapter (
258
260
& self ,
@@ -276,7 +278,7 @@ impl EthereumNetworkAdapters {
276
278
}
277
279
278
280
/// Performs random selection of adapters with preference for those with fewer errors.
279
- ///
281
+ ///
280
282
/// Randomly selects up to 3 adapters from the available pool, then chooses the one
281
283
/// with the lowest error count. This provides a balance between load distribution
282
284
/// and avoiding problematic endpoints.
@@ -285,9 +287,7 @@ impl EthereumNetworkAdapters {
285
287
input : Vec < & EthereumNetworkAdapter > ,
286
288
required_capabilities : & NodeCapabilities ,
287
289
) -> Result < Arc < EthereumAdapter > , Error > {
288
- let choices = input
289
- . into_iter ( )
290
- . choose_multiple ( & mut rand:: rng ( ) , 3 ) ;
290
+ let choices = input. into_iter ( ) . choose_multiple ( & mut rand:: rng ( ) , 3 ) ;
291
291
if let Some ( adapter) = choices. iter ( ) . min_by_key ( |a| a. current_error_count ( ) ) {
292
292
Ok ( adapter. adapter . clone ( ) )
293
293
} else {
@@ -390,10 +390,7 @@ mod tests {
390
390
use graph:: http:: HeaderMap ;
391
391
use graph:: slog:: { o, Discard , Logger } ;
392
392
use graph:: {
393
- endpoint:: EndpointMetrics ,
394
- firehose:: SubgraphLimit ,
395
- prelude:: MetricsRegistry ,
396
- tokio,
393
+ endpoint:: EndpointMetrics , firehose:: SubgraphLimit , prelude:: MetricsRegistry , tokio,
397
394
url:: Url ,
398
395
} ;
399
396
use std:: sync:: Arc ;
0 commit comments