Skip to content

Commit

Permalink
Add clean_channel method
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterseth committed Nov 4, 2022
1 parent d663090 commit ab40db7
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions channels_redis/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,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.
Expand All @@ -186,14 +196,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)
Expand Down

0 comments on commit ab40db7

Please sign in to comment.