@@ -1906,28 +1906,36 @@ def re_auth_callback(self, token: TokenInterface):
19061906 for conn in self ._in_use_connections :
19071907 conn .set_re_auth_token (token )
19081908
1909+ def should_update_connection (
1910+ self ,
1911+ conn : "Connection" ,
1912+ address_type_to_match : Literal ["connected" , "configured" ] = "connected" ,
1913+ matching_address : Optional [str ] = None ,
1914+ ) -> bool :
1915+ if address_type_to_match == "connected" :
1916+ if matching_address and conn .getpeername () != matching_address :
1917+ return False
1918+ else :
1919+ if matching_address and conn .host != matching_address :
1920+ return False
1921+ return True
1922+
19091923 def set_maintenance_state_for_connections (
19101924 self ,
19111925 state : "MaintenanceState" ,
19121926 matching_address : Optional [str ] = None ,
19131927 address_type_to_match : Literal ["connected" , "configured" ] = "connected" ,
19141928 ):
19151929 for conn in self ._available_connections :
1916- if address_type_to_match == "connected" :
1917- if matching_address and conn .getpeername () != matching_address :
1918- continue
1919- else :
1920- if matching_address and conn .host != matching_address :
1921- continue
1922- conn .maintenance_state = state
1930+ if self .should_update_connection (
1931+ conn , address_type_to_match , matching_address
1932+ ):
1933+ conn .maintenance_state = state
19231934 for conn in self ._in_use_connections :
1924- if address_type_to_match == "connected" :
1925- if matching_address and conn .getpeername () != matching_address :
1926- continue
1927- else :
1928- if matching_address and conn .host != matching_address :
1929- continue
1930- conn .maintenance_state = state
1935+ if self .should_update_connection (
1936+ conn , address_type_to_match , matching_address
1937+ ):
1938+ conn .maintenance_state = state
19311939
19321940 def set_maintenance_state_in_connection_kwargs (self , state : "MaintenanceState" ):
19331941 self .connection_kwargs ["maintenance_state" ] = state
@@ -2091,23 +2099,17 @@ def update_connections_current_timeout(
20912099 :param include_available_connections: Whether to include available connections in the update.
20922100 """
20932101 for conn in self ._in_use_connections :
2094- if address_type_to_match == "connected" :
2095- if matching_address and conn .getpeername () != matching_address :
2096- continue
2097- else :
2098- if matching_address and conn .host != matching_address :
2099- continue
2100- conn .update_current_socket_timeout (relax_timeout )
2102+ if self .should_update_connection (
2103+ conn , address_type_to_match , matching_address
2104+ ):
2105+ conn .update_current_socket_timeout (relax_timeout )
21012106
21022107 if include_free_connections :
21032108 for conn in self ._available_connections :
2104- if address_type_to_match == "connected" :
2105- if matching_address and conn .getpeername () != matching_address :
2106- continue
2107- else :
2108- if matching_address and conn .host != matching_address :
2109- continue
2110- conn .update_current_socket_timeout (relax_timeout )
2109+ if self .should_update_connection (
2110+ conn , address_type_to_match , matching_address
2111+ ):
2112+ conn .update_current_socket_timeout (relax_timeout )
21112113
21122114 def _update_connection_for_reconnect (
21132115 self ,
@@ -2439,24 +2441,18 @@ def update_connections_current_timeout(
24392441 """
24402442 if include_free_connections :
24412443 for conn in tuple (self ._connections ):
2442- if address_type_to_match == "connected" :
2443- if matching_address and conn .getpeername () != matching_address :
2444- continue
2445- else :
2446- if matching_address and conn .host != matching_address :
2447- continue
2448- conn .update_current_socket_timeout (relax_timeout )
2444+ if self .should_update_connection (
2445+ conn , address_type_to_match , matching_address
2446+ ):
2447+ conn .update_current_socket_timeout (relax_timeout )
24492448 else :
24502449 connections_in_queue = {conn for conn in self .pool .queue if conn }
24512450 for conn in self ._connections :
24522451 if conn not in connections_in_queue :
2453- if address_type_to_match == "connected" :
2454- if matching_address and conn .getpeername () != matching_address :
2455- continue
2456- else :
2457- if matching_address and conn .host != matching_address :
2458- continue
2459- conn .update_current_socket_timeout (relax_timeout )
2452+ if self .should_update_connection (
2453+ conn , address_type_to_match , matching_address
2454+ ):
2455+ conn .update_current_socket_timeout (relax_timeout )
24602456
24612457 def _update_maintenance_events_config_for_connections (
24622458 self , maintenance_events_config
@@ -2508,11 +2504,7 @@ def set_maintenance_state_for_connections(
25082504 address_type_to_match : Literal ["connected" , "configured" ] = "connected" ,
25092505 ):
25102506 for conn in self ._connections :
2511- if address_type_to_match == "connected" :
2512- if matching_address and conn .getpeername () != matching_address :
2513- continue
2514- else :
2515- if matching_address and conn .host != matching_address :
2516- continue
2517-
2518- conn .maintenance_state = state
2507+ if self .should_update_connection (
2508+ conn , address_type_to_match , matching_address
2509+ ):
2510+ conn .maintenance_state = state
0 commit comments