db_testkit/
pool.rs

1use std::time::Duration;
2
3/// Configuration for a connection pool
4#[derive(Debug, Clone)]
5pub struct PoolConfig {
6    /// Maximum number of connections in the pool
7    pub max_size: usize,
8    /// Minimum number of idle connections to maintain
9    pub min_idle: Option<usize>,
10    /// Maximum lifetime of a connection
11    pub max_lifetime: Option<Duration>,
12    /// Maximum time to wait for a connection
13    pub connection_timeout: Duration,
14    /// Maximum time a connection can be idle
15    pub idle_timeout: Option<Duration>,
16}
17
18impl Default for PoolConfig {
19    fn default() -> Self {
20        Self {
21            max_size: 10,
22            min_idle: None,
23            max_lifetime: Some(Duration::from_secs(30 * 60)), // 30 minutes
24            connection_timeout: Duration::from_secs(30),
25            idle_timeout: Some(Duration::from_secs(10 * 60)), // 10 minutes
26        }
27    }
28}
29
30impl PoolConfig {
31    /// Create a new pool configuration with the given maximum size
32    pub fn new(max_size: usize) -> Self {
33        Self {
34            max_size,
35            ..Default::default()
36        }
37    }
38
39    /// Set the minimum number of idle connections
40    pub fn min_idle(mut self, min_idle: usize) -> Self {
41        self.min_idle = Some(min_idle);
42        self
43    }
44
45    /// Set the maximum lifetime of a connection
46    pub fn max_lifetime(mut self, max_lifetime: Duration) -> Self {
47        self.max_lifetime = Some(max_lifetime);
48        self
49    }
50
51    /// Set the connection timeout
52    pub fn connection_timeout(mut self, connection_timeout: Duration) -> Self {
53        self.connection_timeout = connection_timeout;
54        self
55    }
56
57    /// Set the idle timeout
58    pub fn idle_timeout(mut self, idle_timeout: Duration) -> Self {
59        self.idle_timeout = Some(idle_timeout);
60        self
61    }
62}