Skip to main content

pool_mod/
config.rs

1//! Pool configuration.
2
3use std::time::Duration;
4
5/// Tunable limits and lifecycle policy for a [`Pool`](crate::Pool).
6///
7/// Construct one through [`Pool::builder`](crate::Pool::builder) for the fluent
8/// path, or assemble it directly — every field is public, so a configuration can
9/// equally be deserialized from a settings file. Validation happens when the pool
10/// is built, not when the struct is created; see
11/// [`Builder::build`](crate::Builder::build).
12///
13/// # Examples
14///
15/// ```
16/// use std::time::Duration;
17/// use pool_mod::PoolConfig;
18///
19/// // Start from the defaults and adjust only what matters.
20/// let cfg = PoolConfig {
21///     max_size: 16,
22///     min_idle: 2,
23///     idle_timeout: Some(Duration::from_secs(300)),
24///     ..PoolConfig::default()
25/// };
26///
27/// assert_eq!(cfg.max_size, 16);
28/// assert_eq!(cfg.create_timeout, Some(Duration::from_secs(30))); // inherited default
29/// ```
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub struct PoolConfig {
32    /// Hard upper bound on the number of resources the pool owns at once — idle
33    /// plus checked out. Must be at least 1.
34    pub max_size: usize,
35
36    /// Number of resources to create up front when the pool is built, and the
37    /// floor the pool keeps ready. Must not exceed `max_size`.
38    pub min_idle: usize,
39
40    /// How long [`Pool::get`](crate::Pool::get) waits for a resource when the
41    /// pool is saturated. `None` waits indefinitely.
42    pub create_timeout: Option<Duration>,
43
44    /// Discard and replace an idle resource that has gone unused for at least
45    /// this long, checked the next time it is borrowed. `None` disables idle
46    /// expiry.
47    pub idle_timeout: Option<Duration>,
48
49    /// Discard and replace a resource older than this, regardless of use,
50    /// checked the next time it is borrowed. `None` disables lifetime expiry.
51    pub max_lifetime: Option<Duration>,
52}
53
54impl Default for PoolConfig {
55    /// A pool of up to ten resources, created on demand, that waits up to thirty
56    /// seconds for a free resource and never expires idle or aged resources.
57    fn default() -> Self {
58        PoolConfig {
59            max_size: 10,
60            min_idle: 0,
61            create_timeout: Some(Duration::from_secs(30)),
62            idle_timeout: None,
63            max_lifetime: None,
64        }
65    }
66}