1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::ConnectionManager;
use crate::Error;
use crate::Pool;
use std::marker::PhantomData;
use std::time::Duration;
pub struct Config<T> {
pub max_size: u32,
pub min_idle: Option<u32>,
pub max_concurrency: u32,
pub executor: T,
pub connection_timeout: Duration,
pub max_lifetime: Option<Duration>,
pub idle_timeout: Option<Duration>,
}
pub struct Builder<M>
where
M: ConnectionManager,
{
max_size: u32,
min_idle: Option<u32>,
max_concurrency: u32,
max_lifetime: Option<Duration>,
idle_timeout: Option<Duration>,
connection_timeout: Duration,
reaper_rate: Duration,
_keep: PhantomData<M>,
}
impl<M> Default for Builder<M>
where
M: ConnectionManager,
{
fn default() -> Self {
Self {
max_size: 10,
min_idle: None,
max_concurrency: 10000,
idle_timeout: Some(Duration::from_secs(10 * 60)),
max_lifetime: Some(Duration::from_secs(30 * 60)),
connection_timeout: Duration::from_secs(30),
reaper_rate: Duration::from_secs(30),
_keep: PhantomData,
}
}
}
impl<M> Builder<M>
where
M: ConnectionManager,
{
pub fn new() -> Self {
Builder::default()
}
pub fn max_size(mut self, max_size: u32) -> Self {
assert!(max_size > 0, "max_size must be positive");
self.max_size = max_size;
self
}
pub fn max_concurrency(mut self, max_concurrency: u32) -> Self {
assert!(max_concurrency > 0, "max_concurrency must be positive");
self.max_concurrency = max_concurrency;
self
}
pub fn min_idle(mut self, min_idle: Option<u32>) -> Self {
self.min_idle = min_idle;
self
}
pub fn max_lifetime(mut self, max_lifetime: Option<Duration>) -> Builder<M> {
assert_ne!(
max_lifetime,
Some(Duration::from_secs(0)),
"max_lifetime must be positive"
);
self.max_lifetime = max_lifetime;
self
}
pub fn idle_timeout(mut self, idle_timeout: Option<Duration>) -> Builder<M> {
assert_ne!(
idle_timeout,
Some(Duration::from_secs(0)),
"idle_timeout must be positive"
);
self.idle_timeout = idle_timeout;
self
}
pub fn connection_timeout(mut self, connection_timeout: Duration) -> Builder<M> {
assert!(
connection_timeout > Duration::from_secs(0),
"connection_timeout must be positive"
);
self.connection_timeout = connection_timeout;
self
}
#[doc(hidden)]
#[allow(dead_code)]
pub fn reaper_rate(mut self, reaper_rate: Duration) -> Builder<M> {
self.reaper_rate = reaper_rate;
self
}
pub async fn build<E>(self, manager: M) -> Result<Pool<M>, Error<E>>
where
Error<E>: std::convert::From<<M as ConnectionManager>::Error>,
{
let pool = self.build_unchecked(manager).await;
pool.wait_for_initialization().await?;
Ok(pool)
}
pub async fn build_unchecked(self, manager: M) -> Pool<M> {
if let Some(min_idle) = self.min_idle {
assert!(
self.max_size >= min_idle,
"min_idle must be no larger than max_size"
);
}
let config = Config {
max_size: self.max_size,
min_idle: self.min_idle,
max_lifetime: self.max_lifetime,
idle_timeout: self.idle_timeout,
max_concurrency: self.max_concurrency,
connection_timeout: self.connection_timeout,
executor: manager.get_executor(),
};
Pool::new_inner(config, manager, self.reaper_rate).await
}
}