Skip to main content

bb_runtime/framework/
peer_state.rs

1//! `PeerState` — the framework's consolidated per-peer state.
2//!
3//! The engine's four peer-related primitives — `PeerGate` (named
4//! concurrency limiter), `PeerGovernor` (policy + health source of
5//! truth), `BackoffTable` (per-peer exponential backoff), and
6//! `BackpressureTracker` (receiver-side overload state for the
7//! backpressure protocol per
8//! `docs/internal/superpowers/specs/2026-06-23-backpressure-runtime.md`)
9//! — cluster under one struct. Component authors reach them through
10//! `ctx.peer_state.{gate, governor, backoff, backpressure}` instead
11//! of four sibling fields on the framework bundle.
12
13use crate::framework::{BackoffTable, BackpressureTracker, PeerGate, PeerGovernor};
14
15/// Consolidated per-peer state.
16///
17/// Each sub-field retains its existing API; the consolidation is
18/// purely organisational so `FrameworkComponents` carries one peer
19/// field instead of four. Future work can merge the sub-fields
20/// into a single per-peer entry table.
21pub struct PeerState {
22    /// Named concurrency limiter consulted by `Limit.Acquire` /
23    /// `Limit.Release` syscalls.
24    pub gate: PeerGate,
25    /// Single source of truth for peer policy (blocklist /
26    /// allowlist) + per-peer health tracking. Consulted by inbound
27    /// + outbound peer-health gates.
28    pub governor: PeerGovernor,
29    /// Per-peer exponential backoff schedule. Consulted by the
30    /// outbound governor at `check_outbound` time.
31    pub backoff: BackoffTable,
32    /// Receiver-side per-peer back-pressure state. Tracks notices
33    /// emitted to each sender, the duplicate-suppression window,
34    /// and the K-then-silent fallback. Consulted at Phase 1 of the
35    /// engine poll cycle when ingress overload is detected.
36    pub backpressure: BackpressureTracker,
37}
38
39impl Default for PeerState {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45impl PeerState {
46    /// Construct a fresh consolidated peer-state bundle.
47    pub fn new() -> Self {
48        Self {
49            gate: PeerGate::new(),
50            governor: PeerGovernor::new(),
51            backoff: BackoffTable::new(),
52            backpressure: BackpressureTracker::new(),
53        }
54    }
55}