forked from aembke/fred.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.rs
287 lines (261 loc) · 8.53 KB
/
builder.rs
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 crate::{
clients::{RedisClient, RedisPool},
error::{RedisError, RedisErrorKind},
prelude::ReconnectPolicy,
types::{ConnectionConfig, PerformanceConfig, RedisConfig, ServerConfig},
};
#[cfg(feature = "subscriber-client")]
use crate::clients::SubscriberClient;
#[cfg(feature = "sentinel-client")]
use crate::{clients::SentinelClient, types::SentinelConfig};
/// A client and pool builder interface.
///
/// ```rust
/// # use std::time::Duration;
/// # use redis_protocol::resp3::types::RespVersion;
/// # use fred::prelude::*;
/// fn example() -> Result<(), RedisError> {
/// // use default values
/// let client = Builder::default_centralized().build()?;
///
/// // or initialize from a URL or config
/// let config = RedisConfig::from_url("redis://localhost:6379/1")?;
/// let mut builder = Builder::from_config(config);
/// // or modify values in place (creating defaults if needed)
/// builder
/// .with_performance_config(|config| {
/// config.auto_pipeline = true;
/// })
/// .with_config(|config| {
/// config.version = RespVersion::RESP3;
/// config.fail_fast = true;
/// })
/// .with_connection_config(|config| {
/// config.tcp = TcpConfig {
/// nodelay: Some(true),
/// ..Default::default()
/// };
/// config.internal_command_timeout = Duration::from_secs(10);
/// });
/// // or overwrite configuration structs in place
/// builder.set_policy(ReconnectPolicy::new_exponential(0, 100, 30_000, 2));
/// builder.set_performance_config(PerformanceConfig::default());
///
/// // reuse the builder as needed to create any kind of client
/// let client = builder.build()?;
/// let pool = builder.build_pool(3)?;
/// let subscriber = builder.build_subscriber_client()?;
///
/// // ...
///
/// Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct Builder {
config: Option<RedisConfig>,
performance: PerformanceConfig,
connection: ConnectionConfig,
policy: Option<ReconnectPolicy>,
#[cfg(feature = "sentinel-client")]
sentinel: Option<SentinelConfig>,
}
impl Default for Builder {
fn default() -> Self {
Builder {
config: None,
performance: PerformanceConfig::default(),
connection: ConnectionConfig::default(),
policy: None,
#[cfg(feature = "sentinel-client")]
sentinel: None,
}
}
}
impl Builder {
/// Create a new builder instance with default config values for a centralized deployment.
pub fn default_centralized() -> Self {
Builder {
config: Some(RedisConfig {
server: ServerConfig::default_centralized(),
..Default::default()
}),
..Default::default()
}
}
/// Create a new builder instance with default config values for a clustered deployment.
pub fn default_clustered() -> Self {
Builder {
config: Some(RedisConfig {
server: ServerConfig::default_clustered(),
..Default::default()
}),
..Default::default()
}
}
/// Create a new builder instance from the provided client config.
pub fn from_config(config: RedisConfig) -> Self {
Builder {
config: Some(config),
..Default::default()
}
}
/// Read the client config.
pub fn get_config(&self) -> Option<&RedisConfig> {
self.config.as_ref()
}
/// Read the reconnection policy.
pub fn get_policy(&self) -> Option<&ReconnectPolicy> {
self.policy.as_ref()
}
/// Read the performance config.
pub fn get_performance_config(&self) -> &PerformanceConfig {
&self.performance
}
/// Read the connection config.
pub fn get_connection_config(&self) -> &ConnectionConfig {
&self.connection
}
/// Read the sentinel client config.
#[cfg(feature = "sentinel-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "sentinel-client")))]
pub fn get_sentinel_config(&self) -> Option<&RedisConfig> {
self.config.as_ref()
}
/// Overwrite the client config on the builder.
pub fn set_config(&mut self, config: RedisConfig) -> &mut Self {
self.config = Some(config);
self
}
/// Overwrite the reconnection policy on the builder.
pub fn set_policy(&mut self, policy: ReconnectPolicy) -> &mut Self {
self.policy = Some(policy);
self
}
/// Overwrite the performance config on the builder.
pub fn set_performance_config(&mut self, config: PerformanceConfig) -> &mut Self {
self.performance = config;
self
}
/// Overwrite the connection config on the builder.
pub fn set_connection_config(&mut self, config: ConnectionConfig) -> &mut Self {
self.connection = config;
self
}
/// Overwrite the sentinel config on the builder.
#[cfg(feature = "sentinel-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "sentinel-client")))]
pub fn set_sentinel_config(&mut self, config: SentinelConfig) -> &mut Self {
self.sentinel = Some(config);
self
}
/// Modify the client config in place, creating a new one with default centralized values first if needed.
pub fn with_config<F>(&mut self, func: F) -> &mut Self
where
F: FnOnce(&mut RedisConfig),
{
if let Some(config) = self.config.as_mut() {
func(config);
} else {
let mut config = RedisConfig::default();
func(&mut config);
self.config = Some(config);
}
self
}
/// Modify the performance config in place, creating a new one with default values first if needed.
pub fn with_performance_config<F>(&mut self, func: F) -> &mut Self
where
F: FnOnce(&mut PerformanceConfig),
{
func(&mut self.performance);
self
}
/// Modify the connection config in place, creating a new one with default values first if needed.
pub fn with_connection_config<F>(&mut self, func: F) -> &mut Self
where
F: FnOnce(&mut ConnectionConfig),
{
func(&mut self.connection);
self
}
/// Modify the sentinel config in place, creating a new one with default values first if needed.
#[cfg(feature = "sentinel-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "sentinel-client")))]
pub fn with_sentinel_config<F>(&mut self, func: F) -> &mut Self
where
F: FnOnce(&mut SentinelConfig),
{
if let Some(config) = self.sentinel.as_mut() {
func(config);
} else {
let mut config = SentinelConfig::default();
func(&mut config);
self.sentinel = Some(config);
}
self
}
/// Create a new client.
pub fn build(&self) -> Result<RedisClient, RedisError> {
if let Some(config) = self.config.as_ref() {
Ok(RedisClient::new(
config.clone(),
Some(self.performance.clone()),
Some(self.connection.clone()),
self.policy.clone(),
))
} else {
Err(RedisError::new(RedisErrorKind::Config, "Missing client configuration."))
}
}
/// Create a new client pool.
pub fn build_pool(&self, size: usize) -> Result<RedisPool, RedisError> {
if let Some(config) = self.config.as_ref() {
RedisPool::new(
config.clone(),
Some(self.performance.clone()),
Some(self.connection.clone()),
self.policy.clone(),
size,
)
} else {
Err(RedisError::new(RedisErrorKind::Config, "Missing client configuration."))
}
}
/// Create a new subscriber client.
#[cfg(feature = "subscriber-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "subscriber-client")))]
pub fn build_subscriber_client(&self) -> Result<SubscriberClient, RedisError> {
if let Some(config) = self.config.as_ref() {
Ok(SubscriberClient::new(
config.clone(),
Some(self.performance.clone()),
Some(self.connection.clone()),
self.policy.clone(),
))
} else {
Err(RedisError::new(RedisErrorKind::Config, "Missing client configuration."))
}
}
/// Create a new sentinel client.
///
/// This is only necessary if callers need to communicate directly with sentinel nodes. Use a
/// `ServerConfig::Sentinel` to interact with Redis servers behind a sentinel layer.
#[cfg(feature = "sentinel-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "sentinel-client")))]
pub fn build_sentinel_client(&self) -> Result<SentinelClient, RedisError> {
if let Some(config) = self.sentinel.as_ref() {
Ok(SentinelClient::new(
config.clone(),
Some(self.performance.clone()),
Some(self.connection.clone()),
self.policy.clone(),
))
} else {
Err(RedisError::new(
RedisErrorKind::Config,
"Missing sentinel client configuration.",
))
}
}
}