Skip to main content

rskit_worker/pool/
config.rs

1//! Pool statistics, overflow policy, and configuration.
2
3use std::time::Duration;
4
5use crate::dispatch::DispatchStrategy;
6
7/// Statistics snapshot for the pool.
8#[derive(Debug, Clone)]
9pub struct PoolStats {
10    /// Human-readable name of the pool.
11    pub name: String,
12    /// Number of tasks currently executing.
13    pub running: usize,
14    /// Maximum concurrent tasks the pool allows.
15    pub capacity: usize,
16}
17
18/// Overflow behavior applied when the submission queue is full.
19#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
20#[non_exhaustive]
21pub enum OverflowPolicy {
22    /// Wait until queue capacity becomes available.
23    #[default]
24    Block,
25    /// Reject the new submission immediately.
26    Reject,
27    /// Drop the oldest queued task and enqueue the new submission.
28    DropOldest,
29}
30
31/// Configuration for a [`Pool`](super::Pool).
32pub struct PoolConfig {
33    /// Human-readable name used in tracing.
34    pub name: String,
35    /// Maximum concurrent tasks (semaphore permits).
36    pub size: usize,
37    /// Capacity of the internal submit queue.
38    pub queue_size: usize,
39    /// Broadcast channel capacity for events per task.
40    pub event_buffer: usize,
41    /// Grace period given to in-flight tasks on shutdown.
42    pub grace_period: Duration,
43    /// Dispatch strategy (reserved for future multi-queue extensions).
44    pub dispatch: DispatchStrategy,
45    /// Queue overflow behavior.
46    pub overflow_policy: OverflowPolicy,
47}
48
49impl Default for PoolConfig {
50    fn default() -> Self {
51        Self {
52            name: "pool".into(),
53            size: available_parallelism(),
54            queue_size: 256,
55            event_buffer: 64,
56            grace_period: Duration::from_secs(30),
57            dispatch: DispatchStrategy::RoundRobin,
58            overflow_policy: OverflowPolicy::Block,
59        }
60    }
61}
62
63impl PoolConfig {
64    /// Create a named pool configuration with sensible defaults.
65    #[must_use]
66    pub fn new(name: impl Into<String>) -> Self {
67        Self {
68            name: name.into(),
69            ..Default::default()
70        }
71    }
72
73    /// Set the maximum number of concurrent tasks.
74    /// Values below 1 are clamped to 1 inside `Pool::new` (with a tracing warning),
75    /// since a zero-sized pool can never execute tasks.
76    #[must_use]
77    pub fn with_size(mut self, size: usize) -> Self {
78        self.size = size;
79        self
80    }
81
82    /// Set the capacity of the internal submit queue.
83    #[must_use]
84    pub fn with_queue_size(mut self, queue_size: usize) -> Self {
85        self.queue_size = queue_size;
86        self
87    }
88
89    /// Set the grace period given to in-flight tasks during shutdown.
90    #[must_use]
91    pub fn with_grace_period(mut self, d: Duration) -> Self {
92        self.grace_period = d;
93        self
94    }
95
96    /// Set the queue overflow policy.
97    #[must_use]
98    pub fn with_overflow_policy(mut self, overflow_policy: OverflowPolicy) -> Self {
99        self.overflow_policy = overflow_policy;
100        self
101    }
102}
103
104fn available_parallelism() -> usize {
105    std::thread::available_parallelism()
106        .map(|n| n.get())
107        .unwrap_or(4)
108}