所以,我最终这样做的方式如下所述。
在消费者中,如果我发现我没有立即响应发送,我会将其存储message.reply_channel
在一个 Group 上,在发生相关事件时会收到通知,并安排一个延迟消息,当最大等待时间为时触发到达。
group_name = group_name_from_mac(mac_address)
Group(group_name).add(message.reply_channel)
message.channel_session['will_wait'] = True
delayed_message = {
'channel': 'long_polling_terminator',
'content': {'mac_address': mac_address,
'reply_channel': message.reply_channel.name,
'group_name': group_name},
'delay': settings.LONG_POLLING_TIMEOUT
}
Channel('asgi.delay').send(delayed_message, immediately=True)
然后,可能会发生两件事。要么我们在相关组上收到一条消息并提前发送响应,要么延迟的消息到达表明我们已经用尽了我们必须等待的时间,并且必须返回一个表明没有事件的响应。
为了在相关事件发生时触发消息,我依赖 Django 信号:
class PortalConfig(AppConfig):
name = 'portal'
def ready(self):
from .models import STBMessage
post_save.connect(notify_new_message, sender=STBMessage)
def notify_new_message(sender, **kwargs):
mac_address = kwargs['instance'].set_top_box.id
layer = channel_layers['default']
group_name = group_name_from_mac(mac_address)
response = JsonResponse({'error': False, 'new_events': True})
group = Group(group_name)
for chunk in AsgiHandler.encode_response(response):
group.send(chunk)
当超时到期时,我在long_polling_terminator
通道上收到一条消息,我需要发送一条消息,表明没有事件:
def long_polling_terminator(message):
reply_channel = Channel(message['reply_channel'])
group_name = message['group_name']
mac_address = message['mac_address']
layer = channel_layers['default']
boxes = layer.group_channels(group_name)
if message['reply_channel'] in boxes:
response = JsonResponse({'error': False, 'new_events': False})
write_http_response(response, reply_channel)
return
最后要做的是从组中删除此回复通道,我在http.disconnect
消费者中执行此操作:
def process_disconnect(message, group_name_from_mac):
if message.channel_session.get('will_wait', False):
reply_channel = Channel(message['reply_channel'])
mac_address = message.channel_session['mac_address']
group_name = group_name_from_mac(mac_address)
Group(group_name).discard(reply_channel)