@@ -54,122 +54,12 @@ use super::{
54
54
ErrorKind , IntoConnectionInfo , RedisError , RedisResult , Value ,
55
55
} ;
56
56
57
+ pub use crate :: cluster_client:: { ClusterClient , ClusterClientBuilder } ;
58
+
57
59
const SLOT_SIZE : usize = 16384 ;
58
60
59
61
type SlotMap = BTreeMap < u16 , String > ;
60
62
61
- /// This is a ClusterClientBuilder of Redis cluster client.
62
- pub struct ClusterClientBuilder {
63
- initial_nodes : RedisResult < Vec < ConnectionInfo > > ,
64
- readonly : bool ,
65
- password : Option < String > ,
66
- }
67
-
68
- impl ClusterClientBuilder {
69
- /// Generate the base configuration for new Client.
70
- pub fn new < T : IntoConnectionInfo > ( initial_nodes : Vec < T > ) -> ClusterClientBuilder {
71
- ClusterClientBuilder {
72
- initial_nodes : initial_nodes
73
- . into_iter ( )
74
- . map ( |x| x. into_connection_info ( ) )
75
- . collect ( ) ,
76
- readonly : false ,
77
- password : None ,
78
- }
79
- }
80
-
81
- /// Connect to a redis cluster server and return a cluster client.
82
- /// This does not actually open a connection yet but it performs some basic checks on the URL.
83
- /// The password of initial nodes must be the same all.
84
- ///
85
- /// # Errors
86
- ///
87
- /// If it is failed to parse initial_nodes or the initial nodes has different password, an error is returned.
88
- pub fn open ( self ) -> RedisResult < ClusterClient > {
89
- ClusterClient :: open_internal ( self )
90
- }
91
-
92
- /// Set password for new ClusterClient.
93
- pub fn password ( mut self , password : String ) -> ClusterClientBuilder {
94
- self . password = Some ( password) ;
95
- self
96
- }
97
-
98
- /// Set read only mode for new ClusterClient.
99
- /// Default is not read only mode.
100
- /// When it is set to readonly mode, all query use replica nodes except there are no replica nodes.
101
- /// If there are no replica nodes, it use master node.
102
- pub fn readonly ( mut self , readonly : bool ) -> ClusterClientBuilder {
103
- self . readonly = readonly;
104
- self
105
- }
106
- }
107
-
108
- /// This is a Redis cluster client.
109
- pub struct ClusterClient {
110
- initial_nodes : Vec < ConnectionInfo > ,
111
- readonly : bool ,
112
- password : Option < String > ,
113
- }
114
-
115
- impl ClusterClient {
116
- /// Connect to a redis cluster server and return a cluster client.
117
- /// This does not actually open a connection yet but it performs some basic checks on the URL.
118
- /// The password of initial nodes must be the same all.
119
- ///
120
- /// # Errors
121
- ///
122
- /// If it is failed to parse initial_nodes or the initial nodes has different password, an error is returned.
123
- pub fn open < T : IntoConnectionInfo > ( initial_nodes : Vec < T > ) -> RedisResult < ClusterClient > {
124
- ClusterClientBuilder :: new ( initial_nodes) . open ( )
125
- }
126
-
127
- /// Open and get a Redis cluster connection.
128
- ///
129
- /// # Errors
130
- ///
131
- /// If it is failed to open connections and to create slots, an error is returned.
132
- pub fn get_connection ( & self ) -> RedisResult < ClusterConnection > {
133
- ClusterConnection :: new (
134
- self . initial_nodes . clone ( ) ,
135
- self . readonly ,
136
- self . password . clone ( ) ,
137
- )
138
- }
139
-
140
- fn open_internal ( builder : ClusterClientBuilder ) -> RedisResult < ClusterClient > {
141
- let initial_nodes = builder. initial_nodes ?;
142
- let mut nodes = Vec :: with_capacity ( initial_nodes. len ( ) ) ;
143
- let mut connection_info_password = None :: < String > ;
144
-
145
- for ( index, info) in initial_nodes. into_iter ( ) . enumerate ( ) {
146
- if let ConnectionAddr :: Unix ( _) = * info. addr {
147
- return Err ( RedisError :: from ( ( ErrorKind :: InvalidClientConfig ,
148
- "This library cannot use unix socket because Redis's cluster command returns only cluster's IP and port." ) ) ) ;
149
- }
150
-
151
- if builder. password . is_none ( ) {
152
- if index == 0 {
153
- connection_info_password = info. passwd . clone ( ) ;
154
- } else if connection_info_password != info. passwd {
155
- return Err ( RedisError :: from ( (
156
- ErrorKind :: InvalidClientConfig ,
157
- "Cannot use different password among initial nodes." ,
158
- ) ) ) ;
159
- }
160
- }
161
-
162
- nodes. push ( info) ;
163
- }
164
-
165
- Ok ( ClusterClient {
166
- initial_nodes : nodes,
167
- readonly : builder. readonly ,
168
- password : builder. password . or ( connection_info_password) ,
169
- } )
170
- }
171
- }
172
-
173
63
/// This is a connection of Redis cluster.
174
64
pub struct ClusterConnection {
175
65
initial_nodes : Vec < ConnectionInfo > ,
@@ -181,7 +71,7 @@ pub struct ClusterConnection {
181
71
}
182
72
183
73
impl ClusterConnection {
184
- fn new (
74
+ pub ( crate ) fn new (
185
75
initial_nodes : Vec < ConnectionInfo > ,
186
76
readonly : bool ,
187
77
password : Option < String > ,
@@ -610,12 +500,6 @@ impl ConnectionLike for ClusterConnection {
610
500
}
611
501
}
612
502
613
- impl Clone for ClusterClient {
614
- fn clone ( & self ) -> ClusterClient {
615
- ClusterClient :: open ( self . initial_nodes . clone ( ) ) . unwrap ( )
616
- }
617
- }
618
-
619
503
fn connect < T : IntoConnectionInfo > (
620
504
info : T ,
621
505
readonly : bool ,
@@ -861,61 +745,6 @@ fn get_slots(connection: &mut Connection) -> RedisResult<Vec<Slot>> {
861
745
#[ cfg( test) ]
862
746
mod tests {
863
747
use super :: get_hashtag;
864
- use super :: { ClusterClient , ClusterClientBuilder } ;
865
- use super :: { ConnectionInfo , IntoConnectionInfo } ;
866
-
867
- fn get_connection_data ( ) -> Vec < ConnectionInfo > {
868
- vec ! [
869
- "redis://127.0.0.1:6379" . into_connection_info( ) . unwrap( ) ,
870
- "redis://127.0.0.1:6378" . into_connection_info( ) . unwrap( ) ,
871
- "redis://127.0.0.1:6377" . into_connection_info( ) . unwrap( ) ,
872
- ]
873
- }
874
-
875
- fn get_connection_data_with_password ( ) -> Vec < ConnectionInfo > {
876
- vec ! [
877
- "redis://:password@127.0.0.1:6379"
878
- . into_connection_info( )
879
- . unwrap( ) ,
880
- "redis://:password@127.0.0.1:6378"
881
- . into_connection_info( )
882
- . unwrap( ) ,
883
- "redis://:password@127.0.0.1:6377"
884
- . into_connection_info( )
885
- . unwrap( ) ,
886
- ]
887
- }
888
-
889
- #[ test]
890
- fn give_no_password ( ) {
891
- let client = ClusterClient :: open ( get_connection_data ( ) ) . unwrap ( ) ;
892
- assert_eq ! ( client. password, None ) ;
893
- }
894
-
895
- #[ test]
896
- fn give_password_by_initial_nodes ( ) {
897
- let client = ClusterClient :: open ( get_connection_data_with_password ( ) ) . unwrap ( ) ;
898
- assert_eq ! ( client. password, Some ( "password" . to_string( ) ) ) ;
899
- }
900
-
901
- #[ test]
902
- fn give_different_password_by_initial_nodes ( ) {
903
- let result = ClusterClient :: open ( vec ! [
904
- "redis://:password1@127.0.0.1:6379" ,
905
- "redis://:password2@127.0.0.1:6378" ,
906
- "redis://:password3@127.0.0.1:6377" ,
907
- ] ) ;
908
- assert ! ( result. is_err( ) ) ;
909
- }
910
-
911
- #[ test]
912
- fn give_password_by_method ( ) {
913
- let client = ClusterClientBuilder :: new ( get_connection_data_with_password ( ) )
914
- . password ( "pass" . to_string ( ) )
915
- . open ( )
916
- . unwrap ( ) ;
917
- assert_eq ! ( client. password, Some ( "pass" . to_string( ) ) ) ;
918
- }
919
748
920
749
#[ test]
921
750
fn test_get_hashtag ( ) {
0 commit comments