| 1 | // Copyright 2026 Deno Land Inc. Apache-2.0 license. |
| 2 | |
| 3 | //! Memory-pressure shedding as a pure classifier of an RSS sample plus the |
| 4 | //! prior shedding state (the hysteresis latch). No I/O, no clock — the env |
| 5 | //! read that builds the config stays at the edge (`main.rs`). |
| 6 | //! |
| 7 | //! Residency is deliberately *not* here. A node's cell count is a hard cap |
| 8 | //! enforced at admission ([`crate::State::has_capacity`]), self-limiting and |
| 9 | //! known exactly; it is not a resource that needs a proactive walk down. This |
| 10 | //! classifier answers only the other question — "is this node out of memory |
| 11 | //! and must give cells back to recover?" — which a cell count cannot answer. |
| 12 | //! Conflating the two produced the placement churn and the admission wedge; |
| 13 | //! splitting them is what keeps each decision small. |
| 14 | |
| 15 | /// Resource watermarks. Built once from the environment by the caller; the |
| 16 | /// core never reads the environment itself. Residency has no watermark here — |
| 17 | /// it is capped at admission. |
| 18 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] |
| 19 | pub struct PressureConfig { |
| 20 | pub rss_high_bytes: Option<u64>, |
| 21 | } |
| 22 | |
| 23 | /// A resource sample — the only input the classifier reads. `resident_cells` |
| 24 | /// is carried so a resource trigger can size its walk down as a proportion of |
| 25 | /// what is actually resident. |
| 26 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 27 | pub struct Load { |
| 28 | pub resident_cells: usize, |
| 29 | pub rss_bytes: u64, |
| 30 | } |
| 31 | |
| 32 | impl PressureConfig { |
| 33 | /// Is the RSS sample over its ceiling, or is a prior crossing still above |
| 34 | /// the low watermark (80% of high)? |
| 35 | pub fn shedding(self, s: Load, was_shedding: bool) -> bool { |
| 36 | self.rss_high_bytes.is_some_and(|h| { |
| 37 | s.rss_bytes >= h || (was_shedding && s.rss_bytes > h.saturating_mul(4) / 5) |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | /// How far to shed down to. A memory trigger takes a proportion of what was |
| 42 | /// just measured because the effect of an eviction is not visible until |
| 43 | /// the next RSS sample. |
| 44 | pub fn release_target(resident_cells: usize) -> usize { |
| 45 | resident_cells.saturating_sub((resident_cells / 10).max(1)) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub const MAX_OUTBOUND_PIN_PERCENT: usize = 50; |
| 50 | |
| 51 | /// May another cell be pinned resident by an outbound WebSocket? |
| 52 | /// |
| 53 | /// An outbound socket is not hibernatable, so eviction refuses its cell for as |
| 54 | /// long as it is open. That is correct — a live host transport cannot survive |
| 55 | /// eviction — but it means every pinned cell is removed from the eviction |
| 56 | /// pool. Pin the whole ceiling and a resource walk down has nothing to |
| 57 | /// nominate. The budget is node-wide, counted in pinned *cells* rather than |
| 58 | /// sockets, because one socket is enough to pin. `ceiling` is the hard resident |
| 59 | /// cap. |
| 60 | pub fn may_pin_outbound(pinned_cells: usize, ceiling: Option<usize>) -> bool { |
| 61 | ceiling.is_none_or(|cap| { |
| 62 | // At least one, always. The share alone rounds to zero below a ceiling |
| 63 | // of two, which would make an outbound socket impossible on a small |
| 64 | // node rather than merely budgeted. |
| 65 | pinned_cells < (cap.saturating_mul(MAX_OUTBOUND_PIN_PERCENT) / 100).max(1) |
| 66 | }) |
| 67 | } |