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 /// How often a background thread should prune idle resources that have
54 /// outlived `idle_timeout` or `max_lifetime`, rather than waiting for them to
55 /// be rejected on their next checkout. `None` (the default) runs no
56 /// background thread; expiry is then applied lazily, on borrow.
57 ///
58 /// The reaper only prunes — it never creates resources — and has no effect
59 /// unless `idle_timeout` or `max_lifetime` is also set.
60 pub reap_interval: Option<Duration>,
61}
62
63impl Default for PoolConfig {
64 /// A pool of up to ten resources, created on demand, that waits up to thirty
65 /// seconds for a free resource, never expires idle or aged resources, and
66 /// runs no background reaper.
67 fn default() -> Self {
68 PoolConfig {
69 max_size: 10,
70 min_idle: 0,
71 create_timeout: Some(Duration::from_secs(30)),
72 idle_timeout: None,
73 max_lifetime: None,
74 reap_interval: None,
75 }
76 }
77}