| //! 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 | |
| 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 | |
| } | |
| } | |