@@ -1906,28 +1906,36 @@ def re_auth_callback(self, token: TokenInterface):
1906
1906
for conn in self ._in_use_connections :
1907
1907
conn .set_re_auth_token (token )
1908
1908
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
+
1909
1923
def set_maintenance_state_for_connections (
1910
1924
self ,
1911
1925
state : "MaintenanceState" ,
1912
1926
matching_address : Optional [str ] = None ,
1913
1927
address_type_to_match : Literal ["connected" , "configured" ] = "connected" ,
1914
1928
):
1915
1929
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
1923
1934
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
1931
1939
1932
1940
def set_maintenance_state_in_connection_kwargs (self , state : "MaintenanceState" ):
1933
1941
self .connection_kwargs ["maintenance_state" ] = state
@@ -2091,23 +2099,17 @@ def update_connections_current_timeout(
2091
2099
:param include_available_connections: Whether to include available connections in the update.
2092
2100
"""
2093
2101
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 )
2101
2106
2102
2107
if include_free_connections :
2103
2108
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 )
2111
2113
2112
2114
def _update_connection_for_reconnect (
2113
2115
self ,
@@ -2439,24 +2441,18 @@ def update_connections_current_timeout(
2439
2441
"""
2440
2442
if include_free_connections :
2441
2443
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 )
2449
2448
else :
2450
2449
connections_in_queue = {conn for conn in self .pool .queue if conn }
2451
2450
for conn in self ._connections :
2452
2451
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 )
2460
2456
2461
2457
def _update_maintenance_events_config_for_connections (
2462
2458
self , maintenance_events_config
@@ -2508,11 +2504,7 @@ def set_maintenance_state_for_connections(
2508
2504
address_type_to_match : Literal ["connected" , "configured" ] = "connected" ,
2509
2505
):
2510
2506
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