From 817baa72e3964e33233b9bf0964dd11510a052d5 Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Wed, 5 Dec 2012 12:56:09 -0500 Subject: [PATCH] Avoid sending messages to clients more than once due to out-of-order processing This resolves #535 (imported from commit 9a7bf7443d17523e9ba14797a0215ee11262c7de) --- zephyr/views.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/zephyr/views.py b/zephyr/views.py index 930908203b..e796a4ea5a 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -443,16 +443,28 @@ def get_updates_backend(request, user_profile, handler, client_id, if handler.request.connection.stream.closed(): return try: - # It would be nice to be able to do this check in + # It would be nice to be able to do these checks in # UserProfile.receive, but it doesn't know what the value # of "last" was for each callback. if last is not None and "messages" in cb_kwargs: + messages = cb_kwargs["messages"] + + # Make sure the client doesn't get a message twice + # when messages are processed out of order. + if messages[0].id <= last: + # We must return a response because we don't have + # a way to re-queue a callback and so the client + # must do it by making a new request + handler.finish({"result": "success", + "msg": "", + 'update_types': []}) + return + # We need to check whether there are any new messages # between the client's get_updates call and the # message we're about to return to the client and # return them as well or the client will miss them. # See #174. - messages = cb_kwargs["messages"] extra_messages = (Message.objects.select_related() .filter(usermessage__user_profile = user_profile, id__gt = last,