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
use std::fmt;
use debug_builders::DebugStruct;
use {HandleError, NopErrorHandler, CustomizeConnection, NopConnectionCustomizer};
#[derive(Debug)]
pub struct Builder<C, E> {
c: Config<C, E>,
}
impl<C, E> Builder<C, E> {
pub fn new() -> Builder<C, E> {
Builder {
c: Config::default(),
}
}
pub fn pool_size(mut self, pool_size: u32) -> Builder<C, E> {
assert!(pool_size > 0, "pool_size must be positive");
self.c.pool_size = pool_size;
self
}
pub fn helper_threads(mut self, helper_threads: u32) -> Builder<C, E> {
assert!(helper_threads > 0, "helper_threads must be positive");
self.c.helper_threads = helper_threads;
self
}
pub fn test_on_check_out(mut self, test_on_check_out: bool) -> Builder<C, E> {
self.c.test_on_check_out = test_on_check_out;
self
}
pub fn initialization_fail_fast(mut self, initialization_fail_fast: bool) -> Builder<C, E> {
self.c.initialization_fail_fast = initialization_fail_fast;
self
}
pub fn connection_timeout_ms(mut self, connection_timeout_ms: u32) -> Builder<C, E> {
assert!(connection_timeout_ms > 0, "connection_timeout_ms must be positive");
self.c.connection_timeout_ms = connection_timeout_ms;
self
}
pub fn error_handler(mut self, error_handler: Box<HandleError<E>>) -> Builder<C, E> {
self.c.error_handler = error_handler;
self
}
pub fn connection_customizer(mut self, connection_customizer: Box<CustomizeConnection<C, E>>)
-> Builder<C, E> {
self.c.connection_customizer = connection_customizer;
self
}
pub fn build(self) -> Config<C, E> {
self.c
}
}
pub struct Config<C, E> {
pool_size: u32,
helper_threads: u32,
test_on_check_out: bool,
initialization_fail_fast: bool,
connection_timeout_ms: u32,
error_handler: Box<HandleError<E>>,
connection_customizer: Box<CustomizeConnection<C, E>>,
}
impl<C, E> fmt::Debug for Config<C, E> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
DebugStruct::new(fmt, "Config")
.field("pool_size", &self.pool_size)
.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("connection_timeout_ms", &self.connection_timeout_ms)
.finish()
}
}
impl<C, E> Default for Config<C, E> {
fn default() -> Config<C, E> {
Config {
pool_size: 10,
helper_threads: 3,
test_on_check_out: true,
initialization_fail_fast: true,
connection_timeout_ms: 30 * 1000,
error_handler: Box::new(NopErrorHandler),
connection_customizer: Box::new(NopConnectionCustomizer),
}
}
}
impl<C, E> Config<C, E> {
pub fn builder() -> Builder<C, E> {
Builder::new()
}
pub fn pool_size(&self) -> u32 {
self.pool_size
}
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 connection_timeout_ms(&self) -> u32 {
self.connection_timeout_ms
}
pub fn error_handler(&self) -> &HandleError<E> {
&*self.error_handler
}
pub fn connection_customizer(&self) -> &CustomizeConnection<C, E> {
&*self.connection_customizer
}
}