msgpack.exceptions.ExtraData 在 AWS 上的 Django 通道中接收消息反序列化错误:

问题描述 投票:0回答:1

我正在使用 Django 和 Django Channels 开发一个 Web 应用程序,在处理 WebSocket 消费者接收事件时遇到了问题。当从客户端接收消息时,应用程序抛出反序列化错误:msgpack.exceptions.ExtraData: unpack(b)received extra data.

背景: 框架和版本:我使用的是通过 AWS Beanstalk 部署的环境,其中包括 Linux 2023、Django(版本 4.2.6)、Django Channels(版本 4.0.0)、Channels-redis(版本 4.1.0)。

我们有一个负载均衡器,用于将流量重定向到 WSGI 服务器(端口 8000,一切按预期工作),并将 wss 流量重定向到 ASGI 服务器(Daphne)以进行异步管理。 安全组配置正确并按预期允许流量。 Elastic Redis 缓存服务已启用并且工作正常(我们保存和检索信息没有问题)。

最初,对于CHANNEL_LAYERS后端,我选择了channels_redis.core.RedisChannelLayer,但是连接不是持久的(客户端在一秒钟内连接和断开),通过选择channels_redis.pubsub.RedisPubSubChannelLayer解决了这个问题。

Channels 后端:我已将 Django Channels 配置为使用 Redis 作为通道后端,在我的 settings.py 中使用 CHANNEL_LAYERS 我还包括我当前的消费者代码、asgi 和完整的回溯

预期行为: 我希望消费者能够毫无问题地处理传入消息,但消息反序列化似乎存在问题。

解决方案尝试: 我已经验证通过通道发送的数据格式是正确的。 我尝试了各种不同的数据格式,甚至对要发送的消息进行了硬编码。 我已经检查了 Django Channels 配置并确保所有内容都配置正确。

问题: 有没有人遇到过类似的问题,或者可以确定是什么原因导致 Django Channels 中的 msgpack 出现反序列化错误?任何帮助或建议将不胜感激。

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.pubsub.RedisPubSubChannelLayer',
        'CONFIG': {
            "hosts": [('cacheback-mbxa32.serverless.euw3.cache.amazonaws.com', 6379)],
        },
    },
}

Also, this is my asgi.py: 

    import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import appProject.routing
import logging

logger = logging.getLogger(__name__)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'appProject.settings')
logger.debug("Entra en ASGI")
application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
        URLRouter(
            appProject.routing.websocket_urlpatterns
        )
    ),
})


**Consumer receive event**

   async def receive(self, text_data):
        try:
            text_data_json = json.loads(text_data)
            message = text_data_json['message']
           
            # Enviar mensaje al grupo de chat
            await self.channel_layer.group_send(
                self.room_group_name,
                {
                    'type': 'chat_message',
                    'message': message
                }
            )`

**And here it goes the traceback log:**
2024-01-16 03:46:33,676 No-IP daphne.ws_protocol DEBUG WebSocket ['172.31.9.79', 52020] open and established
2024-01-16 03:46:33,676 No-IP daphne.ws_protocol DEBUG WebSocket ['172.31.9.79', 52020] accepted by application
2024-01-16 03:46:33,676 No-IP daphne.ws_protocol DEBUG Sent WebSocket packet to client for ['172.31.9.79', 52020]
2024-01-16 03:46:33,677 No-IP daphne.ws_protocol DEBUG Sent WebSocket packet to client for ['172.31.9.79', 52020]
2024-01-16 03:46:39,226 No-IP daphne.ws_protocol DEBUG WebSocket incoming frame on ['172.31.9.79', 52020]
2024-01-16 03:46:40,053 No-IP daphne.server ERROR Exception inside application: unpack(b) received extra data.
Traceback (most recent call last):
  File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/channels/routing.py", line 62, in __call__
    return await application(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/channels/sessions.py", line 47, in __call__
    return await self.inner(dict(scope, cookies=cookies), receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/channels/sessions.py", line 263, in __call__
    return await self.inner(wrapper.scope, receive, wrapper.send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/channels/auth.py", line 185, in __call__
    return await super().__call__(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/channels/middleware.py", line 24, in __call__
    return await self.inner(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/channels/routing.py", line 116, in __call__
    return await application(
           ^^^^^^^^^^^^^^^^^^
  File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/channels/consumer.py", line 94, in app
    return await consumer(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/channels/consumer.py", line 58, in __call__
    await await_many_dispatch(
  File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/channels/utils.py", line 57, in await_many_dispatch
    await task
msgpack.exceptions.ExtraData: unpack(b) received extra data.
2024-01-16 03:46:40,289 No-IP daphne.ws_protocol DEBUG WebSocket closed for ['172.31.9.79', 52020]


django deserialization channel
1个回答
0
投票

我已经按照 Niloct 的建议进行了操作,并且在我的本地 Redis 监视器中得到了以下信息:

1705421806.387313 [0 127.0.0.1:38956] "CLIENT" "SETINFO" "LIB-NAME" "redis-py" 
1705421806.387603 [0 127.0.0.1:38956] "CLIENT" "SETINFO" "LIB-VER" "5.0.1"
1705421806.389391 [0 127.0.0.1:38956] "SUBSCRIBE" "asgispecific.2f346c113eab4adcbbc47be90003e250"
1705421806.389663 [0 127.0.0.1:38956] "SUBSCRIBE" "asgi__group__chat_test_group"
1705421821.303837 [0 127.0.0.1:44304] "CLIENT" "SETINFO" "LIB-NAME" "redis-py"
1705421821.303892 [0 127.0.0.1:44304] "CLIENT" "SETINFO" "LIB-VER" "5.0.1"
1705421821.305117 [0 127.0.0.1:44304] "PUBLISH" "asgi__group__chat_test_group" "\x82\xa4type\xacchat.message\xa7message\xb0hola mundo"

我没有得到更多信息...

© www.soinside.com 2019 - 2024. All rights reserved.