reinhardt_db/pool/
config.rs1#[non_exhaustive]
4#[derive(Debug, Clone)]
5pub struct PoolConfig {
7 pub max_size: u32,
9 pub min_idle: Option<u32>,
11 pub max_lifetime: Option<std::time::Duration>,
13 pub idle_timeout: Option<std::time::Duration>,
15 pub connect_timeout: std::time::Duration,
17 pub max_connections: u32,
19 pub min_connections: u32,
21 pub acquire_timeout: std::time::Duration,
23 pub test_before_acquire: bool,
25}
26
27impl Default for PoolConfig {
28 fn default() -> Self {
29 Self {
30 max_size: 10,
31 min_idle: None,
32 max_lifetime: Some(std::time::Duration::from_secs(1800)),
33 idle_timeout: Some(std::time::Duration::from_secs(600)),
34 connect_timeout: std::time::Duration::from_secs(30),
35 max_connections: 10,
36 min_connections: 1,
37 acquire_timeout: std::time::Duration::from_secs(30),
38 test_before_acquire: false,
39 }
40 }
41}
42
43impl PoolConfig {
44 pub fn new() -> Self {
55 Self::default()
56 }
57
58 pub fn with_max_connections(mut self, max: u32) -> Self {
60 self.max_connections = max;
61 self
62 }
63
64 pub fn with_min_connections(mut self, min: u32) -> Self {
66 self.min_connections = min;
67 self
68 }
69
70 pub fn with_connection_timeout(mut self, timeout: std::time::Duration) -> Self {
72 self.connect_timeout = timeout;
73 self
74 }
75
76 pub fn with_connect_timeout(mut self, timeout: std::time::Duration) -> Self {
78 self.connect_timeout = timeout;
79 self
80 }
81
82 pub fn with_acquire_timeout(mut self, timeout: std::time::Duration) -> Self {
84 self.acquire_timeout = timeout;
85 self
86 }
87
88 pub fn with_max_lifetime(mut self, lifetime: Option<std::time::Duration>) -> Self {
90 self.max_lifetime = lifetime;
91 self
92 }
93
94 pub fn with_idle_timeout(mut self, timeout: Option<std::time::Duration>) -> Self {
96 self.idle_timeout = timeout;
97 self
98 }
99
100 pub fn with_test_before_acquire(mut self, test: bool) -> Self {
102 self.test_before_acquire = test;
103 self
104 }
105
106 pub fn validate(&self) -> Result<(), String> {
108 if self.max_connections < self.min_connections {
109 return Err("max_connections must be >= min_connections".to_string());
110 }
111 Ok(())
112 }
113}
114
115#[non_exhaustive]
116#[derive(Debug, Clone, Default)]
117pub struct PoolOptions {
119 pub config: PoolConfig,
121}
122
123impl PoolOptions {
124 pub fn new() -> Self {
126 Self::default()
127 }
128
129 pub fn max_size(mut self, max_size: u32) -> Self {
131 self.config.max_size = max_size;
132 self
133 }
134
135 pub fn min_idle(mut self, min_idle: u32) -> Self {
137 self.config.min_idle = Some(min_idle);
138 self
139 }
140}