File size: 1,759 Bytes
3374e90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Configuration for the JS worker pool.

/// Configuration for the JsPool.
///
/// Defaults are tuned for a plugin engine workload:
/// - 2 initial workers, up to min(4, cpu_count) max
/// - 32MB memory limit per context
/// - 10s default timeout
/// - 300s context idle TTL (reclaim unused contexts)
/// - 120s worker idle TTL (shrink pool)
/// - 512KB max stack size per runtime
#[derive(Debug, Clone)]
pub struct JsPoolConfig {
    /// Number of worker threads to start initially.
    pub initial_workers: usize,
    /// Maximum number of worker threads.
    pub max_workers: usize,
    /// Memory limit per JS context in bytes.
    pub memory_limit_bytes: usize,
    /// Default evaluation timeout in milliseconds.
    pub default_timeout_ms: u32,
    /// How long a per-plugin JS context can be idle before being reclaimed.
    pub context_idle_ttl_secs: u64,
    /// How long a worker thread can be idle before being shut down.
    pub worker_idle_ttl_secs: u64,
    /// Maximum stack size per JS runtime in bytes.
    pub max_stack_bytes: usize,
}

impl Default for JsPoolConfig {
    fn default() -> Self {
        let cpu_count = std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(2);
        Self {
            initial_workers: 2,
            max_workers: cpu_count.min(4),
            memory_limit_bytes: 32 * 1024 * 1024, // 32 MB
            default_timeout_ms: 10_000,
            context_idle_ttl_secs: 300,
            worker_idle_ttl_secs: 120,
            max_stack_bytes: 512 * 1024, // 512 KB
        }
    }
}

impl JsPoolConfig {
    /// Memory limit in megabytes (for display purposes).
    pub fn memory_limit_mb(&self) -> u32 {
        (self.memory_limit_bytes / (1024 * 1024)) as u32
    }
}