Skip to main content

a3s_runtime/contract/
resource.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
4#[serde(rename_all = "snake_case")]
5pub enum IsolationLevel {
6    Process,
7    Container,
8    Sandbox,
9    Confidential,
10}
11
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(deny_unknown_fields)]
14pub struct ResourceLimits {
15    pub cpu_millis: u64,
16    pub memory_bytes: u64,
17    pub pids: u32,
18    /// Optional writable-layer quota. Providers must advertise
19    /// `ResourceControl::EphemeralStorage` before accepting this limit.
20    pub ephemeral_storage_bytes: Option<u64>,
21    /// Required for finite Tasks and forbidden for long-running Services.
22    pub execution_timeout_ms: Option<u64>,
23}
24
25impl ResourceLimits {
26    pub(crate) fn validate(&self) -> Result<(), String> {
27        if self.cpu_millis == 0 || self.memory_bytes == 0 || self.pids == 0 {
28            return Err("required Runtime resource limits must be positive".into());
29        }
30        if self.ephemeral_storage_bytes == Some(0) {
31            return Err("ephemeral_storage_bytes must be positive when present".into());
32        }
33        if self.execution_timeout_ms == Some(0) {
34            return Err("execution_timeout_ms must be positive when present".into());
35        }
36        Ok(())
37    }
38}