Skip to content

Commit 7b54329

Browse files
utkarshgupta137djc
authored andcommitted
cluster_client: implement proper builder pattern
- implement proper builder pattern by adding `new` & `builder` methods to ClusterClient and `new` & `build` methods to ClusterClientBuilder - deprecate & redirect `open` methods for ClusterClient & ClusterClientBuilder
1 parent 9f6d949 commit 7b54329

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

redis/src/cluster.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! use redis::cluster::ClusterClient;
1313
//!
1414
//! let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
15-
//! let client = ClusterClient::open(nodes).unwrap();
15+
//! let client = ClusterClient::new(nodes).unwrap();
1616
//! let mut connection = client.get_connection().unwrap();
1717
//!
1818
//! let _: () = connection.set("test", "test_data").unwrap();
@@ -27,7 +27,7 @@
2727
//! use redis::cluster::{cluster_pipe, ClusterClient};
2828
//!
2929
//! let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
30-
//! let client = ClusterClient::open(nodes).unwrap();
30+
//! let client = ClusterClient::new(nodes).unwrap();
3131
//! let mut connection = client.get_connection().unwrap();
3232
//!
3333
//! let key = "test";

redis/src/cluster_client.rs

+45-27
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ pub struct ClusterClientBuilder {
1313
}
1414

1515
impl ClusterClientBuilder {
16-
/// Generate the base configuration for new Client.
16+
/// Creates a new `ClusterClientBuilder` with the the provided initial_nodes.
17+
///
18+
/// This is the same as `ClusterClient::builder(initial_nodes)`.
1719
pub fn new<T: IntoConnectionInfo>(initial_nodes: Vec<T>) -> ClusterClientBuilder {
1820
ClusterClientBuilder {
1921
initial_nodes: initial_nodes
@@ -26,16 +28,26 @@ impl ClusterClientBuilder {
2628
}
2729
}
2830

29-
fn build(self) -> RedisResult<ClusterClient> {
31+
/// Creates a new [`ClusterClient`] with the parameters.
32+
///
33+
/// This does not create connections to the Redis Cluster, but only performs some basic checks
34+
/// on the initial nodes' URLs and passwords/usernames.
35+
///
36+
/// # Errors
37+
///
38+
/// Upon failure to parse initial nodes or if the initial nodes have different passwords or
39+
/// usernames, an error is returned.
40+
pub fn build(self) -> RedisResult<ClusterClient> {
3041
let initial_nodes = self.initial_nodes?;
42+
3143
let mut nodes = Vec::with_capacity(initial_nodes.len());
3244
let mut connection_info_password = None::<String>;
3345
let mut connection_info_username = None::<String>;
3446

3547
for (index, info) in initial_nodes.into_iter().enumerate() {
3648
if let ConnectionAddr::Unix(_) = info.addr {
3749
return Err(RedisError::from((ErrorKind::InvalidClientConfig,
38-
"This library cannot use unix socket because Redis's cluster command returns only cluster's IP and port.")));
50+
"This library cannot use unix socket because Redis's cluster command returns only cluster's IP and port.")));
3951
}
4052

4153
if self.password.is_none() {
@@ -92,14 +104,8 @@ impl ClusterClientBuilder {
92104
self
93105
}
94106

95-
/// Builds a [`ClusterClient`]. Despite the name, this does not actually open
96-
/// a connection to Redis Cluster, but will perform some basic checks of the initial
97-
/// nodes' URLs and passwords.
98-
///
99-
/// # Errors
100-
///
101-
/// Upon failure to parse initial nodes or if the initial nodes have different passwords,
102-
/// an error is returned.
107+
/// Use `build()`.
108+
#[deprecated(since = "0.22.0", note = "Use build()")]
103109
pub fn open(self) -> RedisResult<ClusterClient> {
104110
self.build()
105111
}
@@ -121,6 +127,24 @@ pub struct ClusterClient {
121127
}
122128

123129
impl ClusterClient {
130+
/// Creates a `ClusterClient` with the default parameters.
131+
///
132+
/// This does not create connections to the Redis Cluster, but only performs some basic checks
133+
/// on the initial nodes' URLs and passwords/usernames.
134+
///
135+
/// # Errors
136+
///
137+
/// Upon failure to parse initial nodes or if the initial nodes have different passwords or
138+
/// usernames, an error is returned.
139+
pub fn new<T: IntoConnectionInfo>(initial_nodes: Vec<T>) -> RedisResult<ClusterClient> {
140+
ClusterClientBuilder::new(initial_nodes).build()
141+
}
142+
143+
/// Creates a [`ClusterClientBuilder`] with the the provided initial_nodes.
144+
pub fn builder<T: IntoConnectionInfo>(initial_nodes: Vec<T>) -> ClusterClientBuilder {
145+
ClusterClientBuilder::new(initial_nodes)
146+
}
147+
124148
/// Opens connections to Redis Cluster nodes and returns a
125149
/// [`ClusterConnection`].
126150
///
@@ -136,22 +160,16 @@ impl ClusterClient {
136160
)
137161
}
138162

139-
/// Create a [`ClusterClient`] with the default configuration. Despite the name,
140-
/// this does not actually open a connection to Redis Cluster, but only performs some basic
141-
/// checks of the initial nodes' URLs and passwords.
142-
///
143-
/// # Errors
144-
///
145-
/// Upon failure to parse initial nodes or if the initial nodes have different passwords,
146-
/// an error is returned.
163+
/// Use `new()`.
164+
#[deprecated(since = "0.22.0", note = "Use new()")]
147165
pub fn open<T: IntoConnectionInfo>(initial_nodes: Vec<T>) -> RedisResult<ClusterClient> {
148-
ClusterClientBuilder::new(initial_nodes).open()
166+
ClusterClient::new(initial_nodes)
149167
}
150168
}
151169

152170
impl Clone for ClusterClient {
153171
fn clone(&self) -> ClusterClient {
154-
ClusterClient::open(self.initial_nodes.clone()).unwrap()
172+
ClusterClient::new(self.initial_nodes.clone()).unwrap()
155173
}
156174
}
157175

@@ -198,26 +216,26 @@ mod tests {
198216

199217
#[test]
200218
fn give_no_password() {
201-
let client = ClusterClient::open(get_connection_data()).unwrap();
219+
let client = ClusterClient::new(get_connection_data()).unwrap();
202220
assert_eq!(client.password, None);
203221
}
204222

205223
#[test]
206224
fn give_password_by_initial_nodes() {
207-
let client = ClusterClient::open(get_connection_data_with_password()).unwrap();
225+
let client = ClusterClient::new(get_connection_data_with_password()).unwrap();
208226
assert_eq!(client.password, Some("password".to_string()));
209227
}
210228

211229
#[test]
212230
fn give_username_and_password_by_initial_nodes() {
213-
let client = ClusterClient::open(get_connection_data_with_username_and_password()).unwrap();
231+
let client = ClusterClient::new(get_connection_data_with_username_and_password()).unwrap();
214232
assert_eq!(client.password, Some("password".to_string()));
215233
assert_eq!(client.username, Some("user1".to_string()));
216234
}
217235

218236
#[test]
219237
fn give_different_password_by_initial_nodes() {
220-
let result = ClusterClient::open(vec![
238+
let result = ClusterClient::new(vec![
221239
"redis://:[email protected]:6379",
222240
"redis://:[email protected]:6378",
223241
"redis://:[email protected]:6377",
@@ -227,7 +245,7 @@ mod tests {
227245

228246
#[test]
229247
fn give_different_username_by_initial_nodes() {
230-
let result = ClusterClient::open(vec![
248+
let result = ClusterClient::new(vec![
231249
"redis://user1:[email protected]:6379",
232250
"redis://user2:[email protected]:6378",
233251
"redis://user1:[email protected]:6377",
@@ -240,7 +258,7 @@ mod tests {
240258
let client = ClusterClientBuilder::new(get_connection_data_with_password())
241259
.password("pass".to_string())
242260
.username("user1".to_string())
243-
.open()
261+
.build()
244262
.unwrap();
245263
assert_eq!(client.password, Some("pass".to_string()));
246264
assert_eq!(client.username, Some("user1".to_string()));

redis/src/cluster_pipeline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl ClusterPipeline {
9292
///
9393
/// ```rust,no_run
9494
/// # let nodes = vec!["redis://127.0.0.1:6379/"];
95-
/// # let client = redis::cluster::ClusterClient::open(nodes).unwrap();
95+
/// # let client = redis::cluster::ClusterClient::new(nodes).unwrap();
9696
/// # let mut con = client.get_connection().unwrap();
9797
/// let mut pipe = redis::cluster::cluster_pipe();
9898
/// let (k1, k2) : (i32, i32) = pipe
@@ -137,7 +137,7 @@ impl ClusterPipeline {
137137
///
138138
/// ```rust,no_run
139139
/// # let nodes = vec!["redis://127.0.0.1:6379/"];
140-
/// # let client = redis::cluster::ClusterClient::open(nodes).unwrap();
140+
/// # let client = redis::cluster::ClusterClient::new(nodes).unwrap();
141141
/// # let mut con = client.get_connection().unwrap();
142142
/// let mut pipe = redis::cluster::cluster_pipe();
143143
/// let _ : () = pipe.cmd("SET").arg("key_1").arg(42).ignore().query(&mut con).unwrap();

redis/tests/support/cluster.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl TestClusterContext {
232232
.collect(),
233233
);
234234
builder = initializer(builder);
235-
let client = builder.open().unwrap();
235+
let client = builder.build().unwrap();
236236
TestClusterContext { cluster, client }
237237
}
238238

0 commit comments

Comments
 (0)