Skip to content

Commit 0357301

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

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

redis/client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ def __del__(self):
665665
def reset(self):
666666
if self.connection:
667667
self.connection.disconnect()
668-
self.connection.clear_connect_callbacks()
668+
self.connection._deregister_connect_callbacks(self.on_connect)
669669
self.connection_pool.release(self.connection)
670670
self.connection = None
671671
self.health_check_response_counter = 0
@@ -723,7 +723,7 @@ def execute_command(self, *args):
723723
)
724724
# register a callback that re-subscribes to any channels we
725725
# were listening to when we were disconnected
726-
self.connection.register_connect_callback(self.on_connect)
726+
self.connection._register_connect_callback(self.on_connect)
727727
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
728728
self.connection._parser.set_push_handler(self.push_handler_func)
729729
connection = self.connection

redis/connection.py

+11-4
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)