Diffinery

homeflare/celldpublic

celld/crates/celld/env_vars.rs
182 lines · 5.4 KBRaw History
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
9use 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.
16pub 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_DEPLOY_POLL_S",
32 "CELLD_EVICTIONS",
33 "CELLD_FETCH_TIMEOUT_S",
34 "CELLD_HANDLER_BUDGET_S",
35 "CELLD_IDLE_EVICT_S",
36 "CELLD_LOG_CAPTURE_WORKERS",
37 "CELLD_LOG_PIPELINE",
38 "CELLD_LTX_COMPACTIONS",
39 "CELLD_LTX_COMPACTION_MIN_TXIDS",
40 "CELLD_LTX_DURABILITY_TIMEOUT_SECS",
41 "CELLD_MAX_LOADED_WORKERS",
42 "CELLD_MAX_CELL_REQUESTS",
43 "CELLD_MAX_OUTBOUND_WEBSOCKETS",
44 "CELLD_MAX_REQUEST_BODY_BYTES",
45 "CELLD_MAX_REQUESTS",
46 "CELLD_MAX_STATELESS_ISOLATES",
47 "CELLD_OPERATION_DEADLINE_MS",
48 "CELLD_RELEASES",
49 "CELLD_SHUTDOWN_DRAIN_MS",
50 "CELLD_SHUTDOWN_TOTAL_MS",
51 "CELLD_TOKIO_THREADS",
52 "CELLD_TTL_MS",
53 "CELLD_WAKER_TICK_MS",
54 ] {
55 positive::<u64>(name)?;
56 }
57
58 for name in [
59 "CELLD_ADMISSION_WAIT_MS",
60 "CELLD_ALARM_RESIDENT_MS",
61 "CELLD_ASSET_CACHE_BYTES",
62 "CELLD_DEPLOY_MAX_AGE_S",
63 "CELLD_DRAIN_TOKEN_WAIT_MS",
64 "CELLD_LOCAL_CACHE_MAX_BYTES",
65 "CELLD_LTX_TRUNCATE_PAGES",
66 "CELLD_LOG_HEDGE_MS",
67 "CELLD_LOG_WINDOW",
68 "CELLD_LOG_WINDOW_BYTES",
69 "CELLD_MAX_RESIDENT_CELLS",
70 "CELLD_MAX_RSS_MB",
71 "CELLD_READY_FLEET_GATE_MS",
72 ] {
73 optional::<u64>(name)?;
74 }
75
76 if let Some(value) = optional::<u64>("CELLD_PRESENCE_HEARTBEAT_MS")? {
77 if !(50..=30_000).contains(&value) {
78 bail!("CELLD_PRESENCE_HEARTBEAT_MS must be between 50 and 30000, not {value}");
79 }
80 }
81
82 if let Some(megabytes) = positive::<usize>("CELLD_V8_HEAP_LIMIT_MB")? {
83 if megabytes.checked_mul(1024 * 1024).is_none() {
84 bail!("CELLD_V8_HEAP_LIMIT_MB is too large: {megabytes}");
85 }
86 }
87
88 if let Some(value) = value("CELLD_LOG_TRANSPORT")? {
89 if !matches!(value.as_str(), "http" | "stream") {
90 bail!("CELLD_LOG_TRANSPORT must be http or stream, not {value:?}");
91 }
92 }
93
94 if let Some(value) = value("CELLD_PRESSURE_OWNERSHIP")? {
95 if !matches!(value.as_str(), "release" | "sticky") {
96 bail!("CELLD_PRESSURE_OWNERSHIP must be release or sticky, not {value:?}");
97 }
98 }
99 Ok(())
100}
101
102pub fn value(name: &str) -> anyhow::Result<Option<String>> {
103 match std::env::var(name) {
104 Ok(value) => Ok(Some(value)),
105 Err(std::env::VarError::NotPresent) => Ok(None),
106 Err(error) => Err(anyhow!("read {name}: {error}")),
107 }
108}
109
110pub fn flag(name: &str, default: bool) -> anyhow::Result<bool> {
111 let value = value(name)?;
112 parse_flag(name, value.as_deref(), default)
113}
114
115pub fn parse_flag(name: &str, value: Option<&str>, default: bool) -> anyhow::Result<bool> {
116 match value {
117 None => Ok(default),
118 Some("0") => Ok(false),
119 Some("1") => Ok(true),
120 Some(other) => bail!("{name} must be 0 or 1, not {other:?}"),
121 }
122}
123
124pub fn optional<T>(name: &str) -> anyhow::Result<Option<T>>
125where
126 T: std::str::FromStr,
127 T::Err: std::fmt::Display,
128{
129 parse_optional(name, value(name)?)
130}
131
132pub fn parse_optional<T>(name: &str, value: Option<String>) -> anyhow::Result<Option<T>>
133where
134 T: std::str::FromStr,
135 T::Err: std::fmt::Display,
136{
137 value
138 .map(|value| {
139 value
140 .parse::<T>()
141 .map_err(|error| anyhow!("{name} has invalid value {value:?}: {error}"))
142 })
143 .transpose()
144}
145
146pub fn with_default<T>(name: &str, default: T) -> anyhow::Result<T>
147where
148 T: std::str::FromStr,
149 T::Err: std::fmt::Display,
150{
151 Ok(optional(name)?.unwrap_or(default))
152}
153
154pub fn positive<T>(name: &str) -> anyhow::Result<Option<T>>
155where
156 T: std::str::FromStr + Default + PartialOrd + std::fmt::Display,
157 T::Err: std::fmt::Display,
158{
159 parse_positive(name, value(name)?)
160}
161
162pub fn parse_positive<T>(name: &str, value: Option<String>) -> anyhow::Result<Option<T>>
163where
164 T: std::str::FromStr + Default + PartialOrd + std::fmt::Display,
165 T::Err: std::fmt::Display,
166{
167 let Some(value) = parse_optional::<T>(name, value)? else {
168 return Ok(None);
169 };
170 if value <= T::default() {
171 bail!("{name} must be greater than zero, not {value}");
172 }
173 Ok(Some(value))
174}
175
176pub fn positive_or<T>(name: &str, default: T) -> anyhow::Result<T>
177where
178 T: std::str::FromStr + Default + PartialOrd + std::fmt::Display,
179 T::Err: std::fmt::Display,
180{
181 Ok(positive(name)?.unwrap_or(default))
182}