Skip to content

Commit 811cdd0

Browse files
committed
ConnectionPool.disconnect now accepts arg to disconnect only idle conns
When the sentinel's master address is changed, disconnect all idle connections in the pool.
1 parent 6518c08 commit 811cdd0

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

redis/connection.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,13 +1250,23 @@ def release(self, connection):
12501250
def owns_connection(self, connection):
12511251
return connection.pid == self.pid
12521252

1253-
def disconnect(self):
1254-
"Disconnects all connections in the pool"
1253+
def disconnect(self, inuse_connections=True):
1254+
"""
1255+
Disconnects connections in the pool
1256+
1257+
If ``inuse_connections`` is True, disconnect connections that are
1258+
current in use, potentially by other threads. Otherwise only disconnect
1259+
connections that are idle in the pool.
1260+
"""
12551261
self._checkpid()
12561262
with self._lock:
1257-
all_conns = chain(self._available_connections,
1258-
self._in_use_connections)
1259-
for connection in all_conns:
1263+
if inuse_connections:
1264+
connections = chain(self._available_connections,
1265+
self._in_use_connections)
1266+
else:
1267+
connections = self._available_connections
1268+
1269+
for connection in connections:
12601270
connection.disconnect()
12611271

12621272

redis/sentinel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def get_master_address(self):
108108
if self.is_master:
109109
if self.master_address != master_address:
110110
self.master_address = master_address
111+
# disconnect any idle connections so that they reconnect
112+
# to the new master the next time that they are used.
113+
self.disconnect(inuse_connections=False)
111114
return master_address
112115

113116
def rotate_slaves(self):

0 commit comments

Comments
 (0)