Skip to main content

dekopon_process/
lib.rs

1#![forbid(unsafe_code)]
2#![cfg_attr(test, allow(clippy::unwrap_used))]
3#![cfg_attr(
4    test,
5    allow(
6        clippy::disallowed_methods,
7        clippy::disallowed_types,
8        reason = "tests spawn, join and drain freely; production sites carry their own expectation"
9    )
10)]
11use std::time::Duration;
12use tokio::sync::watch;
13mod threads;
14mod tree;
15pub use threads::{ShellThreads, ThreadName, ThreadPermit};
16pub use tree::{CallBudget, RetainedBytes, TreeContext};
17
18pub const DEFAULT_MAX_STEPS: u64 = 100_000;
19pub const DEFAULT_MAX_RECURSION_DEPTH: u32 = 32;
20pub const DEFAULT_MAX_OUTPUT_BYTES: usize = 256 * 1024;
21pub const DEFAULT_MAX_OUTPUT_LINES: usize = 2_000;
22pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
23pub const DEFAULT_MAX_CAPABILITY_CALLS: u32 = 32;
24pub const DEFAULT_MAX_VALUE_BYTES: u64 = 32 * 1024 * 1024;
25
26#[derive(Clone, Copy, Debug, Eq, PartialEq)]
27pub struct Limits {
28    pub max_steps: u64,
29    pub max_recursion_depth: u32,
30    pub max_output_bytes: usize,
31    pub max_output_lines: usize,
32    pub timeout: Duration,
33    pub max_capability_calls: u32,
34    pub max_value_bytes: u64,
35}
36
37impl Default for Limits {
38    fn default() -> Self {
39        Self {
40            max_steps: DEFAULT_MAX_STEPS,
41            max_recursion_depth: DEFAULT_MAX_RECURSION_DEPTH,
42            max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES,
43            max_output_lines: DEFAULT_MAX_OUTPUT_LINES,
44            timeout: DEFAULT_TIMEOUT,
45            max_capability_calls: DEFAULT_MAX_CAPABILITY_CALLS,
46            max_value_bytes: DEFAULT_MAX_VALUE_BYTES,
47        }
48    }
49}
50
51#[derive(Clone, Copy, Debug, Eq, PartialEq)]
52pub enum LimitExceeded {
53    Steps { maximum: u64 },
54    RecursionDepth { maximum: u32 },
55    Deadline { timeout_ms: u128 },
56    CapabilityCalls { maximum: u32 },
57    ValueBytes { maximum: u64 },
58    ShellThreads { maximum: u32 },
59    Cancelled,
60}
61
62#[derive(Clone)]
63pub struct CancelHandle {
64    sender: watch::Sender<bool>,
65}
66
67impl CancelHandle {
68    pub fn cancel(&self) {
69        self.sender.send_replace(true);
70    }
71}
72
73#[derive(Clone, Debug)]
74pub struct CancelSignal {
75    receiver: watch::Receiver<bool>,
76}
77
78impl CancelSignal {
79    #[must_use]
80    pub fn pair() -> (CancelHandle, Self) {
81        let (sender, receiver) = watch::channel(false);
82        (CancelHandle { sender }, Self { receiver })
83    }
84
85    #[must_use]
86    pub fn never() -> Self {
87        Self::pair().1
88    }
89
90    #[must_use]
91    pub fn is_cancelled(&self) -> bool {
92        *self.receiver.borrow()
93    }
94
95    #[must_use]
96    pub fn watch(&self) -> watch::Receiver<bool> {
97        self.receiver.clone()
98    }
99
100    pub async fn cancelled(&self) {
101        if self.watch().wait_for(|cancelled| *cancelled).await.is_err() {
102            std::future::pending::<()>().await;
103        }
104    }
105}
106
107mod exit;
108mod job;
109pub mod pipe;
110pub use exit::ExitCode;
111pub use job::{JOBS_OFF, JobId, JobOutcome, JobState, JobSummary, JobWait};
112
113#[cfg(test)]
114mod tests;