aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/transport/paths.rs101
1 files changed, 91 insertions, 10 deletions
diff --git a/src/transport/paths.rs b/src/transport/paths.rs
index e2d9ec9..f7a146f 100644
--- a/src/transport/paths.rs
+++ b/src/transport/paths.rs
@@ -109,7 +109,7 @@ const DOWN_FOR: Duration = Duration::from_secs(60);
const PROBE_EVERY: Duration = Duration::from_secs(30);
const PROBE_TIMEOUT: Duration = Duration::from_secs(15);
const UNANSWERED_DOWN: u32 = 3;
-/// How often the probe task looks for paths due.
+/// The least time between two looks of the probe task for paths due.
const PROBE_TICK: Duration = Duration::from_secs(5);
/// Paths this node had a choice of, or sent over, within this long are probed; ones it
/// didn't use for twice as long are forgotten.
@@ -198,6 +198,29 @@ fn rested(path: &PathState, now: Instant, every: Duration) -> bool {
}
impl PathState {
+ /// Whether it was chosen among others, or sent over, within `ACTIVE_FOR`: such a
+ /// path is probed.
+ fn active(&self, now: Instant) -> bool {
+ let lately = |at: Option<Instant>| {
+ at.is_some_and(|at| now.saturating_duration_since(at) < ACTIVE_FOR)
+ };
+ lately(self.used) || lately(self.carried)
+ }
+
+ /// From when it may be pinged again (`rested`), pinged every `every`; none where it
+ /// was never pinged: at once.
+ fn rested_at(&self, every: Duration) -> Option<Instant> {
+ match (self.last_probe, self.peer_probe) {
+ (None, Some(theirs)) => Some(theirs + every),
+ (None, None) => None,
+ (Some(ours), Some(theirs)) if theirs > ours => Some(theirs + every),
+ (Some(ours), Some(theirs)) if ours.saturating_duration_since(theirs) < every * 2 => {
+ Some(ours + every + every / 2)
+ }
+ (Some(ours), _) => Some(ours + every),
+ }
+ }
+
fn is_down(&self, now: Instant) -> bool {
self.unanswered >= UNANSWERED_DOWN
|| self
@@ -218,6 +241,9 @@ struct Table {
/// long interval is chosen to spare. A busy pair, whose keepalives don't go, still
/// gets its paths measured this often.
probe_every: Duration,
+ /// Wakes the task that pings (`Paths::probe`): a path came into use, so one may be
+ /// due before the task looks next.
+ wake: Arc<tokio::sync::Notify>,
}
impl Default for Table {
@@ -233,6 +259,7 @@ impl Table {
chosen: HashMap::new(),
counts: PathCounts::default(),
probe_every: PROBE_EVERY.max(keepalive),
+ wake: Arc::default(),
}
}
@@ -257,7 +284,12 @@ impl Table {
}
for (address, _) in &usable {
if self.paths.len() < MAX_PATHS || self.paths.contains_key(address) {
- self.paths.entry(address.clone()).or_default().used = Some(now);
+ let path = self.paths.entry(address.clone()).or_default();
+ let idle = !path.active(now);
+ path.used = Some(now);
+ if idle {
+ self.wake.notify_one();
+ }
}
}
}
@@ -338,6 +370,8 @@ impl Table {
/// A send to `address` failed.
fn failed(&mut self, address: &str, now: Instant) {
+ // What is due next may be sooner now (`next_look`).
+ self.wake.notify_one();
self.counts.send_failures += 1;
if let Some(path) = self.paths.get_mut(address) {
path.failed_at = Some(now);
@@ -356,7 +390,11 @@ impl Table {
}
let path = self.paths.entry(address.to_owned()).or_default();
path.failed_at = None;
+ let idle = !path.active(now);
path.carried = Some(now);
+ if idle {
+ self.wake.notify_one();
+ }
}
/// The PINGs due now, with their nonces (from `nonce`): to every path chosen among
@@ -377,10 +415,7 @@ impl Table {
info!(address = %address, "A path stopped answering PINGs; using others");
}
}
- let lately = |at: Option<Instant>| {
- at.is_some_and(|at| now.saturating_duration_since(at) < ACTIVE_FOR)
- };
- let active = lately(path.used) || lately(path.carried);
+ let active = path.active(now);
let rested = rested(path, now, self.probe_every);
if active && rested && path.probe.is_none() {
let nonce = nonce();
@@ -393,6 +428,30 @@ impl Table {
due
}
+ /// When `due` may next have something to do, as the paths stand at `now`: a PING
+ /// out counts as unanswered, a path in use is rested, or one unused for long is
+ /// forgotten. None while it holds no path.
+ fn next_look(&self, now: Instant) -> Option<Instant> {
+ let every = self.probe_every;
+ self.paths
+ .values()
+ .map(|path| {
+ if let Some((_, sent)) = path.probe {
+ return sent + PROBE_TIMEOUT;
+ }
+ if path.active(now) {
+ return path.rested_at(every).unwrap_or(now);
+ }
+ // Forgotten once unused for twice as long.
+ [path.used, path.carried, path.last_probe]
+ .into_iter()
+ .flatten()
+ .max()
+ .map_or(now, |last| last + ACTIVE_FOR * 2)
+ })
+ .min()
+ }
+
/// What went to the peer at `addresses` went unanswered: the first, the one in use,
/// is suspect (`PathState::suspect`), and each without a PING out gets one now.
/// Returns those PINGs, with their nonces (from `nonce`).
@@ -402,6 +461,8 @@ impl Table {
now: Instant,
mut nonce: impl FnMut() -> u64,
) -> Vec<(String, u64)> {
+ // What is due next may be sooner now (`next_look`).
+ self.wake.notify_one();
if let Some(first) = addresses.first()
&& addresses.len() > 1
&& let Some(path) = self.paths.get_mut(first)
@@ -431,6 +492,8 @@ impl Table {
/// path, which its PONG measures, even if it is a peer's only path; `due` then
/// doesn't ping it again within `probe_every`.
fn pinged(&mut self, address: &str, nonce: u64, now: Instant) {
+ // What is due next may be sooner now (`next_look`).
+ self.wake.notify_one();
if self.paths.len() >= MAX_PATHS && !self.paths.contains_key(address) {
self.forget_unused(now);
if self.paths.len() >= MAX_PATHS {
@@ -454,6 +517,8 @@ impl Table {
/// The PING with `nonce` to `address` (from `due`) goes now: its round trip counts
/// from here, not from when it was found due.
fn sending(&mut self, address: &str, nonce: u64, now: Instant) {
+ // What is due next may be sooner now (`next_look`).
+ self.wake.notify_one();
if let Some(path) = self.paths.get_mut(address)
&& let Some((expected, sent)) = &mut path.probe
&& *expected == nonce
@@ -1015,12 +1080,28 @@ impl Paths {
sent
}
- /// Pings the paths due (`Table::due`), every `PROBE_TICK`, until dropped.
+ /// Pings the paths due (`Table::due`), when the next is (`Table::next_look`), and no
+ /// more often than every `PROBE_TICK`, so that paths due close together go
+ /// together; until dropped. What changes when the next is due (a path coming into
+ /// use, a PING sent otherwise) has it look that up anew. With no path, it
+ /// waits for one.
async fn probe(self: Arc<Self>) {
- let mut tick = tokio::time::interval(PROBE_TICK);
- tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
+ let wake = Arc::clone(&self.table.lock_or_recover().wake);
+ let mut looked = Instant::now();
loop {
- tick.tick().await;
+ let next = self.table.lock_or_recover().next_look(Instant::now());
+ match next {
+ Some(at) => tokio::select! {
+ () = tokio::time::sleep_until(at.max(looked + PROBE_TICK)) => {}
+ // Something changed what is due next: looked up anew.
+ () = wake.notified() => continue,
+ },
+ None => {
+ wake.notified().await;
+ continue;
+ }
+ }
+ looked = Instant::now();
let due = self
.table
.lock_or_recover()