use std::time::Duration;
use crate::engine::wasm::bindings::astrid::process::host::OverflowPolicy;
use super::ring::Overflow;
const DEFAULT_LOG_RING_BYTES: usize = 1024 * 1024;
const MAX_LOG_RING_BYTES: usize = 8 * 1024 * 1024;
const MIN_LOG_RING_BYTES: usize = 4096;
pub(super) const MAX_STDIN_WRITE: usize = 1024 * 1024;
pub(super) const MAX_RETAINED_PER_PRINCIPAL: usize = 32;
pub(super) const MAX_REGISTRY_ENTRIES: usize = 256;
const MAX_LIFETIME: Duration = Duration::from_secs(60 * 60 * 6);
const DEFAULT_IDLE_TIMEOUT: Duration = Duration::from_secs(60 * 30);
const DEFAULT_EXIT_RETENTION: Duration = Duration::from_secs(60 * 5);
const MAX_EXIT_RETENTION: Duration = Duration::from_secs(60 * 60);
pub(super) const DEFAULT_STOP_GRACE: Duration = Duration::from_secs(5);
pub(super) const MAX_STOP_GRACE: Duration = Duration::from_secs(30);
pub(super) const MAX_READ_SINCE_BYTES: usize = 4 * 1024 * 1024;
const MAX_LABEL_BYTES: usize = 128;
pub(super) fn overflow_from_wit(o: Option<OverflowPolicy>) -> Overflow {
match o {
Some(OverflowPolicy::Backpressure) => Overflow::Backpressure,
_ => Overflow::DropOldest,
}
}
pub(super) fn clamp_log_ring(bytes: Option<u32>) -> usize {
bytes
.map(|b| (b as usize).clamp(MIN_LOG_RING_BYTES, MAX_LOG_RING_BYTES))
.unwrap_or(DEFAULT_LOG_RING_BYTES)
}
pub(super) fn clamp_label(label: Option<String>, cmd: &str) -> String {
let raw = label.unwrap_or_else(|| cmd.to_string());
let mut out = String::with_capacity(MAX_LABEL_BYTES);
for c in raw.chars().filter(|c| !c.is_control()) {
if out.len() + c.len_utf8() > MAX_LABEL_BYTES {
break;
}
out.push(c);
}
out
}
pub(super) fn resolve_ttls(
max_lifetime_ms: Option<u64>,
idle_timeout_ms: Option<u64>,
exit_retention_ms: Option<u64>,
) -> (Duration, Duration, Duration) {
let lifetime = max_lifetime_ms
.map(Duration::from_millis)
.unwrap_or(MAX_LIFETIME)
.min(MAX_LIFETIME);
let idle = idle_timeout_ms
.map(Duration::from_millis)
.unwrap_or(DEFAULT_IDLE_TIMEOUT)
.min(MAX_LIFETIME);
let retention = exit_retention_ms
.map(Duration::from_millis)
.unwrap_or(DEFAULT_EXIT_RETENTION)
.min(MAX_EXIT_RETENTION);
(lifetime, idle, retention)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clamp_label_caps_bytes_not_chars() {
let long = "é".repeat(MAX_LABEL_BYTES); let clamped = clamp_label(Some(long), "cmd");
assert!(clamped.len() <= MAX_LABEL_BYTES);
assert!(clamped.is_char_boundary(clamped.len())); }
#[test]
fn clamp_label_strips_control_and_derives_from_cmd() {
assert_eq!(clamp_label(Some("a\nb\tc".into()), "cmd"), "abc");
assert_eq!(clamp_label(None, "my-cmd"), "my-cmd");
}
}