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    /// [`Pool`]: super::Pool
14    pub max_size: usize,
15
16    /// Timeouts of the [`Pool`].
17    ///
18    /// [`Pool`]: super::Pool
19    #[cfg_attr(feature = "serde", serde(default))]
20    pub timeouts: Timeouts,
21}
22
23impl PoolConfig {
24    /// Creates a new [`PoolConfig`] without any timeouts and with the provided
25    /// `max_size`.
26    #[must_use]
27    pub fn new(max_size: usize) -> Self {
28        Self {
29            max_size,
30            timeouts: Timeouts::default(),
31        }
32    }
33}
34
35impl Default for PoolConfig {
36    /// Creates a new [`PoolConfig`] with the `max_size` being set to
37    /// `cpu_count * 4` ignoring any logical CPUs (Hyper-Threading).
38    fn default() -> Self {
39        Self::new(num_cpus::get_physical() * 4)
40    }
41}
42
43/// Timeouts when getting [`Object`]s from a [`Pool`].
44///
45/// [`Object`]: super::Object
46/// [`Pool`]: super::Pool
47#[derive(Clone, Copy, Debug)]
48#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
49pub struct Timeouts {
50    /// Timeout when waiting for a slot to become available.
51    pub wait: Option<Duration>,
52
53    /// Timeout when creating a new object.
54    pub create: Option<Duration>,
55
56    /// Timeout when recycling an object.
57    pub recycle: Option<Duration>,
58}
59
60impl Timeouts {
61    /// Create an empty [`Timeouts`] config (no timeouts set).
62    #[must_use]
63    pub fn new() -> Self {
64        Self::default()
65    }
66
67    /// Creates a new [`Timeouts`] config with only the `wait` timeout being
68    /// set.
69    #[must_use]
70    pub fn wait_millis(wait: u64) -> Self {
71        Self {
72            create: None,
73            wait: Some(Duration::from_millis(wait)),
74            recycle: None,
75        }
76    }
77}
78
79// Implemented manually to provide a custom documentation.
80impl Default for Timeouts {
81    /// Creates an empty [`Timeouts`] config (no timeouts set).
82    fn default() -> Self {
83        Self {
84            create: None,
85            wait: None,
86            recycle: None,
87        }
88    }
89}
90
91/// This error is used when building pools via the config `create_pool`
92/// methods.
93#[derive(Debug)]
94pub enum CreatePoolError<C, B> {
95    /// This variant is used for configuration errors
96    Config(C),
97    /// This variant is used for errors while building the pool
98    Build(BuildError<B>),
99}
100
101impl<C, B> fmt::Display for CreatePoolError<C, B>
102where
103    C: fmt::Display,
104    B: fmt::Display,
105{
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        match self {
108            Self::Config(e) => write!(f, "Config: {}", e),
109            Self::Build(e) => write!(f, "Build: {}", e),
110        }
111    }
112}
113
114impl<C, B> std::error::Error for CreatePoolError<C, B>
115where
116    C: std::error::Error,
117    B: std::error::Error,
118{
119}