Skip to content

Commit fd281e0

Browse files
Make the connection callback methods public again, add documentation (#2980)
1 parent 50245cd commit fd281e0

File tree

6 files changed

+35
-10
lines changed

6 files changed

+35
-10
lines changed

CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Connection.register_connect_callback() is made public.
12
* Fix async `read_response` to use `disable_decoding`.
23
* Add 'aclose()' methods to async classes, deprecate async close().
34
* Fix #2831, add auto_close_connection_pool=True arg to asyncio.Redis.from_url()

redis/asyncio/client.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):
788788

789789
def __del__(self):
790790
if self.connection:
791-
self.connection._deregister_connect_callback(self.on_connect)
791+
self.connection.deregister_connect_callback(self.on_connect)
792792

793793
async def aclose(self):
794794
# In case a connection property does not yet exist
@@ -799,7 +799,7 @@ async def aclose(self):
799799
async with self._lock:
800800
if self.connection:
801801
await self.connection.disconnect()
802-
self.connection._deregister_connect_callback(self.on_connect)
802+
self.connection.deregister_connect_callback(self.on_connect)
803803
await self.connection_pool.release(self.connection)
804804
self.connection = None
805805
self.channels = {}
@@ -862,7 +862,7 @@ async def connect(self):
862862
)
863863
# register a callback that re-subscribes to any channels we
864864
# were listening to when we were disconnected
865-
self.connection._register_connect_callback(self.on_connect)
865+
self.connection.register_connect_callback(self.on_connect)
866866
else:
867867
await self.connection.connect()
868868
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:

redis/asyncio/connection.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,24 @@ def repr_pieces(self):
235235
def is_connected(self):
236236
return self._reader is not None and self._writer is not None
237237

238-
def _register_connect_callback(self, callback):
238+
def register_connect_callback(self, callback):
239+
"""
240+
Register a callback to be called when the connection is established either
241+
initially or reconnected. This allows listeners to issue commands that
242+
are ephemeral to the connection, for example pub/sub subscription or
243+
key tracking. The callback must be a _method_ and will be kept as
244+
a weak reference.
245+
"""
239246
wm = weakref.WeakMethod(callback)
240247
if wm not in self._connect_callbacks:
241248
self._connect_callbacks.append(wm)
242249

243-
def _deregister_connect_callback(self, callback):
250+
def deregister_connect_callback(self, callback):
251+
"""
252+
De-register a previously registered callback. It will no-longer receive
253+
notifications on connection events. Calling this is not required when the
254+
listener goes away, since the callbacks are kept as weak methods.
255+
"""
244256
try:
245257
self._connect_callbacks.remove(weakref.WeakMethod(callback))
246258
except ValueError:

redis/client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ def __del__(self) -> None:
759759
def reset(self) -> None:
760760
if self.connection:
761761
self.connection.disconnect()
762-
self.connection._deregister_connect_callback(self.on_connect)
762+
self.connection.deregister_connect_callback(self.on_connect)
763763
self.connection_pool.release(self.connection)
764764
self.connection = None
765765
self.health_check_response_counter = 0
@@ -817,7 +817,7 @@ def execute_command(self, *args):
817817
)
818818
# register a callback that re-subscribes to any channels we
819819
# were listening to when we were disconnected
820-
self.connection._register_connect_callback(self.on_connect)
820+
self.connection.register_connect_callback(self.on_connect)
821821
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
822822
self.connection._parser.set_push_handler(self.push_handler_func)
823823
connection = self.connection

redis/cluster.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ def execute_command(self, *args):
17761776
)
17771777
# register a callback that re-subscribes to any channels we
17781778
# were listening to when we were disconnected
1779-
self.connection._register_connect_callback(self.on_connect)
1779+
self.connection.register_connect_callback(self.on_connect)
17801780
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
17811781
self.connection._parser.set_push_handler(self.push_handler_func)
17821782
connection = self.connection

redis/connection.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,24 @@ 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):
240+
def register_connect_callback(self, callback):
241+
"""
242+
Register a callback to be called when the connection is established either
243+
initially or reconnected. This allows listeners to issue commands that
244+
are ephemeral to the connection, for example pub/sub subscription or
245+
key tracking. The callback must be a _method_ and will be kept as
246+
a weak reference.
247+
"""
241248
wm = weakref.WeakMethod(callback)
242249
if wm not in self._connect_callbacks:
243250
self._connect_callbacks.append(wm)
244251

245-
def _deregister_connect_callback(self, callback):
252+
def deregister_connect_callback(self, callback):
253+
"""
254+
De-register a previously registered callback. It will no-longer receive
255+
notifications on connection events. Calling this is not required when the
256+
listener goes away, since the callbacks are kept as weak methods.
257+
"""
246258
try:
247259
self._connect_callbacks.remove(weakref.WeakMethod(callback))
248260
except ValueError:

0 commit comments

Comments
 (0)