Skip to content

Commit d434af1

Browse files
committed
similarly make non-async connect callbacks internal, use same system as for async.
1 parent b0214b2 commit d434af1

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

redis/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def __del__(self):
690690
def reset(self):
691691
if self.connection:
692692
self.connection.disconnect()
693-
self.connection.clear_connect_callbacks()
693+
self.connection._deregister_connect_callback(self.on_connect)
694694
self.connection_pool.release(self.connection)
695695
self.connection = None
696696
self.health_check_response_counter = 0
@@ -748,7 +748,7 @@ def execute_command(self, *args):
748748
)
749749
# register a callback that re-subscribes to any channels we
750750
# were listening to when we were disconnected
751-
self.connection.register_connect_callback(self.on_connect)
751+
self.connection._register_connect_callback(self.on_connect)
752752
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
753753
self.connection._parser.set_push_handler(self.push_handler_func)
754754
connection = self.connection

redis/cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ def execute_command(self, *args):
17711771
)
17721772
# register a callback that re-subscribes to any channels we
17731773
# were listening to when we were disconnected
1774-
self.connection.register_connect_callback(self.on_connect)
1774+
self.connection._register_connect_callback(self.on_connect)
17751775
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
17761776
self.connection._parser.set_push_handler(self.push_handler_func)
17771777
connection = self.connection

redis/connection.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,16 @@ def _construct_command_packer(self, packer):
237237
else:
238238
return PythonRespSerializer(self._buffer_cutoff, self.encoder.encode)
239239

240-
def register_connect_callback(self, callback):
241-
self._connect_callbacks.append(weakref.WeakMethod(callback))
240+
def _register_connect_callback(self, callback):
241+
wm = weakref.WeakMethod(callback)
242+
if wm not in self._connect_callbacks:
243+
self._connect_callbacks.append(wm)
242244

243-
def clear_connect_callbacks(self):
244-
self._connect_callbacks = []
245+
def _deregister_connect_callback(self, callback):
246+
try:
247+
self._connect_callbacks.remove(weakref.WeakMethod(callback))
248+
except ValueError:
249+
pass
245250

246251
def set_parser(self, parser_class):
247252
"""
@@ -279,6 +284,8 @@ def connect(self):
279284

280285
# run any user callbacks. right now the only internal callback
281286
# is for pubsub channel/pattern resubscription
287+
# first, remove any dead weakrefs
288+
self._connect_callbacks = [ref for ref in self._connect_callbacks if ref()]
282289
for ref in self._connect_callbacks:
283290
callback = ref()
284291
if callback:

0 commit comments

Comments
 (0)