@@ -431,7 +431,20 @@ def set_parser(self, parser_class):
431
431
def set_maintenance_event_pool_handler (
432
432
self , maintenance_event_pool_handler : MaintenanceEventPoolHandler
433
433
):
434
- self ._parser .set_node_moving_push_handler (maintenance_event_pool_handler )
434
+ self ._parser .set_node_moving_push_handler (
435
+ maintenance_event_pool_handler .handle_event
436
+ )
437
+
438
+ # Initialize maintenance event connection handler if it doesn't exist
439
+ if not hasattr (self , "_maintenance_event_connection_handler" ):
440
+ self ._maintenance_event_connection_handler = (
441
+ MaintenanceEventConnectionHandler (
442
+ self , maintenance_event_pool_handler .config
443
+ )
444
+ )
445
+ self ._parser .set_maintenance_push_handler (
446
+ self ._maintenance_event_connection_handler .handle_event
447
+ )
435
448
436
449
def connect (self ):
437
450
"Connects to the Redis server if not already connected"
@@ -802,10 +815,6 @@ def should_reconnect(self):
802
815
def update_current_socket_timeout (self , relax_timeout : Optional [float ] = None ):
803
816
if self ._sock :
804
817
timeout = relax_timeout if relax_timeout != - 1 else self .socket_timeout
805
- logging .debug (
806
- f"***** Connection --> Updating timeout for { self ._sock .getpeername ()} "
807
- f" to timeout { timeout } ; relax_timeout: { relax_timeout } "
808
- )
809
818
self ._sock .settimeout (timeout )
810
819
self .update_parser_buffer_timeout (timeout )
811
820
@@ -858,10 +867,6 @@ def _connect(self):
858
867
# ipv4/ipv6, but we want to set options prior to calling
859
868
# socket.connect()
860
869
err = None
861
- if self .tmp_host_address is not None :
862
- logging .debug (
863
- f"***** Connection --> Using tmp_host_address: { self .tmp_host_address } "
864
- )
865
870
host = self .tmp_host_address or self .host
866
871
867
872
for res in socket .getaddrinfo (
@@ -882,31 +887,18 @@ def _connect(self):
882
887
883
888
# set the socket_connect_timeout before we connect
884
889
if self .tmp_relax_timeout != - 1 :
885
- logging .debug (
886
- f"***** Connection connect --> Using relax_timeout: { self .tmp_relax_timeout } "
887
- )
888
890
sock .settimeout (self .tmp_relax_timeout )
889
891
else :
890
- logging .debug (
891
- f"***** Connection connect --> Using default socket_connect_timeout: { self .socket_connect_timeout } "
892
- )
893
892
sock .settimeout (self .socket_connect_timeout )
894
893
895
894
# connect
896
895
sock .connect (socket_address )
897
896
898
897
# set the socket_timeout now that we're connected
899
898
if self .tmp_relax_timeout != - 1 :
900
- logging .debug (
901
- f"***** Connection --> Using relax_timeout: { self .tmp_relax_timeout } "
902
- )
903
899
sock .settimeout (self .tmp_relax_timeout )
904
900
else :
905
- logging .debug (
906
- f"***** Connection --> Using default socket_timeout: { self .socket_timeout } "
907
- )
908
901
sock .settimeout (self .socket_timeout )
909
- logging .debug (f"Connected to { sock .getpeername ()} " )
910
902
return sock
911
903
912
904
except OSError as _ :
@@ -1606,14 +1598,10 @@ def _update_maintenance_events_configs_for_connections(
1606
1598
):
1607
1599
with self ._lock :
1608
1600
for conn in self ._available_connections :
1609
- conn .set_maintenance_events_pool_handler (
1610
- maintenance_events_pool_handler
1611
- )
1601
+ conn .set_maintenance_event_pool_handler (maintenance_events_pool_handler )
1612
1602
conn .maintenance_events_config = maintenance_events_pool_handler .config
1613
1603
for conn in self ._in_use_connections :
1614
- conn .set_maintenance_events_pool_handler (
1615
- maintenance_events_pool_handler
1616
- )
1604
+ conn .set_maintenance_event_pool_handler (maintenance_events_pool_handler )
1617
1605
conn .maintenance_events_config = maintenance_events_pool_handler .config
1618
1606
1619
1607
def reset (self ) -> None :
@@ -1755,9 +1743,6 @@ def release(self, connection: "Connection") -> None:
1755
1743
1756
1744
if self .owns_connection (connection ):
1757
1745
if connection .should_reconnect ():
1758
- logging .debug (
1759
- f"***** Pool--> disconnecting in release { connection ._sock .getpeername ()} "
1760
- )
1761
1746
connection .disconnect ()
1762
1747
self ._available_connections .append (connection )
1763
1748
self ._event_dispatcher .dispatch (
@@ -1917,21 +1902,13 @@ def update_connections_current_timeout(
1917
1902
If -1 is provided - the relax timeout is disabled.
1918
1903
:param include_available_connections: Whether to include available connections in the update.
1919
1904
"""
1920
- logging .debug (f"***** Pool --> Updating timeouts. New value: { relax_timeout } " )
1921
- start_time = time .time ()
1922
-
1923
1905
for conn in self ._in_use_connections :
1924
1906
self ._update_connection_timeout (conn , relax_timeout )
1925
1907
1926
1908
if include_free_connections :
1927
1909
for conn in self ._available_connections :
1928
1910
self ._update_connection_timeout (conn , relax_timeout )
1929
1911
1930
- execution_time_us = (time .time () - start_time ) * 1000000
1931
- logging .error (
1932
- f"###### TIMEOUTS execution time: { execution_time_us :.0f} microseconds"
1933
- )
1934
-
1935
1912
def _update_connection_for_reconnect (
1936
1913
self ,
1937
1914
connection : "Connection" ,
@@ -2021,6 +1998,8 @@ def __init__(
2021
1998
):
2022
1999
self .queue_class = queue_class
2023
2000
self .timeout = timeout
2001
+ self ._in_maintenance = False
2002
+ self ._locked = False
2024
2003
super ().__init__ (
2025
2004
connection_class = connection_class ,
2026
2005
max_connections = max_connections ,
@@ -2029,7 +2008,10 @@ def __init__(
2029
2008
2030
2009
def reset (self ):
2031
2010
# Create and fill up a thread safe queue with ``None`` values.
2032
- with self ._lock :
2011
+ try :
2012
+ if self ._in_maintenance :
2013
+ self ._lock .acquire ()
2014
+ self ._locked = True
2033
2015
self .pool = self .queue_class (self .max_connections )
2034
2016
while True :
2035
2017
try :
@@ -2040,6 +2022,13 @@ def reset(self):
2040
2022
# Keep a list of actual connection instances so that we can
2041
2023
# disconnect them later.
2042
2024
self ._connections = []
2025
+ finally :
2026
+ if self ._locked :
2027
+ try :
2028
+ self ._lock .release ()
2029
+ except Exception :
2030
+ pass
2031
+ self ._locked = False
2043
2032
2044
2033
# this must be the last operation in this method. while reset() is
2045
2034
# called when holding _fork_lock, other threads in this process
@@ -2054,7 +2043,10 @@ def reset(self):
2054
2043
2055
2044
def make_connection (self ):
2056
2045
"Make a fresh connection."
2057
- with self ._lock :
2046
+ try :
2047
+ if self ._in_maintenance :
2048
+ self ._lock .acquire ()
2049
+ self ._locked = True
2058
2050
if self .cache is not None :
2059
2051
connection = CacheProxyConnection (
2060
2052
self .connection_class (** self .connection_kwargs ),
@@ -2066,6 +2058,13 @@ def make_connection(self):
2066
2058
2067
2059
self ._connections .append (connection )
2068
2060
return connection
2061
+ finally :
2062
+ if self ._locked :
2063
+ try :
2064
+ self ._lock .release ()
2065
+ except Exception :
2066
+ pass
2067
+ self ._locked = False
2069
2068
2070
2069
@deprecated_args (
2071
2070
args_to_warn = ["*" ],
@@ -2090,7 +2089,10 @@ def get_connection(self, command_name=None, *keys, **options):
2090
2089
# Try and get a connection from the pool. If one isn't available within
2091
2090
# self.timeout then raise a ``ConnectionError``.
2092
2091
connection = None
2093
- with self ._lock :
2092
+ try :
2093
+ if self ._in_maintenance :
2094
+ self ._lock .acquire ()
2095
+ self ._locked = True
2094
2096
try :
2095
2097
connection = self .pool .get (block = True , timeout = self .timeout )
2096
2098
except Empty :
@@ -2102,6 +2104,13 @@ def get_connection(self, command_name=None, *keys, **options):
2102
2104
# a new connection to add to the pool.
2103
2105
if connection is None :
2104
2106
connection = self .make_connection ()
2107
+ finally :
2108
+ if self ._locked :
2109
+ try :
2110
+ self ._lock .release ()
2111
+ except Exception :
2112
+ pass
2113
+ self ._locked = False
2105
2114
2106
2115
try :
2107
2116
# ensure this connection is connected to Redis
@@ -2130,7 +2139,10 @@ def release(self, connection):
2130
2139
# Make sure we haven't changed process.
2131
2140
self ._checkpid ()
2132
2141
2133
- with self ._lock :
2142
+ try :
2143
+ if self ._in_maintenance :
2144
+ self ._lock .acquire ()
2145
+ self ._locked = True
2134
2146
if not self .owns_connection (connection ):
2135
2147
# pool doesn't own this connection. do not add it back
2136
2148
# to the pool. instead add a None value which is a placeholder
@@ -2140,24 +2152,39 @@ def release(self, connection):
2140
2152
self .pool .put_nowait (None )
2141
2153
return
2142
2154
if connection .should_reconnect ():
2143
- logging .debug (
2144
- f"***** Blocking Pool--> disconnecting in release { connection ._sock .getpeername ()} "
2145
- )
2146
2155
connection .disconnect ()
2147
2156
# Put the connection back into the pool.
2148
2157
try :
2158
+ print ("Releasing connection - in the pool" )
2149
2159
self .pool .put_nowait (connection )
2150
2160
except Full :
2151
2161
# perhaps the pool has been reset() after a fork? regardless,
2152
2162
# we don't want this connection
2153
2163
pass
2164
+ finally :
2165
+ if self ._locked :
2166
+ try :
2167
+ self ._lock .release ()
2168
+ except Exception :
2169
+ pass
2170
+ self ._locked = False
2154
2171
2155
2172
def disconnect (self ):
2156
2173
"Disconnects all connections in the pool."
2157
2174
self ._checkpid ()
2158
- with self ._lock :
2175
+ try :
2176
+ if self ._in_maintenance :
2177
+ self ._lock .acquire ()
2178
+ self ._locked = True
2159
2179
for connection in self ._connections :
2160
2180
connection .disconnect ()
2181
+ finally :
2182
+ if self ._locked :
2183
+ try :
2184
+ self ._lock .release ()
2185
+ except Exception :
2186
+ pass
2187
+ self ._locked = False
2161
2188
2162
2189
def update_active_connections_for_reconnect (
2163
2190
self ,
@@ -2236,3 +2263,7 @@ def _update_maintenance_events_configs_for_connections(
2236
2263
conn .maintenance_events_config = (
2237
2264
maintenance_events_pool_handler .config
2238
2265
)
2266
+
2267
+ def set_in_maintenance (self , in_maintenance : bool ):
2268
+ """Set the maintenance mode for the connection pool."""
2269
+ self ._in_maintenance = in_maintenance
0 commit comments