8
8
from abc import abstractmethod
9
9
from itertools import chain
10
10
from queue import Empty , Full , LifoQueue
11
- from typing import Any , Callable , Dict , List , Optional , Type , TypeVar , Union
11
+ from typing import Any , Callable , Dict , List , Literal , Optional , Type , TypeVar , Union
12
12
from urllib .parse import parse_qs , unquote , urlparse
13
13
14
14
from redis .cache import (
@@ -249,6 +249,13 @@ def maintenance_state(self, state: "MaintenanceState"):
249
249
"""
250
250
pass
251
251
252
+ @abstractmethod
253
+ def getpeername (self ):
254
+ """
255
+ Returns the peer name of the connection.
256
+ """
257
+ pass
258
+
252
259
@abstractmethod
253
260
def mark_for_reconnect (self ):
254
261
"""
@@ -402,6 +409,7 @@ def __init__(
402
409
403
410
if maintenance_events_config and maintenance_events_config .enabled :
404
411
if maintenance_events_pool_handler :
412
+ maintenance_events_pool_handler .set_connection (self )
405
413
self ._parser .set_node_moving_push_handler (
406
414
maintenance_events_pool_handler .handle_event
407
415
)
@@ -484,6 +492,7 @@ def set_parser(self, parser_class):
484
492
def set_maintenance_event_pool_handler (
485
493
self , maintenance_event_pool_handler : MaintenanceEventPoolHandler
486
494
):
495
+ maintenance_event_pool_handler .set_connection (self )
487
496
self ._parser .set_node_moving_push_handler (
488
497
maintenance_event_pool_handler .handle_event
489
498
)
@@ -867,6 +876,11 @@ def maintenance_state(self) -> MaintenanceState:
867
876
def maintenance_state (self , state : "MaintenanceState" ):
868
877
self ._maintenance_state = state
869
878
879
+ def getpeername (self ):
880
+ if not self ._sock :
881
+ return None
882
+ return self ._sock .getpeername ()[0 ]
883
+
870
884
def mark_for_reconnect (self ):
871
885
self ._should_reconnect = True
872
886
@@ -1892,10 +1906,27 @@ def re_auth_callback(self, token: TokenInterface):
1892
1906
for conn in self ._in_use_connections :
1893
1907
conn .set_re_auth_token (token )
1894
1908
1895
- def set_maintenance_state_for_all_connections (self , state : "MaintenanceState" ):
1909
+ def set_maintenance_state_for_connections (
1910
+ self ,
1911
+ state : "MaintenanceState" ,
1912
+ matching_address : Optional [str ] = None ,
1913
+ address_type_to_match : Literal ["connected" , "configured" ] = "connected" ,
1914
+ ):
1896
1915
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
1897
1922
conn .maintenance_state = state
1898
1923
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
1899
1930
conn .maintenance_state = state
1900
1931
1901
1932
def set_maintenance_state_in_connection_kwargs (self , state : "MaintenanceState" ):
@@ -1963,7 +1994,12 @@ def remove_tmp_config_from_connection_kwargs(self):
1963
1994
}
1964
1995
)
1965
1996
1966
- def reset_connections_tmp_settings (self ):
1997
+ def reset_connections_tmp_settings (
1998
+ self ,
1999
+ moving_address : Optional [str ] = None ,
2000
+ reset_host_address : bool = False ,
2001
+ reset_relax_timeout : bool = False ,
2002
+ ):
1967
2003
"""
1968
2004
Restore original settings from temporary configuration for all connections in the pool.
1969
2005
@@ -1978,16 +2014,25 @@ def reset_connections_tmp_settings(self):
1978
2014
"""
1979
2015
with self ._lock :
1980
2016
for conn in self ._available_connections :
2017
+ if moving_address and conn .host != moving_address :
2018
+ continue
1981
2019
conn .reset_tmp_settings (
1982
- reset_host_address = True , reset_relax_timeout = True
2020
+ reset_host_address = reset_host_address ,
2021
+ reset_relax_timeout = reset_relax_timeout ,
1983
2022
)
1984
2023
for conn in self ._in_use_connections :
2024
+ if moving_address and conn .host != moving_address :
2025
+ continue
1985
2026
conn .reset_tmp_settings (
1986
- reset_host_address = True , reset_relax_timeout = True
2027
+ reset_host_address = reset_host_address ,
2028
+ reset_relax_timeout = reset_relax_timeout ,
1987
2029
)
1988
2030
1989
2031
def update_active_connections_for_reconnect (
1990
- self , tmp_host_address : str , tmp_relax_timeout : Optional [float ] = None
2032
+ self ,
2033
+ tmp_host_address : str ,
2034
+ tmp_relax_timeout : Optional [float ] = None ,
2035
+ moving_address_src : Optional [str ] = None ,
1991
2036
):
1992
2037
"""
1993
2038
Mark all active connections for reconnect.
@@ -1999,6 +2044,8 @@ def update_active_connections_for_reconnect(
1999
2044
:param tmp_relax_timeout: The relax timeout to use for the connection.
2000
2045
"""
2001
2046
for conn in self ._in_use_connections :
2047
+ if moving_address_src and conn .getpeername () != moving_address_src :
2048
+ continue
2002
2049
self ._update_connection_for_reconnect (
2003
2050
conn , tmp_host_address , tmp_relax_timeout
2004
2051
)
@@ -2007,6 +2054,7 @@ def disconnect_and_reconfigure_free_connections(
2007
2054
self ,
2008
2055
tmp_host_address : str ,
2009
2056
tmp_relax_timeout : Optional [float ] = None ,
2057
+ moving_address_src : Optional [str ] = None ,
2010
2058
):
2011
2059
"""
2012
2060
Disconnect all free/available connections.
@@ -2019,13 +2067,17 @@ def disconnect_and_reconfigure_free_connections(
2019
2067
"""
2020
2068
2021
2069
for conn in self ._available_connections :
2070
+ if moving_address_src and conn .getpeername () != moving_address_src :
2071
+ continue
2022
2072
self ._disconnect_and_update_connection_for_reconnect (
2023
2073
conn , tmp_host_address , tmp_relax_timeout
2024
2074
)
2025
2075
2026
2076
def update_connections_current_timeout (
2027
2077
self ,
2028
2078
relax_timeout : Optional [float ],
2079
+ matching_address : Optional [str ] = None ,
2080
+ address_type_to_match : Literal ["connected" , "configured" ] = "connected" ,
2029
2081
include_free_connections : bool = False ,
2030
2082
):
2031
2083
"""
@@ -2039,10 +2091,22 @@ def update_connections_current_timeout(
2039
2091
:param include_available_connections: Whether to include available connections in the update.
2040
2092
"""
2041
2093
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
2042
2100
conn .update_current_socket_timeout (relax_timeout )
2043
2101
2044
2102
if include_free_connections :
2045
2103
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
2046
2110
conn .update_current_socket_timeout (relax_timeout )
2047
2111
2048
2112
def _update_connection_for_reconnect (
@@ -2308,7 +2372,10 @@ def disconnect(self):
2308
2372
self ._locked = False
2309
2373
2310
2374
def update_active_connections_for_reconnect (
2311
- self , tmp_host_address : str , tmp_relax_timeout : Optional [float ] = None
2375
+ self ,
2376
+ tmp_host_address : str ,
2377
+ tmp_relax_timeout : Optional [float ] = None ,
2378
+ moving_address_src : Optional [str ] = None ,
2312
2379
):
2313
2380
"""
2314
2381
Mark all active connections for reconnect.
@@ -2323,6 +2390,8 @@ def update_active_connections_for_reconnect(
2323
2390
connections_in_queue = {conn for conn in self .pool .queue if conn }
2324
2391
for conn in self ._connections :
2325
2392
if conn not in connections_in_queue :
2393
+ if moving_address_src and conn .getpeername () != moving_address_src :
2394
+ continue
2326
2395
self ._update_connection_for_reconnect (
2327
2396
conn , tmp_host_address , tmp_relax_timeout
2328
2397
)
@@ -2331,6 +2400,7 @@ def disconnect_and_reconfigure_free_connections(
2331
2400
self ,
2332
2401
tmp_host_address : str ,
2333
2402
tmp_relax_timeout : Optional [Number ] = None ,
2403
+ moving_address_src : Optional [str ] = None ,
2334
2404
):
2335
2405
"""
2336
2406
Disconnect all free/available connections.
@@ -2345,13 +2415,17 @@ def disconnect_and_reconfigure_free_connections(
2345
2415
2346
2416
for conn in existing_connections :
2347
2417
if conn :
2418
+ if moving_address_src and conn .getpeername () != moving_address_src :
2419
+ continue
2348
2420
self ._disconnect_and_update_connection_for_reconnect (
2349
2421
conn , tmp_host_address , tmp_relax_timeout
2350
2422
)
2351
2423
2352
2424
def update_connections_current_timeout (
2353
2425
self ,
2354
2426
relax_timeout : Optional [float ] = None ,
2427
+ matching_address : Optional [str ] = None ,
2428
+ address_type_to_match : Literal ["connected" , "configured" ] = "connected" ,
2355
2429
include_free_connections : bool = False ,
2356
2430
):
2357
2431
"""
@@ -2365,11 +2439,23 @@ def update_connections_current_timeout(
2365
2439
"""
2366
2440
if include_free_connections :
2367
2441
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
2368
2448
conn .update_current_socket_timeout (relax_timeout )
2369
2449
else :
2370
2450
connections_in_queue = {conn for conn in self .pool .queue if conn }
2371
2451
for conn in self ._connections :
2372
2452
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
2373
2459
conn .update_current_socket_timeout (relax_timeout )
2374
2460
2375
2461
def _update_maintenance_events_config_for_connections (
@@ -2387,14 +2473,24 @@ def _update_maintenance_events_configs_for_connections(
2387
2473
conn .set_maintenance_event_pool_handler (maintenance_events_pool_handler )
2388
2474
conn .maintenance_events_config = maintenance_events_pool_handler .config
2389
2475
2390
- def reset_connections_tmp_settings (self ):
2476
+ def reset_connections_tmp_settings (
2477
+ self ,
2478
+ moving_address : Optional [str ] = None ,
2479
+ reset_host_address : bool = False ,
2480
+ reset_relax_timeout : bool = False ,
2481
+ ):
2391
2482
"""
2392
2483
Override base class method to work with BlockingConnectionPool's structure.
2393
2484
2394
2485
Restore original settings from temporary configuration for all connections in the pool.
2395
2486
"""
2396
2487
for conn in tuple (self ._connections ):
2397
- conn .reset_tmp_settings (reset_host_address = True , reset_relax_timeout = True )
2488
+ if moving_address and conn .host != moving_address :
2489
+ continue
2490
+ conn .reset_tmp_settings (
2491
+ reset_host_address = reset_host_address ,
2492
+ reset_relax_timeout = reset_relax_timeout ,
2493
+ )
2398
2494
2399
2495
def set_in_maintenance (self , in_maintenance : bool ):
2400
2496
"""
@@ -2405,6 +2501,18 @@ def set_in_maintenance(self, in_maintenance: bool):
2405
2501
"""
2406
2502
self ._in_maintenance = in_maintenance
2407
2503
2408
- def set_maintenance_state_for_all_connections (self , state : "MaintenanceState" ):
2504
+ def set_maintenance_state_for_connections (
2505
+ self ,
2506
+ state : "MaintenanceState" ,
2507
+ matching_address : Optional [str ] = None ,
2508
+ address_type_to_match : Literal ["connected" , "configured" ] = "connected" ,
2509
+ ):
2409
2510
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
+
2410
2518
conn .maintenance_state = state
0 commit comments