rskit_worker/pool/
config.rs1use std::time::Duration;
4
5use crate::dispatch::DispatchStrategy;
6
7#[derive(Debug, Clone)]
9pub struct PoolStats {
10 pub name: String,
12 pub running: usize,
14 pub capacity: usize,
16}
17
18#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
20#[non_exhaustive]
21pub enum OverflowPolicy {
22 #[default]
24 Block,
25 Reject,
27 DropOldest,
29}
30
31pub struct PoolConfig {
33 pub name: String,
35 pub size: usize,
37 pub queue_size: usize,
39 pub event_buffer: usize,
41 pub grace_period: Duration,
43 pub dispatch: DispatchStrategy,
45 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 #[must_use]
66 pub fn new(name: impl Into<String>) -> Self {
67 Self {
68 name: name.into(),
69 ..Default::default()
70 }
71 }
72
73 #[must_use]
77 pub fn with_size(mut self, size: usize) -> Self {
78 self.size = size;
79 self
80 }
81
82 #[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 #[must_use]
91 pub fn with_grace_period(mut self, d: Duration) -> Self {
92 self.grace_period = d;
93 self
94 }
95
96 #[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}