Skip to content

Commit ab99e33

Browse files
committed
Make the connection callback methods public again, add documentation
1 parent 0113034 commit ab99e33

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
* Add 'aclose()' methods to async classes, deprecate async close().
23
* Fix #2831, add auto_close_connection_pool=True arg to asyncio.Redis.from_url()
34
* Fix incorrect redis.asyncio.Cluster type hint for `retry_on_error`

redis/asyncio/client.py

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

785785
def __del__(self):
786786
if self.connection:
787-
self.connection._deregister_connect_callback(self.on_connect)
787+
self.connection.deregister_connect_callback(self.on_connect)
788788

789789
async def aclose(self):
790790
# In case a connection property does not yet exist
@@ -795,7 +795,7 @@ async def aclose(self):
795795
async with self._lock:
796796
if self.connection:
797797
await self.connection.disconnect()
798-
self.connection._deregister_connect_callback(self.on_connect)
798+
self.connection.deregister_connect_callback(self.on_connect)
799799
await self.connection_pool.release(self.connection)
800800
self.connection = None
801801
self.channels = {}
@@ -858,7 +858,7 @@ async def connect(self):
858858
)
859859
# register a callback that re-subscribes to any channels we
860860
# were listening to when we were disconnected
861-
self.connection._register_connect_callback(self.on_connect)
861+
self.connection.register_connect_callback(self.on_connect)
862862
else:
863863
await self.connection.connect()
864864
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
@@ -693,7 +693,7 @@ def __del__(self) -> None:
693693
def reset(self) -> None:
694694
if self.connection:
695695
self.connection.disconnect()
696-
self.connection._deregister_connect_callback(self.on_connect)
696+
self.connection.deregister_connect_callback(self.on_connect)
697697
self.connection_pool.release(self.connection)
698698
self.connection = None
699699
self.health_check_response_counter = 0
@@ -751,7 +751,7 @@ def execute_command(self, *args):
751751
)
752752
# register a callback that re-subscribes to any channels we
753753
# were listening to when we were disconnected
754-
self.connection._register_connect_callback(self.on_connect)
754+
self.connection.register_connect_callback(self.on_connect)
755755
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
756756
self.connection._parser.set_push_handler(self.push_handler_func)
757757
connection = self.connection

redis/cluster.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1775,7 +1775,7 @@ def execute_command(self, *args):
17751775
)
17761776
# register a callback that re-subscribes to any channels we
17771777
# were listening to when we were disconnected
1778-
self.connection._register_connect_callback(self.on_connect)
1778+
self.connection.register_connect_callback(self.on_connect)
17791779
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
17801780
self.connection._parser.set_push_handler(self.push_handler_func)
17811781
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)