Skip to content

Commit 577016c

Browse files
committed
Make the connection callback methods public again, add documentation
1 parent 787d731 commit 577016c

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
@@ -783,7 +783,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):
783783

784784
def __del__(self):
785785
if self.connection:
786-
self.connection._deregister_connect_callback(self.on_connect)
786+
self.connection.deregister_connect_callback(self.on_connect)
787787

788788
async def aclose(self):
789789
# In case a connection property does not yet exist
@@ -794,7 +794,7 @@ async def aclose(self):
794794
async with self._lock:
795795
if self.connection:
796796
await self.connection.disconnect()
797-
self.connection._deregister_connect_callback(self.on_connect)
797+
self.connection.deregister_connect_callback(self.on_connect)
798798
await self.connection_pool.release(self.connection)
799799
self.connection = None
800800
self.channels = {}
@@ -857,7 +857,7 @@ async def connect(self):
857857
)
858858
# register a callback that re-subscribes to any channels we
859859
# were listening to when we were disconnected
860-
self.connection._register_connect_callback(self.on_connect)
860+
self.connection.register_connect_callback(self.on_connect)
861861
else:
862862
await self.connection.connect()
863863
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
@@ -216,12 +216,24 @@ def repr_pieces(self):
216216
def is_connected(self):
217217
return self._reader is not None and self._writer is not None
218218

219-
def _register_connect_callback(self, callback):
219+
def register_connect_callback(self, callback):
220+
"""
221+
Register a callback to be called when the connection is established either
222+
initially or reconnected. This allows listeners to issue commands that
223+
are ephemeral to the connection, for example pub/sub subscription or
224+
key tracking. The callback must be a _method_ and will be kept as
225+
a weak reference.
226+
"""
220227
wm = weakref.WeakMethod(callback)
221228
if wm not in self._connect_callbacks:
222229
self._connect_callbacks.append(wm)
223230

224-
def _deregister_connect_callback(self, callback):
231+
def deregister_connect_callback(self, callback):
232+
"""
233+
De-register a previously registered callback. It will no-longer receive
234+
notifications on connection events. Calling this is not required when the
235+
listener goes away, since the callbacks are kept as weak methods.
236+
"""
225237
try:
226238
self._connect_callbacks.remove(weakref.WeakMethod(callback))
227239
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)