Skip to content

Commit

Permalink
ConnectionPool.disconnect now accepts arg to disconnect only idle conns
Browse files Browse the repository at this point in the history
When the sentinel's master address is changed, disconnect all idle connections
in the pool.
  • Loading branch information
andymccurdy committed Jun 1, 2020
1 parent 6518c08 commit 811cdd0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
20 changes: 15 additions & 5 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,13 +1250,23 @@ def release(self, connection):
def owns_connection(self, connection):
return connection.pid == self.pid

def disconnect(self):
"Disconnects all connections in the pool"
def disconnect(self, inuse_connections=True):
"""
Disconnects connections in the pool
If ``inuse_connections`` is True, disconnect connections that are
current in use, potentially by other threads. Otherwise only disconnect
connections that are idle in the pool.
"""
self._checkpid()
with self._lock:
all_conns = chain(self._available_connections,
self._in_use_connections)
for connection in all_conns:
if inuse_connections:
connections = chain(self._available_connections,
self._in_use_connections)
else:
connections = self._available_connections

for connection in connections:
connection.disconnect()


Expand Down
3 changes: 3 additions & 0 deletions redis/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def get_master_address(self):
if self.is_master:
if self.master_address != master_address:
self.master_address = master_address
# disconnect any idle connections so that they reconnect
# to the new master the next time that they are used.
self.disconnect(inuse_connections=False)
return master_address

def rotate_slaves(self):
Expand Down

0 comments on commit 811cdd0

Please sign in to comment.