diff options
Diffstat (limited to 'src/swarm/hyparview.rs')
| -rw-r--r-- | src/swarm/hyparview.rs | 46 |
1 files changed, 13 insertions, 33 deletions
diff --git a/src/swarm/hyparview.rs b/src/swarm/hyparview.rs index 1f2bdc6..3bee7e8 100644 --- a/src/swarm/hyparview.rs +++ b/src/swarm/hyparview.rs @@ -10,12 +10,12 @@ //! even where that means dropping another (told so: DISCONNECT). One a neighbour short //! says so, and a member with `k` takes it as its `k + 1`th, the most it keeps: else //! a few members one short each, neighbours of each other, would ask only members with -//! `k`, be refused, and stay an island (`ONE_SHORT_EVERY`). One left with none +//! `k`, be refused, and stay an island (`Timing::one_short_every`). One left with none //! (dropped so itself) while its asks are low ones has one of them, or another, made //! high at once, even of a member that refused before. A neighbour that fails //! (judged unreachable: unanswered HELLOs, dormant, not alive lately) leaves the active //! view, and another is asked. Every neighbour is asked again now and then whether it -//! still is (`REAFFIRM_EVERY`): the paper's links are TCP connections, whose ends know +//! still is (`Timing::reaffirm_every`): the paper's links are TCP connections, whose ends know //! when the other closed them; ours, without, could be left in one end's view. //! //! The paper's passive view comes from its own messages (JOIN, FORWARD_JOIN, SHUFFLE), @@ -30,13 +30,12 @@ //! //! A member being asked counts as a neighbour already: it gets HELLOs, and NEIGHBOUR //! goes once the handshake is done (a swarm's members only take messages from peers -//! that did one). Refused, or unanswered for `ASK_FOR`, it is not asked again for -//! `REFUSED_FOR`, and stops counting. +//! that did one). Refused, or unanswered for `Timing::ask_for`, it is not asked again for +//! `Timing::refused_for`, and stops counting. use std::collections::{BTreeMap, BTreeSet}; use std::sync::Mutex; use std::sync::atomic::{AtomicU64, Ordering}; -use std::time::Duration; use async_trait::async_trait; use tokio::time::Instant; @@ -47,26 +46,6 @@ use crate::state::{AppState, HelloState}; use crate::swarm::topology::{self, Inputs, Topology}; use crate::transport::{NeighbourAnswer, NeighbourRequest, ParsedMessage}; -/// How long a member asked to be a neighbour has to agree, the handshake before -/// included: over Tor, a few round trips of up to 3 s. -const ASK_FOR: Duration = Duration::from_secs(20); - -/// A member that refused, didn't answer, or left isn't asked again for this long. -const REFUSED_FOR: Duration = Duration::from_secs(60); - -/// A member one neighbour short asks this seldom: in a swarm whose members mostly have -/// `k`, asking each in turn kept NEIGHBOURs going in a quiet swarm (up to 300 in 20 s -/// at 50 members). Two or more short, it asks at once. A member with `k` takes it (it -/// says it is one short), so one ask is enough but where those asked have `k + 1`. -const ONE_SHORT_EVERY: Duration = Duration::from_secs(300); - -/// Each neighbour is asked again this often whether it still is (NEIGHBOUR, as a new -/// ask would be): a NEIGHBOUR_REPLY or DISCONNECT lost on a lossy link leaves a link in -/// one end's view only, and nothing else would tell that end: the other still answers -/// it. A neighbour that has us agrees at once; one that doesn't takes us if it has room, -/// else refuses, and we drop it. -const REAFFIRM_EVERY: Duration = Duration::from_secs(60); - /// A member asked to be a neighbour. #[derive(Debug)] struct Asking { @@ -86,9 +65,9 @@ struct Views { asking: BTreeMap<String, Asking>, /// Refused, unanswered or left lately: since when. refused: BTreeMap<String, Instant>, - /// When we last asked while one neighbour short (`ONE_SHORT_EVERY`). + /// When we last asked while one neighbour short (`Timing::one_short_every`). asked_one_short: Option<Instant>, - /// When the neighbours were last asked whether they still are (`REAFFIRM_EVERY`). + /// When the neighbours were last asked whether they still are (`Timing::reaffirm_every`). reaffirmed: Option<Instant>, } @@ -227,7 +206,8 @@ impl Topology for HyParView { .asking .iter() .filter(|(member, asking)| { - !reachable.contains(*member) || now.duration_since(asking.since) > ASK_FOR + !reachable.contains(*member) + || now.duration_since(asking.since) > state.timing.ask_for }) .map(|(member, _)| member.clone()) .collect(); @@ -246,7 +226,7 @@ impl Topology for HyParView { } views .refused - .retain(|_, since| now.duration_since(*since) < REFUSED_FOR); + .retain(|_, since| now.duration_since(*since) < state.timing.refused_for); // More than one beyond `k` (asks sent before others asked us came back // agreed): the excess dropped, at random, and told. if views.active.len() > inputs.k + 1 { @@ -262,12 +242,12 @@ impl Topology for HyParView { let missing = inputs .k .saturating_sub(views.active.len() + views.asking.len()); - // One short, with neighbours: not again before `ONE_SHORT_EVERY`. + // One short, with neighbours: not again before `Timing::one_short_every`. let one_short = missing == 1 && !views.active.is_empty(); let waiting = one_short && views .asked_one_short - .is_some_and(|at| now.duration_since(at) < ONE_SHORT_EVERY); + .is_some_and(|at| now.duration_since(at) < state.timing.one_short_every); if missing > 0 && !waiting { if one_short { views.asked_one_short = Some(now); @@ -330,7 +310,7 @@ impl Topology for HyParView { .collect(); if views .reaffirmed - .is_none_or(|at| now.duration_since(at) >= REAFFIRM_EVERY) + .is_none_or(|at| now.duration_since(at) >= state.timing.reaffirm_every) { views.reaffirmed = Some(now); to_ask.extend(views.active.iter().map(|member| (member.clone(), false))); @@ -444,7 +424,7 @@ impl Topology for HyParView { { let mut views = self.views.lock_or_recover(); // Not asked: a neighbour asked again whether it still is - // (`REAFFIRM_EVERY`), dropped if it refused; or one we gave up on, + // (`Timing::reaffirm_every`), dropped if it refused; or one we gave up on, // and told so (`maintain`), which undoes its agreeing. if views.asking.remove(peer).is_none() { if answer.agreed || !views.active.remove(peer) { |
