1- use std:: net:: { IpAddr , SocketAddr } ;
1+ use std:: net:: SocketAddr ;
22
3- use super :: { connections_container:: ClusterNode , Connect } ;
3+ use super :: {
4+ connections_container:: { ClusterNode , ConnectionWithIp } ,
5+ Connect ,
6+ } ;
47use crate :: {
58 aio:: { ConnectionLike , Runtime } ,
69 cluster:: get_connection_info,
@@ -30,17 +33,9 @@ pub enum RefreshConnectionType {
3033 AllConnections ,
3134}
3235
33- fn to_future < C > ( conn : C ) -> ConnectionFuture < C >
34- where
35- C : Clone + Send + ' static ,
36- {
37- async { conn } . boxed ( ) . shared ( )
38- }
39-
4036fn failed_management_connection < C > (
4137 addr : & str ,
42- user_conn : ConnectionFuture < C > ,
43- ip : Option < IpAddr > ,
38+ user_conn : ConnectionWithIp < ConnectionFuture < C > > ,
4439 err : RedisError ,
4540) -> ConnectAndCheckResult < C >
4641where
5146 addr, err
5247 ) ;
5348 ConnectAndCheckResult :: ManagementConnectionFailed {
54- node : AsyncClusterNode :: new ( user_conn, None , ip ) ,
49+ node : AsyncClusterNode :: new ( user_conn, None ) ,
5550 err,
5651 }
5752}
@@ -90,17 +85,16 @@ where
9085}
9186
9287fn create_async_node < C > (
93- user_conn : C ,
94- management_conn : Option < C > ,
95- ip : Option < IpAddr > ,
88+ user_conn : ConnectionWithIp < C > ,
89+ management_conn : Option < ConnectionWithIp < C > > ,
9690) -> AsyncClusterNode < C >
9791where
9892 C : ConnectionLike + Connect + Send + Sync + ' static + Clone ,
9993{
100- let user_conn = to_future ( user_conn ) ;
101- let management_conn = management_conn . map ( to_future ) ;
102-
103- AsyncClusterNode :: new ( user_conn , management_conn , ip )
94+ AsyncClusterNode :: new (
95+ user_conn . into_future ( ) ,
96+ management_conn . map ( |conn| conn . into_future ( ) ) ,
97+ )
10498}
10599
106100pub ( crate ) async fn connect_and_check_all_connections < C > (
@@ -126,25 +120,23 @@ where
126120 {
127121 ( Ok ( conn_1) , Ok ( conn_2) ) => {
128122 // Both connections were successfully established
129- let ( mut user_conn, ip ) : ( C , Option < IpAddr > ) = conn_1;
130- let ( mut management_conn, _ip ) : ( C , Option < IpAddr > ) = conn_2;
131- if let Err ( err) = setup_user_connection ( & mut user_conn, params) . await {
123+ let mut user_conn: ConnectionWithIp < C > = conn_1;
124+ let mut management_conn: ConnectionWithIp < C > = conn_2;
125+ if let Err ( err) = setup_user_connection ( & mut user_conn. conn , params) . await {
132126 return err. into ( ) ;
133127 }
134- match setup_management_connection ( & mut management_conn) . await {
128+ match setup_management_connection ( & mut management_conn. conn ) . await {
135129 Ok ( _) => ConnectAndCheckResult :: Success ( create_async_node (
136130 user_conn,
137131 Some ( management_conn) ,
138- ip,
139132 ) ) ,
140- Err ( err) => failed_management_connection ( addr, to_future ( user_conn) , ip , err) ,
133+ Err ( err) => failed_management_connection ( addr, user_conn. into_future ( ) , err) ,
141134 }
142135 }
143- ( Ok ( conn ) , Err ( err) ) | ( Err ( err) , Ok ( conn ) ) => {
136+ ( Ok ( mut connection ) , Err ( err) ) | ( Err ( err) , Ok ( mut connection ) ) => {
144137 // Only a single connection was successfully established. Use it for the user connection
145- let ( mut user_conn, ip) : ( C , Option < IpAddr > ) = conn;
146- match setup_user_connection ( & mut user_conn, params) . await {
147- Ok ( _) => failed_management_connection ( addr, to_future ( user_conn) , ip, err) ,
138+ match setup_user_connection ( & mut connection. conn , params) . await {
139+ Ok ( _) => failed_management_connection ( addr, connection. into_future ( ) , err) ,
148140 Err ( err) => err. into ( ) ,
149141 }
150142 }
@@ -173,24 +165,16 @@ where
173165 C : ConnectionLike + Connect + Send + Sync + ' static + Clone ,
174166{
175167 match create_connection :: < C > ( addr, params. clone ( ) , socket_addr, None , true ) . await {
176- Err ( conn_err) => {
177- failed_management_connection ( addr, prev_node. user_connection , prev_node. ip , conn_err)
178- }
168+ Err ( conn_err) => failed_management_connection ( addr, prev_node. user_connection , conn_err) ,
179169
180- Ok ( mut conn) => {
181- if let Err ( err) = setup_management_connection ( & mut conn. 0 ) . await {
182- return failed_management_connection (
183- addr,
184- prev_node. user_connection ,
185- prev_node. ip ,
186- err,
187- ) ;
170+ Ok ( mut connection) => {
171+ if let Err ( err) = setup_management_connection ( & mut connection. conn ) . await {
172+ return failed_management_connection ( addr, prev_node. user_connection , err) ;
188173 }
189174
190175 ConnectAndCheckResult :: Success ( ClusterNode {
191176 user_connection : prev_node. user_connection ,
192- ip : prev_node. ip ,
193- management_connection : Some ( to_future ( conn. 0 ) ) ,
177+ management_connection : Some ( connection. into_future ( ) ) ,
194178 } )
195179 }
196180 }
@@ -263,7 +247,7 @@ where
263247{
264248 match conn_type {
265249 RefreshConnectionType :: OnlyUserConnection => {
266- let ( user_conn, ip ) = match create_and_setup_user_connection (
250+ let user_conn = match create_and_setup_user_connection (
267251 addr,
268252 params. clone ( ) ,
269253 socket_addr,
@@ -274,23 +258,8 @@ where
274258 Ok ( tuple) => tuple,
275259 Err ( err) => return err. into ( ) ,
276260 } ;
277- if let Some ( node) = node {
278- let mut management_conn = match node. management_connection {
279- Some ( ref conn) => Some ( conn. clone ( ) . await ) ,
280- None => None ,
281- } ;
282- if ip != node. ip {
283- // New IP was found, refresh the management connection too
284- management_conn =
285- create_and_setup_management_connection ( addr, params, socket_addr)
286- . await
287- . ok ( )
288- . map ( |( conn, _ip) : ( C , Option < IpAddr > ) | conn) ;
289- }
290- create_async_node ( user_conn, management_conn, ip) . into ( )
291- } else {
292- create_async_node ( user_conn, None , ip) . into ( )
293- }
261+ let management_conn = node. and_then ( |node| node. management_connection ) ;
262+ AsyncClusterNode :: new ( user_conn. into_future ( ) , management_conn) . into ( )
294263 }
295264 RefreshConnectionType :: OnlyManagementConnection => {
296265 // Refreshing only the management connection requires the node to exist alongside a user connection. Otherwise, refresh all connections.
@@ -314,28 +283,14 @@ async fn create_and_setup_user_connection<C>(
314283 params : ClusterParams ,
315284 socket_addr : Option < SocketAddr > ,
316285 push_sender : Option < mpsc:: UnboundedSender < PushInfo > > ,
317- ) -> RedisResult < ( C , Option < IpAddr > ) >
286+ ) -> RedisResult < ConnectionWithIp < C > >
318287where
319288 C : ConnectionLike + Connect + Send + ' static ,
320289{
321- let ( mut conn , ip ) : ( C , Option < IpAddr > ) =
290+ let mut connection : ConnectionWithIp < C > =
322291 create_connection ( node, params. clone ( ) , socket_addr, push_sender, false ) . await ?;
323- setup_user_connection ( & mut conn, params) . await ?;
324- Ok ( ( conn, ip) )
325- }
326-
327- async fn create_and_setup_management_connection < C > (
328- node : & str ,
329- params : ClusterParams ,
330- socket_addr : Option < SocketAddr > ,
331- ) -> RedisResult < ( C , Option < IpAddr > ) >
332- where
333- C : ConnectionLike + Connect + Send + ' static ,
334- {
335- let ( mut conn, ip) : ( C , Option < IpAddr > ) =
336- create_connection ( node, params. clone ( ) , socket_addr, None , true ) . await ?;
337- setup_management_connection ( & mut conn) . await ?;
338- Ok ( ( conn, ip) )
292+ setup_user_connection ( & mut connection. conn , params) . await ?;
293+ Ok ( connection)
339294}
340295
341296async fn setup_user_connection < C > ( conn : & mut C , params : ClusterParams ) -> RedisResult < ( ) >
@@ -373,7 +328,7 @@ async fn create_connection<C>(
373328 socket_addr : Option < SocketAddr > ,
374329 push_sender : Option < mpsc:: UnboundedSender < PushInfo > > ,
375330 is_management : bool ,
376- ) -> RedisResult < ( C , Option < IpAddr > ) >
331+ ) -> RedisResult < ConnectionWithIp < C > >
377332where
378333 C : ConnectionLike + Connect + Send + ' static ,
379334{
@@ -392,6 +347,7 @@ where
392347 if !is_management { push_sender } else { None } ,
393348 )
394349 . await
350+ . map ( |conn| conn. into ( ) )
395351}
396352
397353/// The function returns None if the checked connection/s are healthy. Otherwise, it returns the type of the unhealthy connection/s.
@@ -430,7 +386,7 @@ where
430386 return false ;
431387 }
432388 match node. management_connection. clone( ) {
433- Some ( conn ) => check( conn, timeout, "management" ) . await ,
389+ Some ( connection ) => check( connection . conn, timeout, "management" ) . await ,
434390 None => {
435391 warn!( "The management connection for node {} isn't set" , address) ;
436392 true
@@ -441,7 +397,7 @@ where
441397 if !check_user_connection {
442398 return false ;
443399 }
444- let conn = node. user_connection. clone( ) ;
400+ let conn = node. user_connection. conn . clone( ) ;
445401 check( conn, timeout, "user" ) . await
446402 } ,
447403 ) ;
0 commit comments