Skip to content

Commit 9efb208

Browse files
kristjanvalurvladvildanov
authored andcommitted
Make the connection callback methods public again, add documentation (#2980)
1 parent ce9eb83 commit 9efb208

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

786786
def __del__(self):
787787
if self.connection:
788-
self.connection._deregister_connect_callback(self.on_connect)
788+
self.connection.deregister_connect_callback(self.on_connect)
789789

790790
async def aclose(self):
791791
# In case a connection property does not yet exist
@@ -796,7 +796,7 @@ async def aclose(self):
796796
async with self._lock:
797797
if self.connection:
798798
await self.connection.disconnect()
799-
self.connection._deregister_connect_callback(self.on_connect)
799+
self.connection.deregister_connect_callback(self.on_connect)
800800
await self.connection_pool.release(self.connection)
801801
self.connection = None
802802
self.channels = {}
@@ -859,7 +859,7 @@ async def connect(self):
859859
)
860860
# register a callback that re-subscribes to any channels we
861861
# were listening to when we were disconnected
862-
self.connection._register_connect_callback(self.on_connect)
862+
self.connection.register_connect_callback(self.on_connect)
863863
else:
864864
await self.connection.connect()
865865
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
@@ -756,7 +756,7 @@ def __del__(self) -> None:
756756
def reset(self) -> None:
757757
if self.connection:
758758
self.connection.disconnect()
759-
self.connection._deregister_connect_callback(self.on_connect)
759+
self.connection.deregister_connect_callback(self.on_connect)
760760
self.connection_pool.release(self.connection)
761761
self.connection = None
762762
self.health_check_response_counter = 0
@@ -814,7 +814,7 @@ def execute_command(self, *args):
814814
)
815815
# register a callback that re-subscribes to any channels we
816816
# were listening to when we were disconnected
817-
self.connection._register_connect_callback(self.on_connect)
817+
self.connection.register_connect_callback(self.on_connect)
818818
if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
819819
self.connection._parser.set_push_handler(self.push_handler_func)
820820
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)