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
use std::sync::Arc;
use log::{debug, info, warn};
use crate::connection_factory::ConnectionFactory;
use crate::error::InitializationResult;
use crate::executor_flavour::ExecutorFlavour;
use crate::instrumentation::{Instrumentation, InstrumentationFlavour};
use crate::pools::{PoolPerNode, SinglePool};
use crate::redis_rs::RedisRsFactory;
use crate::{RedisPool, RedisPoolFlavour};
use super::*;
pub use crate::activation_order::ActivationOrder;
pub use crate::backoff_strategy::BackoffStrategy;
pub use crate::error::Error;
pub struct Builder {
config: Config,
executor_flavour: ExecutorFlavour,
instrumentation: InstrumentationFlavour,
}
impl Default for Builder {
fn default() -> Self {
Self {
config: Config::default(),
executor_flavour: ExecutorFlavour::Runtime,
instrumentation: InstrumentationFlavour::NoInstrumentation,
}
}
}
impl Builder {
pub fn desired_pool_size(mut self, v: usize) -> Self {
self.config.desired_pool_size = v;
self
}
pub fn default_checkout_mode<T: Into<DefaultPoolCheckoutMode>>(mut self, v: T) -> Self {
self.config.default_checkout_mode = v.into();
self
}
pub fn backoff_strategy(mut self, v: BackoffStrategy) -> Self {
self.config.backoff_strategy = v;
self
}
pub fn reservation_limit(mut self, v: usize) -> Self {
self.config.reservation_limit = v;
self
}
pub fn activation_order(mut self, v: ActivationOrder) -> Self {
self.config.activation_order = v;
self
}
pub fn min_required_nodes(mut self, v: usize) -> Self {
self.config.min_required_nodes = v;
self
}
pub fn connect_to_nodes(mut self, v: Vec<String>) -> Self {
self.config.connect_to_nodes = v;
self
}
pub fn connect_to_node<T: Into<String>>(mut self, v: T) -> Self {
self.config.connect_to_nodes = vec![v.into()];
self
}
pub fn pool_multiplier(mut self, v: u32) -> Self {
self.config.pool_multiplier = v;
self
}
pub fn checkout_queue_size(mut self, v: usize) -> Self {
self.config.checkout_queue_size = v;
self
}
pub fn retry_on_checkout_limit(mut self, v: bool) -> Self {
self.config.retry_on_checkout_limit = v;
self
}
pub fn default_command_timeout<T: Into<DefaultCommandTimeout>>(mut self, v: T) -> Self {
self.config.default_command_timeout = v.into();
self
}
pub fn task_executor(mut self, handle: ::tokio::runtime::Handle) -> Self {
self.executor_flavour = ExecutorFlavour::TokioTaskExecutor(handle);
self
}
pub fn instrumented<I>(mut self, instrumentation: I) -> Self
where
I: Instrumentation + Send + Sync + 'static,
{
self.instrumentation = InstrumentationFlavour::Custom(Arc::new(instrumentation));
self
}
#[cfg(feature = "metrix")]
pub fn with_mounted_metrix_instrumentation<A: metrix::processor::AggregatesProcessors>(
mut self,
aggregates_processors: &mut A,
config: crate::instrumentation::MetrixConfig,
) -> Self {
let instrumentation =
crate::instrumentation::MetrixInstrumentation::new(aggregates_processors, config);
self.instrumentation = InstrumentationFlavour::Metrix(instrumentation);
self
}
#[cfg(feature = "metrix")]
pub fn with_metrix_instrumentation(
mut self,
instrumentation: crate::instrumentation::MetrixInstrumentation,
) -> Self {
self.instrumentation = InstrumentationFlavour::Metrix(instrumentation);
self
}
pub fn update_from_environment(&mut self, prefix: Option<&str>) -> InitializationResult<()> {
self.config.update_from_environment(prefix)?;
Ok(())
}
pub fn updated_from_environment(mut self, prefix: Option<&str>) -> InitializationResult<Self> {
self.config.update_from_environment(prefix)?;
Ok(self)
}
pub fn finish<CF, F>(
self,
connection_factory: F,
) -> InitializationResult<RedisPool<CF::Connection>>
where
F: Fn(String) -> InitializationResult<CF>,
CF: ConnectionFactory + Send + Sync + 'static,
{
let config = self.config;
if config.pool_multiplier == 0 {
return Err(Error::message("pool_multiplier must not be zero"));
}
if config.checkout_queue_size == 0 {
return Err(Error::message("checkout_queue_size must be greater than 0"));
}
if config.connect_to_nodes.len() < config.min_required_nodes {
return Err(Error::message(format!(
"There must be at least {} node(s) defined. There are only {} defined.",
config.min_required_nodes,
config.connect_to_nodes.len()
)));
}
if config.connect_to_nodes.is_empty() {
warn!("Creating a pool with no nodes");
return Ok(create_no_pool(self.instrumentation));
}
info!("Configuration: {:?}", config);
let create_single_pool = config.connect_to_nodes.len() == 1 && config.pool_multiplier == 1;
let default_checkout_mode = config.default_checkout_mode;
let retry_on_checkout_limit = config.retry_on_checkout_limit;
let default_command_timeout = config.default_command_timeout;
let flavour = if create_single_pool {
debug!("Create single pool for 1 node",);
RedisPoolFlavour::Single(SinglePool::new(
config,
connection_factory,
self.executor_flavour,
self.instrumentation,
)?)
} else {
debug!(
"Create multiple pools. One for each of the {} nodes",
config.connect_to_nodes.len()
);
RedisPoolFlavour::PerNode(PoolPerNode::new(
config,
connection_factory,
self.executor_flavour,
self.instrumentation,
)?)
};
Ok(RedisPool {
flavour,
default_checkout_mode,
retry_on_checkout_limit,
default_command_timeout,
})
}
pub fn finish_redis_rs(self) -> InitializationResult<RedisPool> {
self.finish(RedisRsFactory::new)
}
}
fn create_no_pool<T: Poolable>(_instrumentation: InstrumentationFlavour) -> RedisPool<T> {
RedisPool::no_pool()
}