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
use super::{ConnectionRuntime, LimitsConfig, Ordering, ServerError};
impl ConnectionRuntime {
/// Atomically reserves one §5 `max_connections` admission slot: a CAS loop
/// against the configured limit, so N concurrent callers racing for the last
/// slot admit EXACTLY one — the bound cannot be transiently exceeded.
///
/// # Errors
/// Returns [`ServerError::ConnectionLimitReached`] when every slot is taken.
pub(super) fn try_reserve_admission(&self) -> Result<(), ServerError> {
self.ensure_participant_service_live()?;
let limit = self.limits.max_connections as u64;
let mut current = self.admissions.load(Ordering::Acquire);
loop {
if current >= limit {
return Err(ServerError::ConnectionLimitReached {
limit: self.limits.max_connections,
});
}
match self.admissions.compare_exchange_weak(
current,
current + 1,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => return Ok(()),
Err(observed) => current = observed,
}
}
}
/// Releases one admission slot. Called by the spawn failure paths (via the
/// [`AdmissionReservation`] guard) and by [`Self::remove`] when a registered
/// record is removed — exactly one release per reservation. Saturating so a
/// spurious release can never wrap the counter.
pub(super) fn release_admission(&self) {
let mut current = self.admissions.load(Ordering::Acquire);
loop {
let next = current.saturating_sub(1);
match self.admissions.compare_exchange_weak(
current,
next,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => return,
Err(observed) => current = observed,
}
}
}
/// The operational caps (§5) this runtime enforces.
pub(in crate::server::connection) const fn limits(&self) -> &LimitsConfig {
&self.limits
}
}