deadpool/unmanaged/config.rs
1use std::time::Duration;
2
3use crate::Runtime;
4
5/// Pool configuration.
6#[derive(Clone, Copy, Debug)]
7#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
8pub struct PoolConfig {
9 /// Maximum size of the pool.
10 pub max_size: usize,
11
12 /// Timeout for [`Pool::get()`] operation.
13 ///
14 /// [`Pool::get()`]: super::Pool::get
15 pub timeout: Option<Duration>,
16
17 /// [`Runtime`] to be used.
18 ///
19 /// # Important
20 ///
21 /// The [`Runtime`] is optional. Most [`Pool`]s don't need a [`Runtime`].
22 /// If you want to utilize a timeout, however, a [`Runtime`] must be
23 /// specified as you will otherwise get a [`PoolError::NoRuntimeSpecified`]
24 /// when trying to use [`Pool::timeout_get()`] or [`Pool::get()`] with a
25 /// [`PoolConfig::timeout`] configured.
26 ///
27 /// [`Pool`]: super::Pool
28 /// [`Pool::get()`]: super::Pool::get()
29 /// [`Pool::timeout_get()`]: super::Pool::timeout_get()
30 /// [`PoolError::NoRuntimeSpecified`]: super::PoolError::NoRuntimeSpecified
31 #[cfg_attr(feature = "serde", serde(skip))]
32 pub runtime: Option<Runtime>,
33}
34
35impl PoolConfig {
36 /// Create a new [`PoolConfig`] without any timeouts.
37 #[must_use]
38 pub fn new(max_size: usize) -> Self {
39 Self {
40 max_size,
41 timeout: None,
42 runtime: None,
43 }
44 }
45}
46
47impl Default for PoolConfig {
48 /// Create a [`PoolConfig`] where [`PoolConfig::max_size`] is set to
49 /// `cpu_core_count * 2` including logical cores (Hyper-Threading).
50 fn default() -> Self {
51 Self::new(crate::util::get_default_pool_max_size())
52 }
53}