Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clean_channel method #339

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions channels_redis/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
Loading