summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2020-06-01 11:41:20 -0700
committerAndy McCurdy <andy@andymccurdy.com>2020-06-01 11:41:20 -0700
commit811cdd0282c0c415a379d882678a5c3f7ab74def (patch)
tree7c64db0c19bae775aa7bf6beef3d95b2fee57754
parent6518c0812667b567fca365481366f07a38e0d755 (diff)
downloadredis-py-sentinel-1345.tar.gz
ConnectionPool.disconnect now accepts arg to disconnect only idle connssentinel-1345
When the sentinel's master address is changed, disconnect all idle connections in the pool.
-rwxr-xr-xredis/connection.py20
-rw-r--r--redis/sentinel.py3
2 files changed, 18 insertions, 5 deletions
diff --git a/redis/connection.py b/redis/connection.py
index e412f05..e3c9b66 100755
--- a/redis/connection.py
+++ b/redis/connection.py
@@ -1250,13 +1250,23 @@ class ConnectionPool(object):
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()
diff --git a/redis/sentinel.py b/redis/sentinel.py
index bbc4cf5..2b212ea 100644
--- a/redis/sentinel.py
+++ b/redis/sentinel.py
@@ -108,6 +108,9 @@ class SentinelConnectionPool(ConnectionPool):
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):