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
use core::time::Duration;
use crate::manager::Manager;
use crate::pool::Pool;
pub struct Builder {
pub(crate) max_size: usize,
pub(crate) min_idle: usize,
pub(crate) always_check: bool,
pub(crate) use_gc: bool,
pub(crate) max_lifetime: Option<Duration>,
pub(crate) idle_timeout: Option<Duration>,
pub(crate) connection_timeout: Duration,
pub(crate) wait_timeout: Duration,
pub(crate) reaper_rate: Duration,
}
impl Default for Builder {
fn default() -> Self {
Builder {
max_size: 10,
min_idle: 1,
always_check: true,
use_gc: false,
max_lifetime: Some(Duration::from_secs(30 * 60)),
idle_timeout: Some(Duration::from_secs(10 * 60)),
connection_timeout: Duration::from_secs(10),
wait_timeout: Duration::from_secs(20),
reaper_rate: Duration::from_secs(15),
}
}
}
impl Builder {
pub fn new() -> Builder {
Default::default()
}
pub fn max_size(mut self, max_size: usize) -> Builder {
self.max_size = max_size;
self
}
pub fn min_idle(mut self, min_idle: usize) -> Builder {
self.min_idle = min_idle;
self
}
pub fn always_check(mut self, always_check: bool) -> Builder {
self.always_check = always_check;
self
}
pub fn use_gc(mut self, use_gc: bool) -> Builder {
self.use_gc = use_gc;
self
}
pub fn max_lifetime(mut self, max_lifetime: Option<Duration>) -> Builder {
self.max_lifetime = max_lifetime;
self
}
pub fn idle_timeout(mut self, idle_timeout: Option<Duration>) -> Builder {
self.idle_timeout = idle_timeout;
self
}
pub fn reaper_rate(mut self, reaper_rate: Duration) -> Builder {
self.reaper_rate = reaper_rate;
self
}
pub fn connection_timeout(mut self, connection_timeout: Duration) -> Builder {
self.connection_timeout = connection_timeout;
self
}
pub fn wait_timeout(mut self, wait_timeout: Duration) -> Builder {
self.wait_timeout = wait_timeout;
self
}
pub async fn build<M: Manager>(self, manager: M) -> Result<Pool<M>, M::Error> {
let pool = self.build_uninitialized(manager);
pool.init().await?;
Ok(pool)
}
pub fn build_uninitialized<M: Manager>(self, manager: M) -> Pool<M> {
assert!(
self.max_size >= self.min_idle,
"min_idle must be no larger than max_size"
);
Pool::new(self, manager)
}
pub fn get_reaper_rate(&self) -> Duration {
self.reaper_rate
}
}