| 1 | // Copyright 2026 Deno Land Inc. Apache-2.0 license. |
| 2 | |
| 3 | //! Strict runtime environment parsing. |
| 4 | //! |
| 5 | //! An unset variable selects its caller's documented default. A supplied |
| 6 | //! variable must contain a valid value, so a typo cannot silently change the |
| 7 | //! configuration of a running node. |
| 8 | |
| 9 | use anyhow::{anyhow, bail}; |
| 10 | |
| 11 | /// Validate every typed production variable before the runtime starts. |
| 12 | /// |
| 13 | /// Some consumers cache a value or read it from a synchronous callback, so |
| 14 | /// they cannot return a configuration error at the point of use. This pass |
| 15 | /// makes those reads infallible without giving malformed values a default. |
| 16 | pub fn validate() -> anyhow::Result<()> { |
| 17 | for name in [ |
| 18 | "CELLD_CLOUD", |
| 19 | "CELLD_CLOUD_RESTART_ON_DEPLOY", |
| 20 | "CELLD_LTX_COMPACTION", |
| 21 | "CELLD_OUTPUT_GATE", |
| 22 | "CELLD_PRESENCE_SHADOW", |
| 23 | "CELLD_TRUST_FORWARDED_HEADERS", |
| 24 | "CELLD_UNSAFE_PUBLIC_ADVERTISE", |
| 25 | ] { |
| 26 | flag(name, false)?; |
| 27 | } |
| 28 | |
| 29 | for name in [ |
| 30 | "CELLD_ACTIVATIONS", |
| 31 | "CELLD_EVICTIONS", |
| 32 | "CELLD_FETCH_TIMEOUT_S", |
| 33 | "CELLD_HANDLER_BUDGET_S", |
| 34 | "CELLD_IDLE_EVICT_S", |
| 35 | "CELLD_LTX_COMPACTIONS", |
| 36 | "CELLD_LTX_COMPACTION_MIN_TXIDS", |
| 37 | "CELLD_MAX_LOADED_WORKERS", |
| 38 | "CELLD_MAX_OUTBOUND_WEBSOCKETS", |
| 39 | "CELLD_MAX_REQUESTS", |
| 40 | "CELLD_MAX_STATELESS_ISOLATES", |
| 41 | "CELLD_OPERATION_DEADLINE_MS", |
| 42 | "CELLD_RELEASES", |
| 43 | "CELLD_SHUTDOWN_DRAIN_MS", |
| 44 | "CELLD_TOKIO_THREADS", |
| 45 | "CELLD_TTL_MS", |
| 46 | "CELLD_WAKER_TICK_MS", |
| 47 | ] { |
| 48 | positive::<u64>(name)?; |
| 49 | } |
| 50 | |
| 51 | for name in [ |
| 52 | "CELLD_ADMISSION_WAIT_MS", |
| 53 | "CELLD_ALARM_RESIDENT_MS", |
| 54 | "CELLD_ASSET_CACHE_BYTES", |
| 55 | "CELLD_LOCAL_CACHE_MAX_BYTES", |
| 56 | "CELLD_MAX_RESIDENT_CELLS", |
| 57 | "CELLD_MAX_RSS_MB", |
| 58 | ] { |
| 59 | optional::<u64>(name)?; |
| 60 | } |
| 61 | |
| 62 | if let Some(value) = optional::<u64>("CELLD_PRESENCE_HEARTBEAT_MS")? { |
| 63 | if !(50..=30_000).contains(&value) { |
| 64 | bail!("CELLD_PRESENCE_HEARTBEAT_MS must be between 50 and 30000, not {value}"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if let Some(megabytes) = positive::<usize>("CELLD_V8_HEAP_LIMIT_MB")? { |
| 69 | if megabytes.checked_mul(1024 * 1024).is_none() { |
| 70 | bail!("CELLD_V8_HEAP_LIMIT_MB is too large: {megabytes}"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if let Some(value) = value("CELLD_PRESSURE_OWNERSHIP")? { |
| 75 | if !matches!(value.as_str(), "release" | "sticky") { |
| 76 | bail!("CELLD_PRESSURE_OWNERSHIP must be release or sticky, not {value:?}"); |
| 77 | } |
| 78 | } |
| 79 | Ok(()) |
| 80 | } |
| 81 | |
| 82 | pub fn value(name: &str) -> anyhow::Result<Option<String>> { |
| 83 | match std::env::var(name) { |
| 84 | Ok(value) => Ok(Some(value)), |
| 85 | Err(std::env::VarError::NotPresent) => Ok(None), |
| 86 | Err(error) => Err(anyhow!("read {name}: {error}")), |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | pub fn flag(name: &str, default: bool) -> anyhow::Result<bool> { |
| 91 | let value = value(name)?; |
| 92 | parse_flag(name, value.as_deref(), default) |
| 93 | } |
| 94 | |
| 95 | pub fn parse_flag(name: &str, value: Option<&str>, default: bool) -> anyhow::Result<bool> { |
| 96 | match value { |
| 97 | None => Ok(default), |
| 98 | Some("0") => Ok(false), |
| 99 | Some("1") => Ok(true), |
| 100 | Some(other) => bail!("{name} must be 0 or 1, not {other:?}"), |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | pub fn optional<T>(name: &str) -> anyhow::Result<Option<T>> |
| 105 | where |
| 106 | T: std::str::FromStr, |
| 107 | T::Err: std::fmt::Display, |
| 108 | { |
| 109 | parse_optional(name, value(name)?) |
| 110 | } |
| 111 | |
| 112 | pub fn parse_optional<T>(name: &str, value: Option<String>) -> anyhow::Result<Option<T>> |
| 113 | where |
| 114 | T: std::str::FromStr, |
| 115 | T::Err: std::fmt::Display, |
| 116 | { |
| 117 | value |
| 118 | .map(|value| { |
| 119 | value |
| 120 | .parse::<T>() |
| 121 | .map_err(|error| anyhow!("{name} has invalid value {value:?}: {error}")) |
| 122 | }) |
| 123 | .transpose() |
| 124 | } |
| 125 | |
| 126 | pub fn with_default<T>(name: &str, default: T) -> anyhow::Result<T> |
| 127 | where |
| 128 | T: std::str::FromStr, |
| 129 | T::Err: std::fmt::Display, |
| 130 | { |
| 131 | Ok(optional(name)?.unwrap_or(default)) |
| 132 | } |
| 133 | |
| 134 | pub fn positive<T>(name: &str) -> anyhow::Result<Option<T>> |
| 135 | where |
| 136 | T: std::str::FromStr + Default + PartialOrd + std::fmt::Display, |
| 137 | T::Err: std::fmt::Display, |
| 138 | { |
| 139 | parse_positive(name, value(name)?) |
| 140 | } |
| 141 | |
| 142 | pub fn parse_positive<T>(name: &str, value: Option<String>) -> anyhow::Result<Option<T>> |
| 143 | where |
| 144 | T: std::str::FromStr + Default + PartialOrd + std::fmt::Display, |
| 145 | T::Err: std::fmt::Display, |
| 146 | { |
| 147 | let Some(value) = parse_optional::<T>(name, value)? else { |
| 148 | return Ok(None); |
| 149 | }; |
| 150 | if value <= T::default() { |
| 151 | bail!("{name} must be greater than zero, not {value}"); |
| 152 | } |
| 153 | Ok(Some(value)) |
| 154 | } |
| 155 | |
| 156 | pub fn positive_or<T>(name: &str, default: T) -> anyhow::Result<T> |
| 157 | where |
| 158 | T: std::str::FromStr + Default + PartialOrd + std::fmt::Display, |
| 159 | T::Err: std::fmt::Display, |
| 160 | { |
| 161 | Ok(positive(name)?.unwrap_or(default)) |
| 162 | } |