From 21e092abd6a69551c14879e496ad7dc6bd8ca6f5 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Fri, 4 Nov 2022 14:21:14 -0400 Subject: [PATCH] Add clean_channel method --- channels_redis/pubsub.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/channels_redis/pubsub.py b/channels_redis/pubsub.py index 6957b0a..5acbadf 100644 --- a/channels_redis/pubsub.py +++ b/channels_redis/pubsub.py @@ -151,6 +151,16 @@ async def new_channel(self, prefix="specific."): await self._subscribe_to_channel(channel) return channel + async def clean_channel(self, channel): + if channel in self.channels: + del self.channels[channel] + try: + shard = self._get_shard(channel) + await shard.unsubscribe(channel) + except BaseException: + logger.exception("Unexpected exception while cleaning-up channel:") + # We don't re-raise here because we want the CancelledError to be the one re-raised. + async def receive(self, channel): """ Receive the first message that arrives on the channel. @@ -172,14 +182,7 @@ async def receive(self, channel): # be named `delete_channel()`. If that were the case, we would do the # following cleanup from that new `delete_channel()` method, but, since # that's not how Django Channels works (yet), we do the cleanup below: - if channel in self.channels: - del self.channels[channel] - try: - shard = self._get_shard(channel) - await shard.unsubscribe(channel) - except BaseException: - logger.exception("Unexpected exception while cleaning-up channel:") - # We don't re-raise here because we want the CancelledError to be the one re-raised. + await self.clean_channel(channel) raise return self.channel_layer.deserialize(message)