jrief / django-websocket-redis

Websockets for Django applications using Redis as message queue
http://django-websocket-redis.awesto.com/
MIT License
895 stars 222 forks source link

Getting unintended echo responses when sending messages too fast. #173

Open pkkid opened 8 years ago

pkkid commented 8 years ago

I was getting a lot of echo responses from the server even though I have not subscribed to them. After digging into it I found that in wsgi_server.py:106 you define recvmsg = None and then later use the value of that (on line 119) to determine if you are sending the same message that just came in. I am finding this breaks down fairly quickly when sending messages even at a low rate of 5 to 10 fps.

A quick solution I came up with to solve this is to use a deque to track the last 10 messages rather than just the last single message. In my very crude testing I am seeing this keep up even at a rate of 200 fps. The code change is pretty simple..

from collections import deque

# line:106 - save most recent 10 items (rather than single most recent)
recent = deque(maxlen=10)

# line:115 - when a message comes in..
recent.append(recvmsg)

# line:119 - change the check on line 119 to..
if sendmsg and (echo_message or sendmsg not in recent):