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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::fmt;
use std::time::Duration;
use std::error::Error;
use std::sync::Arc;
use scheduled_thread_pool::ScheduledThreadPool;
use {HandleError, LoggingErrorHandler, CustomizeConnection, NopConnectionCustomizer};
pub struct Builder<C, E> {
pool_size: u32,
min_idle: Option<u32>,
helper_threads: u32,
test_on_check_out: bool,
initialization_fail_fast: bool,
max_lifetime: Option<Duration>,
idle_timeout: Option<Duration>,
connection_timeout: Duration,
error_handler: Box<HandleError<E>>,
connection_customizer: Box<CustomizeConnection<C, E>>,
thread_pool: Option<Arc<ScheduledThreadPool>>,
}
impl<C, E> fmt::Debug for Builder<C, E> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Builder")
.field("pool_size", &self.pool_size)
.field("min_idle", &self.min_idle)
.field("helper_threads", &self.helper_threads)
.field("test_on_check_out", &self.test_on_check_out)
.field("initialization_fail_fast", &self.initialization_fail_fast)
.field("max_lifetime", &self.max_lifetime)
.field("idle_timeout", &self.idle_timeout)
.field("connection_timeout", &self.connection_timeout)
.field("error_handler", &self.error_handler)
.field("connection_customizer", &self.connection_customizer)
.finish()
}
}
impl<C, E: Error> Builder<C, E> {
pub fn new() -> Builder<C, E> {
Builder {
pool_size: 10,
min_idle: None,
helper_threads: 3,
test_on_check_out: true,
initialization_fail_fast: true,
idle_timeout: Some(Duration::from_secs(10 * 60)),
max_lifetime: Some(Duration::from_secs(30 * 60)),
connection_timeout: Duration::from_secs(30),
error_handler: Box::new(LoggingErrorHandler),
connection_customizer: Box::new(NopConnectionCustomizer),
thread_pool: None,
}
}
pub fn pool_size(mut self, pool_size: u32) -> Builder<C, E> {
assert!(pool_size > 0, "pool_size must be positive");
self.pool_size = pool_size;
self
}
pub fn min_idle(mut self, min_idle: Option<u32>) -> Builder<C, E> {
self.min_idle = min_idle;
self
}
pub fn helper_threads(mut self, helper_threads: u32) -> Builder<C, E> {
assert!(helper_threads > 0, "helper_threads must be positive");
self.helper_threads = helper_threads;
self
}
pub fn thread_pool(mut self, thread_pool: Option<Arc<ScheduledThreadPool>>) -> Builder<C, E> {
self.thread_pool = thread_pool;
self
}
pub fn test_on_check_out(mut self, test_on_check_out: bool) -> Builder<C, E> {
self.test_on_check_out = test_on_check_out;
self
}
pub fn initialization_fail_fast(mut self, initialization_fail_fast: bool) -> Builder<C, E> {
self.initialization_fail_fast = initialization_fail_fast;
self
}
pub fn max_lifetime(mut self, max_lifetime: Option<Duration>) -> Builder<C, E> {
assert!(
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<C, E> {
assert!(
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<C, E> {
assert!(
connection_timeout > Duration::from_secs(0),
"connection_timeout must be positive"
);
self.connection_timeout = connection_timeout;
self
}
pub fn error_handler(mut self, error_handler: Box<HandleError<E>>) -> Builder<C, E> {
self.error_handler = error_handler;
self
}
pub fn connection_customizer(
mut self,
connection_customizer: Box<CustomizeConnection<C, E>>,
) -> Builder<C, E> {
self.connection_customizer = connection_customizer;
self
}
pub fn build(self) -> Config<C, E> {
if let Some(min_idle) = self.min_idle {
assert!(
self.pool_size >= min_idle,
"min_idle must be no larger than pool_size"
);
}
let thread_pool = match self.thread_pool {
Some(thread_pool) => thread_pool,
None => {
Arc::new(ScheduledThreadPool::with_name(
"r2d2-worker-{}",
self.helper_threads as usize,
))
}
};
Config {
pool_size: self.pool_size,
min_idle: self.min_idle,
helper_threads: self.helper_threads,
test_on_check_out: self.test_on_check_out,
initialization_fail_fast: self.initialization_fail_fast,
max_lifetime: self.max_lifetime,
idle_timeout: self.idle_timeout,
connection_timeout: self.connection_timeout,
error_handler: self.error_handler,
connection_customizer: self.connection_customizer,
thread_pool: thread_pool,
}
}
}
pub struct Config<C, E> {
pool_size: u32,
min_idle: Option<u32>,
helper_threads: u32,
test_on_check_out: bool,
initialization_fail_fast: bool,
max_lifetime: Option<Duration>,
idle_timeout: Option<Duration>,
connection_timeout: Duration,
error_handler: Box<HandleError<E>>,
connection_customizer: Box<CustomizeConnection<C, E>>,
thread_pool: Arc<ScheduledThreadPool>,
}
impl<C, E> fmt::Debug for Config<C, E> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Config")
.field("pool_size", &self.pool_size)
.field("min_idle", &self.min_idle)
.field("helper_threads", &self.helper_threads)
.field("test_on_check_out", &self.test_on_check_out)
.field("initialization_fail_fast", &self.initialization_fail_fast)
.field("max_lifetime", &self.max_lifetime)
.field("idle_timeout", &self.idle_timeout)
.field("connection_timeout", &self.connection_timeout)
.field("error_handler", &self.error_handler)
.field("connection_customizer", &self.connection_customizer)
.finish()
}
}
impl<C, E: Error> Default for Config<C, E> {
fn default() -> Config<C, E> {
Config::builder().build()
}
}
impl<C, E: Error> Config<C, E> {
pub fn builder() -> Builder<C, E> {
Builder::new()
}
pub fn pool_size(&self) -> u32 {
self.pool_size
}
pub fn min_idle(&self) -> Option<u32> {
self.min_idle
}
#[deprecated(since = "0.7.4", note = "use thread_pool directly")]
pub fn helper_threads(&self) -> u32 {
self.helper_threads
}
pub fn test_on_check_out(&self) -> bool {
self.test_on_check_out
}
pub fn initialization_fail_fast(&self) -> bool {
self.initialization_fail_fast
}
pub fn idle_timeout(&self) -> Option<Duration> {
self.idle_timeout
}
pub fn max_lifetime(&self) -> Option<Duration> {
self.max_lifetime
}
pub fn connection_timeout(&self) -> Duration {
self.connection_timeout
}
pub fn error_handler(&self) -> &HandleError<E> {
&*self.error_handler
}
pub fn connection_customizer(&self) -> &CustomizeConnection<C, E> {
&*self.connection_customizer
}
pub fn thread_pool(&self) -> &Arc<ScheduledThreadPool> {
&self.thread_pool
}
}
#[cfg(test)]
mod test {
use std::time::Duration;
use super::*;
use test::Error;
#[test]
#[allow(deprecated)]
fn builder() {
let config = Config::<(), Error>::builder()
.pool_size(1)
.helper_threads(2)
.test_on_check_out(false)
.initialization_fail_fast(false)
.connection_timeout(Duration::from_secs(3))
.build();
assert_eq!(1, config.pool_size());
assert_eq!(2, config.helper_threads());
assert_eq!(false, config.test_on_check_out());
assert_eq!(false, config.initialization_fail_fast());
assert_eq!(Duration::from_secs(3), config.connection_timeout());
}
#[test]
#[should_panic(expected = "pool_size must be positive")]
fn builder_zero_pool_size() {
Config::<(), Error>::builder().pool_size(0);
}
#[test]
#[should_panic(expected = "helper_threads must be positive")]
fn builder_zero_helper_threads() {
Config::<(), Error>::builder().helper_threads(0);
}
#[test]
#[should_panic(expected = "connection_timeout must be positive")]
fn builder_zero_connection_timeout() {
Config::<(), Error>::builder().connection_timeout(Duration::from_secs(0));
}
#[test]
#[should_panic(expected = "idle_timeout must be positive")]
fn builder_zero_idle_timeout() {
Config::<(), Error>::builder().idle_timeout(Some(Duration::from_secs(0)));
}
#[test]
#[should_panic(expected = "max_lifetime must be positive")]
fn builder_zero_max_lifetime() {
Config::<(), Error>::builder().max_lifetime(Some(Duration::from_secs(0)));
}
#[test]
#[should_panic(expected = "min_idle must be no larger than pool_size")]
fn builder_too_many_num_idle() {
Config::<(), Error>::builder()
.pool_size(1)
.min_idle(Some(2))
.build();
}
}