Skip to content

Commit

Permalink
similarly make non-async connect callbacks internal, use same system …
Browse files Browse the repository at this point in the history
…as for async.
  • Loading branch information
kristjanvalur committed Sep 17, 2023
1 parent 7c22cdf commit 0357301
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ def __del__(self):
def reset(self):
if self.connection:
self.connection.disconnect()
self.connection.clear_connect_callbacks()
self.connection._deregister_connect_callbacks(self.on_connect)
self.connection_pool.release(self.connection)
self.connection = None
self.health_check_response_counter = 0
Expand Down Expand Up @@ -723,7 +723,7 @@ def execute_command(self, *args):
)
# register a callback that re-subscribes to any channels we
# were listening to when we were disconnected
self.connection.register_connect_callback(self.on_connect)
self.connection._register_connect_callback(self.on_connect)
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
self.connection._parser.set_push_handler(self.push_handler_func)
connection = self.connection
Expand Down
15 changes: 11 additions & 4 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,16 @@ def _construct_command_packer(self, packer):
else:
return PythonRespSerializer(self._buffer_cutoff, self.encoder.encode)

def register_connect_callback(self, callback):
self._connect_callbacks.append(weakref.WeakMethod(callback))
def _register_connect_callback(self, callback):
wm = weakref.WeakMethod(callback)
if wm not in self._connect_callbacks:
self._connect_callbacks.append(wm)

def clear_connect_callbacks(self):
self._connect_callbacks = []
def _deregister_connect_callback(self, callback):
try:
self._connect_callbacks.remove(weakref.WeakMethod(callback))
except ValueError:
pass

def set_parser(self, parser_class):
"""
Expand Down Expand Up @@ -279,6 +284,8 @@ def connect(self):

# run any user callbacks. right now the only internal callback
# is for pubsub channel/pattern resubscription
# first, remove any dead weakrefs
self._connect_callbacks = [ref for ref in self._connect_callbacks if ref()]
for ref in self._connect_callbacks:
callback = ref()
if callback:
Expand Down

0 comments on commit 0357301

Please sign in to comment.