deadpool/managed/config.rs
1use std::{fmt, time::Duration};
2
3use super::BuildError;
4
5/// [`Pool`] configuration.
6///
7/// [`Pool`]: super::Pool
8#[derive(Clone, Copy, Debug)]
9#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
10pub struct PoolConfig {
11 /// Maximum size of the [`Pool`].
12 ///
13 /// Default: `cpu_count * 4`
14 ///
15 /// [`Pool`]: super::Pool
16 pub max_size: usize,
17
18 /// Timeouts of the [`Pool`].
19 ///
20 /// Default: No timeouts
21 ///
22 /// [`Pool`]: super::Pool
23 #[cfg_attr(feature = "serde", serde(default))]
24 pub timeouts: Timeouts,
25
26 /// Queue mode of the [`Pool`].
27 ///
28 /// Determines the order of objects being queued and dequeued.
29 ///
30 /// Default: `Fifo`
31 ///
32 /// [`Pool`]: super::Pool
33 #[cfg_attr(feature = "serde", serde(default))]
34 pub queue_mode: QueueMode,
35}
36
37impl PoolConfig {
38 /// Creates a new [`PoolConfig`] without any timeouts and with the provided
39 /// `max_size`.
40 #[must_use]
41 pub fn new(max_size: usize) -> Self {
42 Self {
43 max_size,
44 timeouts: Timeouts::default(),
45 queue_mode: QueueMode::default(),
46 }
47 }
48}
49
50impl Default for PoolConfig {
51 /// Creates a new [`PoolConfig`] with the `max_size` being set to
52 /// `cpu_count * 4` ignoring any logical CPUs (Hyper-Threading).
53 fn default() -> Self {
54 Self::new(num_cpus::get_physical() * 4)
55 }
56}
57
58/// Timeouts when getting [`Object`]s from a [`Pool`].
59///
60/// [`Object`]: super::Object
61/// [`Pool`]: super::Pool
62#[derive(Clone, Copy, Debug)]
63#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
64pub struct Timeouts {
65 /// Timeout when waiting for a slot to become available.
66 pub wait: Option<Duration>,
67
68 /// Timeout when creating a new object.
69 pub create: Option<Duration>,
70
71 /// Timeout when recycling an object.
72 pub recycle: Option<Duration>,
73}
74
75impl Timeouts {
76 /// Create an empty [`Timeouts`] config (no timeouts set).
77 #[must_use]
78 pub const fn new() -> Self {
79 Self {
80 create: None,
81 wait: None,
82 recycle: None,
83 }
84 }
85
86 /// Creates a new [`Timeouts`] config with only the `wait` timeout being
87 /// set.
88 #[must_use]
89 pub const fn wait_millis(wait: u64) -> Self {
90 Self {
91 create: None,
92 wait: Some(Duration::from_millis(wait)),
93 recycle: None,
94 }
95 }
96}
97
98// Implemented manually to provide a custom documentation.
99impl Default for Timeouts {
100 /// Creates an empty [`Timeouts`] config (no timeouts set).
101 fn default() -> Self {
102 Self::new()
103 }
104}
105
106/// Mode for dequeuing [`Object`]s from a [`Pool`].
107///
108/// [`Object`]: super::Object
109/// [`Pool`]: super::Pool
110#[derive(Clone, Copy, Debug)]
111#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
112pub enum QueueMode {
113 /// Dequeue the object that was least recently added (first in first out).
114 Fifo,
115 /// Dequeue the object that was most recently added (last in first out).
116 Lifo,
117}
118
119impl Default for QueueMode {
120 fn default() -> Self {
121 Self::Fifo
122 }
123}
124
125/// This error is used when building pools via the config `create_pool`
126/// methods.
127#[derive(Debug)]
128pub enum CreatePoolError<C> {
129 /// This variant is used for configuration errors
130 Config(C),
131 /// This variant is used for errors while building the pool
132 Build(BuildError),
133}
134
135impl<C> fmt::Display for CreatePoolError<C>
136where
137 C: fmt::Display,
138{
139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 match self {
141 Self::Config(e) => write!(f, "Config: {}", e),
142 Self::Build(e) => write!(f, "Build: {}", e),
143 }
144 }
145}
146
147impl<C> std::error::Error for CreatePoolError<C> where C: std::error::Error {}