1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Who is connected to the serve side right now, and how.
//!
//! Split from [`crate::listener`] when the per-peer view arrived: the
//! accept loop is about turning QUIC streams into exchanges, and the set of
//! peers it is currently serving is a different thing — one that can be
//! read without an endpoint, tested without a socket, and reported to an
//! embedder as a list rather than only as the aggregate
//! [`PipeStatus`](crate::PipeStatus) it collapses to.
//!
//! Pure: a map behind a `std` mutex, never held across an await. The
//! status it publishes goes through the lifecycle it is handed, so this
//! module owns the *set* and nothing about how a change is broadcast.
use std::collections::{BTreeMap, HashMap};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use tokio::sync::Semaphore;
use crate::lifecycle::{Lifecycle, PeerPath, aggregate};
use crate::path_watch::{self, Reading};
use crate::status::PeerView;
/// How many exchanges one peer may have in flight at once, across every
/// connection it holds.
///
/// Backpressure rather than refusal: a peer may open more streams, and they
/// wait. What is bounded is the work and the memory a single ticket-holder
/// can command, which — with the head size and the head timeout — is the
/// whole of what a leaked ticket is worth before it authenticates.
///
/// Per *peer*, not per connection, and the difference is the bound. The
/// semaphore used to be built inside the connection loop, so a holder who
/// opened N connections had 64·N streams — the cap the docs promised was
/// off by whatever the peer chose. It now lives here, keyed by the peer's
/// identity, and every connection from one endpoint draws on one budget.
///
/// Deliberately generous. A client pipelining a page of requests is normal;
/// a client with sixty-four in flight is not a client.
pub(crate) const MAX_CONCURRENT_STREAMS_PER_PEER: usize = 64;
/// One peer's stream budget, and how many connections are drawing on it.
struct Budget {
slots: Arc<Semaphore>,
connections: usize,
}
/// The connected peers, keyed by an id that exists only to name the right
/// entry when one changes path or goes.
pub(crate) struct PeerRegistry {
peers: Mutex<BTreeMap<u64, (Arc<str>, Reading)>>,
/// Stream budgets by peer identity, shared across that peer's
/// connections and dropped when its last one goes.
budgets: Mutex<HashMap<Arc<str>, Budget>>,
next: AtomicU64,
}
impl PeerRegistry {
pub(crate) fn new() -> Self {
Self {
peers: Mutex::new(BTreeMap::new()),
budgets: Mutex::new(HashMap::new()),
next: AtomicU64::new(0),
}
}
/// The stream budget for `name`, shared with every other connection
/// that peer currently holds. Call once per connection, after
/// [`add`](Self::add); [`remove`](Self::remove) returns the share.
pub(crate) fn slots(&self, name: &Arc<str>) -> Arc<Semaphore> {
let mut budgets = self.lock_budgets();
let budget = budgets.entry(name.clone()).or_insert_with(|| Budget {
slots: Arc::new(Semaphore::new(MAX_CONCURRENT_STREAMS_PER_PEER)),
connections: 0,
});
budget.connections += 1;
let slots = budget.slots.clone();
drop(budgets);
slots
}
/// Record a peer and republish the aggregate status.
///
/// `name` is the fingerprint the listener derived for the connection,
/// which is what [`views`](Self::views) reports and what the `peer`
/// log field and the `X-Modelpipe-Peer` header already carry — one
/// rule, so a device is named identically everywhere it appears.
///
/// `reading` is how that connection is routed *at this instant*, and is
/// routinely not how it will be routed a second later — see
/// [`set_path`](Self::set_path), which is the other half of this.
pub(crate) fn add(&self, name: Arc<str>, reading: Reading, lifecycle: &Lifecycle) -> u64 {
let id = self.next.fetch_add(1, Ordering::Relaxed);
self.mutate(lifecycle, |peers| {
peers.insert(id, (name, reading));
});
id
}
/// Record what one peer's path has become, and republish what the set
/// now means.
///
/// The write [`crate::path_watch`] makes on the serve side, keyed by the
/// `id` [`add`](Self::add) returned. Everything in this registry used to
/// be written once at accept and never again, which is exactly why a
/// connection that hole-punched after establishing went on being
/// reported as relayed for the rest of its life.
///
/// A peer that has already left is not resurrected: a watcher may still
/// be a tick behind [`remove`](Self::remove), and re-inserting the entry
/// it just removed would leave a departed device in `peers()` for ever.
pub(crate) fn set_path(&self, id: u64, reading: Reading, lifecycle: &Lifecycle) {
self.mutate(lifecycle, |peers| {
if let Some((_, held)) = peers.get_mut(&id) {
*held = reading;
}
});
}
pub(crate) fn remove(&self, id: u64, lifecycle: &Lifecycle) {
let mut departed = None;
self.mutate(lifecycle, |peers| {
departed = peers.remove(&id).map(|(name, _)| name);
});
if let Some(name) = departed {
self.release(&name);
}
}
/// Give back one connection's share of a peer's budget, dropping the
/// budget with its last connection so an endpoint that paired once and
/// left does not hold a semaphore for the life of the listener.
fn release(&self, name: &Arc<str>) {
let mut budgets = self.lock_budgets();
if let Some(budget) = budgets.get_mut(name) {
budget.connections = budget.connections.saturating_sub(1);
if budget.connections == 0 {
budgets.remove(name);
}
}
}
/// A snapshot of every connected peer, in the order they connected.
///
/// A snapshot and nothing more: a peer may leave between the return
/// and the read, and a status is honest about the moment it was taken.
pub(crate) fn views(&self) -> Vec<PeerView> {
self.lock()
.values()
.map(|(name, reading)| PeerView {
fingerprint: name.to_string(),
path: aggregate(&[reading.path]),
rtt_ms: reading.rtt.map(path_watch::millis),
})
.collect()
}
/// Mutate the peer set and publish what it now means.
///
/// The lock is never held across an await — the closure is synchronous
/// and the status is computed inside it — so a slow peer cannot stall
/// another's accept.
fn mutate(
&self,
lifecycle: &Lifecycle,
f: impl FnOnce(&mut BTreeMap<u64, (Arc<str>, Reading)>),
) {
let mut guard = self.lock();
f(&mut guard);
let paths: Vec<PeerPath> = guard.values().map(|(_, reading)| reading.path).collect();
// Released before publishing, so nothing observes the status while
// the set it describes is still locked.
drop(guard);
lifecycle.set_status(aggregate(&paths));
}
// A poisoned lock cannot happen here: nothing panics while holding it.
fn lock(&self) -> std::sync::MutexGuard<'_, BTreeMap<u64, (Arc<str>, Reading)>> {
self.peers
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn lock_budgets(&self) -> std::sync::MutexGuard<'_, HashMap<Arc<str>, Budget>> {
self.budgets
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
}
#[cfg(test)]
#[path = "peers_tests.rs"]
mod peers_tests;