diff options
Diffstat (limited to 'src/state.rs')
| -rw-r--r-- | src/state.rs | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/state.rs b/src/state.rs index aae33f5..dfabf8f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -144,6 +144,34 @@ pub(crate) struct PeerState { /// dormant). Cleared by any message from it; ignored after `dormant_probe`, so the /// peer becomes a neighbour candidate again and gets tried once more. pub(crate) unreachable_since: Option<Instant>, + + /// What went wrong with this peer so far (counted only, for /peers and /admin/stats). + pub(crate) incidents: Incidents, +} + +/// Trouble with a peer, counted since it was first listed. Lost keepalives and refused +/// sends are in the traffic counters (`transport::traffic`): every PONG answers a PING. +#[derive(Debug, Default, Clone, Copy, serde::Serialize, utoipa::ToSchema)] +#[serde(rename_all = "camelCase")] +pub(crate) struct Incidents { + /// Our sync messages left unanswered until the reply timeout (then sent again). + pub(crate) reply_timeouts: u64, + /// Re-handshakes because our documents differed while sync was idle. + pub(crate) heals: u64, + /// Times the peer went dormant. + pub(crate) dormancies: u64, + /// Time spent dormant, in milliseconds, up to its last return (the current + /// dormancy, if any, isn't included). + pub(crate) dormant_ms: u64, +} + +impl Incidents { + pub(crate) fn add(&mut self, other: &Incidents) { + self.reply_timeouts += other.reply_timeouts; + self.heals += other.heals; + self.dormancies += other.dormancies; + self.dormant_ms += other.dormant_ms; + } } /// Unanswered HELLOs after which a peer counts as unreachable (1 + 2 + 4 s apart). @@ -296,6 +324,7 @@ impl PeerState { reported_heads: None, hello_attempts: 0, unreachable_since: None, + incidents: Incidents::default(), } } @@ -311,7 +340,11 @@ impl PeerState { if self.liveness != Liveness::Active || self.hello_state != HelloState::Established { return None; } - self.sync.take_overdue_reply(now) + let overdue = self.sync.take_overdue_reply(now); + if overdue.is_some() { + self.incidents.reply_timeouts += 1; + } + overdue } fn quiet_for(&self, now: Instant) -> Duration { @@ -324,7 +357,8 @@ impl PeerState { self.hello_attempts = 0; self.unreachable_since = None; - if matches!(self.liveness, Liveness::Dormant { .. }) { + if let Liveness::Dormant { since } = self.liveness { + self.incidents.dormant_ms += now.saturating_duration_since(since).as_millis() as u64; self.liveness = Liveness::Active; // Force a fresh handshake; its reply resets sync on both sides. if self.hello_state != HelloState::Sending { @@ -365,6 +399,7 @@ impl PeerState { } self.liveness = Liveness::Dormant { since: now }; + self.incidents.dormancies += 1; self.unreachable_since.get_or_insert(now); self.reset_sync(); @@ -446,6 +481,7 @@ impl PeerState { return false; } self.diverged_since = None; + self.incidents.heals += 1; self.last_resync_at = Some(now); self.hello_state = HelloState::NotSent; // establish_peer resets sync on the reply |
