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 /// Create a [`PoolConfig`] where [`PoolConfig::max_size`] is set to
52 /// `cpu_core_count * 2` including logical cores (Hyper-Threading).
53 fn default() -> Self {
54 Self::new(crate::util::get_default_pool_max_size())
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, Default)]
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 #[default]
115 Fifo,
116 /// Dequeue the object that was most recently added (last in first out).
117 Lifo,
118}
119
120/// This error is used when building pools via the config `create_pool`
121/// methods.
122#[derive(Debug)]
123pub enum CreatePoolError<C> {
124 /// This variant is used for configuration errors
125 Config(C),
126 /// This variant is used for errors while building the pool
127 Build(BuildError),
128}
129
130impl<C> fmt::Display for CreatePoolError<C>
131where
132 C: fmt::Display,
133{
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 match self {
136 Self::Config(e) => write!(f, "Config: {}", e),
137 Self::Build(e) => write!(f, "Build: {}", e),
138 }
139 }
140}
141
142impl<C> std::error::Error for CreatePoolError<C> where C: std::error::Error {}