Skip to content

Commit e243c8d

Browse files
sanityiduartgomez
authored andcommitted
fix(router): handle peers without location
1 parent e8d6d57 commit e243c8d

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

crates/core/src/router/mod.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod isotonic_estimator;
22
mod util;
33

4-
use crate::ring::{Location, PeerKeyLocation};
4+
use crate::ring::{Distance, Location, PeerKeyLocation};
55
use isotonic_estimator::{EstimatorType, IsotonicEstimator, IsotonicEvent};
66
use serde::{Deserialize, Serialize};
77
use std::time::Duration;
@@ -20,6 +20,19 @@ pub(crate) struct Router {
2020
}
2121

2222
impl Router {
23+
/// Some code paths (bootstrap, tests) hand Router peer entries before the
24+
/// remote has published a location. Treat them as midway around the ring so
25+
/// we still consider them instead of dropping the candidate set entirely.
26+
#[inline]
27+
fn peer_distance_or_default(
28+
peer: &PeerKeyLocation,
29+
target_location: &Location,
30+
) -> Distance {
31+
peer.location
32+
.map(|loc| target_location.distance(loc))
33+
.unwrap_or_else(|| Distance::new(0.5))
34+
}
35+
2336
pub fn new(history: &[RouteEvent]) -> Self {
2437
let failure_outcomes: Vec<IsotonicEvent> = history
2538
.iter()
@@ -162,9 +175,9 @@ impl Router {
162175

163176
let mut peer_distances: Vec<_> = peers
164177
.into_iter()
165-
.filter_map(|peer| {
166-
peer.location
167-
.map(|loc| (peer, target_location.distance(loc)))
178+
.map(|peer| {
179+
let distance = Self::peer_distance_or_default(peer, target_location);
180+
(peer, distance)
168181
})
169182
.collect();
170183

@@ -202,9 +215,9 @@ impl Router {
202215

203216
let mut peer_distances: Vec<_> = peers
204217
.into_iter()
205-
.filter_map(|peer| {
206-
peer.location
207-
.map(|loc| (peer, target_location.distance(loc)))
218+
.map(|peer| {
219+
let distance = Self::peer_distance_or_default(peer, &target_location);
220+
(peer, distance)
208221
})
209222
.collect();
210223

0 commit comments

Comments
 (0)