| 1 | // Copyright 2026 Deno Land Inc. Apache-2.0 license. |
| 2 | |
| 3 | //! `PressureConfig::state` reified sans-IO: resource-pressure shedding as a |
| 4 | //! pure classifier of a sample plus prior shedding state (the hysteresis |
| 5 | //! latch). No I/O, no clock — the env read that builds the config stays at the |
| 6 | //! edge (`main.rs`). |
| 7 | //! |
| 8 | //! Residency is deliberately *not* here. A node's cell count is a hard cap |
| 9 | //! enforced at admission ([`crate::State::has_capacity`]), self-limiting and |
| 10 | //! known exactly; it is not a resource that needs a proactive walk down. This |
| 11 | //! classifier answers only the other question — "is this node out of memory or |
| 12 | //! CPU, and must give cells back to recover?" — which a cell count cannot |
| 13 | //! answer. Conflating the two produced the placement churn and the admission |
| 14 | //! wedge; splitting them is what keeps each decision small. |
| 15 | |
| 16 | /// Resource watermarks. Built once from the environment by the caller; the |
| 17 | /// core never reads the environment itself. Residency has no watermark here — |
| 18 | /// it is capped at admission. |
| 19 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] |
| 20 | pub struct PressureConfig { |
| 21 | pub rss_high_bytes: Option<u64>, |
| 22 | pub cpu_high_x100: Option<u64>, |
| 23 | } |
| 24 | |
| 25 | /// A resource sample — the only input the classifier reads. `resident_cells` |
| 26 | /// is carried so a resource trigger can size its walk down as a proportion of |
| 27 | /// what is actually resident. |
| 28 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 29 | pub struct Load { |
| 30 | pub resident_cells: usize, |
| 31 | pub rss_bytes: u64, |
| 32 | pub cpu_percent_x100: u64, |
| 33 | } |
| 34 | |
| 35 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 36 | pub struct PressureState { |
| 37 | pub shedding: bool, |
| 38 | pub trigger: Option<&'static str>, |
| 39 | } |
| 40 | |
| 41 | impl PressureConfig { |
| 42 | /// The first resource ceiling the sample crosses, or `None`. |
| 43 | pub fn trigger(self, s: Load) -> Option<&'static str> { |
| 44 | if self.rss_high_bytes.is_some_and(|h| s.rss_bytes >= h) { |
| 45 | return Some("rss"); |
| 46 | } |
| 47 | if self.cpu_high_x100.is_some_and(|h| s.cpu_percent_x100 >= h) { |
| 48 | return Some("cpu"); |
| 49 | } |
| 50 | None |
| 51 | } |
| 52 | |
| 53 | /// Back under every low watermark (80% of each high) — the latch release. |
| 54 | pub fn relieved(self, s: Load) -> bool { |
| 55 | let rss_ok = self |
| 56 | .rss_high_bytes |
| 57 | .is_none_or(|h| s.rss_bytes <= h.saturating_mul(4) / 5); |
| 58 | let cpu_ok = self |
| 59 | .cpu_high_x100 |
| 60 | .is_none_or(|h| s.cpu_percent_x100 <= h.saturating_mul(4) / 5); |
| 61 | rss_ok && cpu_ok |
| 62 | } |
| 63 | |
| 64 | /// How far to shed down to. A resource trigger takes a proportion of what |
| 65 | /// was just measured, because the effect of an eviction on RSS or CPU is |
| 66 | /// not visible until the next sample. |
| 67 | pub fn release_target(self, resident_cells: usize) -> usize { |
| 68 | resident_cells.saturating_sub((resident_cells / 10).max(1)) |
| 69 | } |
| 70 | |
| 71 | /// The resource whose low watermark still holds the latch. An instantaneous |
| 72 | /// trigger wins; inside a hysteresis band, preserve the resource-specific |
| 73 | /// reason until every configured low watermark clears. |
| 74 | pub fn shedding_trigger(self, s: Load, was_shedding: bool) -> Option<&'static str> { |
| 75 | if let Some(trigger) = self.trigger(s) { |
| 76 | return Some(trigger); |
| 77 | } |
| 78 | if !was_shedding || self.relieved(s) { |
| 79 | return None; |
| 80 | } |
| 81 | if self |
| 82 | .rss_high_bytes |
| 83 | .is_some_and(|h| s.rss_bytes > h.saturating_mul(4) / 5) |
| 84 | { |
| 85 | return Some("rss"); |
| 86 | } |
| 87 | if self |
| 88 | .cpu_high_x100 |
| 89 | .is_some_and(|h| s.cpu_percent_x100 > h.saturating_mul(4) / 5) |
| 90 | { |
| 91 | return Some("cpu"); |
| 92 | } |
| 93 | None |
| 94 | } |
| 95 | |
| 96 | /// The transition: `was_shedding` is the only carried state — the latch |
| 97 | /// that keeps shedding between the high crossing and the low release. |
| 98 | pub fn state(self, s: Load, was_shedding: bool) -> PressureState { |
| 99 | PressureState { |
| 100 | shedding: self.shedding_trigger(s, was_shedding).is_some(), |
| 101 | trigger: self.trigger(s), |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | pub const MAX_OUTBOUND_PIN_PERCENT: usize = 50; |
| 107 | |
| 108 | /// May another cell be pinned resident by an outbound WebSocket? |
| 109 | /// |
| 110 | /// An outbound socket is not hibernatable, so eviction refuses its cell for as |
| 111 | /// long as it is open. That is correct — a live host transport cannot survive |
| 112 | /// hibernation — but it means every pinned cell is removed from the eviction |
| 113 | /// pool. Pin the whole ceiling and a resource walk down has nothing to |
| 114 | /// nominate. The budget is node-wide, counted in pinned *cells* rather than |
| 115 | /// sockets, because one socket is enough to pin. `ceiling` is the hard resident |
| 116 | /// cap. |
| 117 | pub fn may_pin_outbound(pinned_cells: usize, ceiling: Option<usize>) -> bool { |
| 118 | ceiling.is_none_or(|cap| { |
| 119 | // At least one, always. The share alone rounds to zero below a ceiling |
| 120 | // of two, which would make an outbound socket impossible on a small |
| 121 | // node rather than merely budgeted. |
| 122 | pinned_cells < (cap.saturating_mul(MAX_OUTBOUND_PIN_PERCENT) / 100).max(1) |
| 123 | }) |
| 124 | } |