wasmer_wasix/
capabilities.rs

1use std::time::Duration;
2
3use crate::http::HttpClientCapabilityV1;
4
5/// Defines capabilities for a Wasi environment.
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7pub struct Capabilities {
8    pub insecure_allow_all: bool,
9    pub http_client: HttpClientCapabilityV1,
10    pub polling: CapabilityPollingV1,
11    pub max_sock_recv_size: Option<u64>,
12    pub threading: CapabilityThreadingV1,
13}
14
15impl Capabilities {
16    pub fn new() -> Self {
17        Self {
18            insecure_allow_all: false,
19            http_client: Default::default(),
20            polling: Default::default(),
21            max_sock_recv_size: Some(16 * 1024 * 1024),
22            threading: Default::default(),
23        }
24    }
25
26    /// Merges another [`Capabilities`] object into this one, overwriting fields
27    /// if necessary.
28    pub fn update(&mut self, other: Capabilities) {
29        let Capabilities {
30            insecure_allow_all,
31            http_client,
32            polling,
33            max_sock_recv_size,
34            threading,
35        } = other;
36        self.insecure_allow_all |= insecure_allow_all;
37        self.http_client.update(http_client);
38        self.polling.update(polling);
39        self.max_sock_recv_size = max_sock_recv_size.or(self.max_sock_recv_size);
40        self.threading.update(threading);
41    }
42}
43
44impl Default for Capabilities {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50/// Defines polling related permissions and limits.
51#[derive(Debug, Clone, PartialEq, Eq, Hash)]
52pub struct CapabilityPollingV1 {
53    /// Maximum number of subscriptions accepted by `poll_oneoff`.
54    ///
55    /// [`None`] means no explicit limit.
56    pub max_poll_subscriptions: Option<usize>,
57}
58
59impl Default for CapabilityPollingV1 {
60    fn default() -> Self {
61        Self {
62            max_poll_subscriptions: Some(1024),
63        }
64    }
65}
66
67impl CapabilityPollingV1 {
68    pub fn update(&mut self, other: CapabilityPollingV1) {
69        let CapabilityPollingV1 {
70            max_poll_subscriptions,
71        } = other;
72        self.max_poll_subscriptions = max_poll_subscriptions;
73    }
74}
75
76/// Defines threading related permissions.
77#[derive(Debug, Clone, PartialEq, Eq, Hash)]
78pub struct CapabilityThreadingV1 {
79    /// Maximum number of threads that can be spawned.
80    ///
81    /// [`None`] means no limit.
82    pub max_threads: Option<usize>,
83
84    /// Flag that indicates if asynchronous threading is enabled.
85    /// (default = true)
86    pub enable_asynchronous_threading: bool,
87
88    /// Flag that indicates if deep sleep is enabled.
89    /// (default = false)
90    pub enable_deep_sleep: bool,
91
92    /// Enables an exponential backoff of the process CPU usage when there
93    /// are no active run tokens (when set holds the maximum amount of
94    /// time that it will pause the CPU)
95    /// (default = off)
96    pub enable_exponential_cpu_backoff: Option<Duration>,
97
98    /// Switches to a blocking sleep implementation instead
99    /// of the asynchronous runtime based implementation
100    pub enable_blocking_sleep: bool,
101}
102
103impl Default for CapabilityThreadingV1 {
104    fn default() -> Self {
105        Self {
106            max_threads: None,
107            enable_asynchronous_threading: true,
108            enable_deep_sleep: false,
109            enable_exponential_cpu_backoff: None,
110            enable_blocking_sleep: false,
111        }
112    }
113}
114
115impl CapabilityThreadingV1 {
116    pub fn update(&mut self, other: CapabilityThreadingV1) {
117        let CapabilityThreadingV1 {
118            max_threads,
119            enable_asynchronous_threading,
120            enable_deep_sleep,
121            enable_exponential_cpu_backoff,
122            enable_blocking_sleep,
123        } = other;
124        self.enable_asynchronous_threading |= enable_asynchronous_threading;
125        self.enable_deep_sleep |= enable_deep_sleep;
126        if let Some(val) = enable_exponential_cpu_backoff {
127            self.enable_exponential_cpu_backoff = Some(val);
128        }
129        self.max_threads = max_threads.or(self.max_threads);
130        self.enable_blocking_sleep |= enable_blocking_sleep;
131    }
132}