11mod isotonic_estimator;
22mod util;
33
4- use crate :: ring:: { Location , PeerKeyLocation } ;
4+ use crate :: ring:: { Distance , Location , PeerKeyLocation } ;
55use isotonic_estimator:: { EstimatorType , IsotonicEstimator , IsotonicEvent } ;
66use serde:: { Deserialize , Serialize } ;
77use std:: time:: Duration ;
@@ -20,6 +20,19 @@ pub(crate) struct Router {
2020}
2121
2222impl 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