1

I am building a voting mechanism for a site. A similar one seen on Stackoverflow.

For instance, if user clicked up-arrow, vote = True. If he clicks again on it, vote = None. The app is working fine except if we submit votes very fastly.

We tried to click arrows very very fastly and see how voting is happening by logging the data. Unfortunately, we are seeing some misbehavior. By fast, I mean, clicking the arrow continuously without stopping for some seconds!

The expected log data should be like

vote=True
vote=None
vote=True
vote=None
..

But I observed it like

vote=True
vote=True
vote=None
vote=None

The observed log data mentioned as second case, seems to be a bit unordered.. This could mean that the requests received by django are not handled as a queue! Which in our case is a bit dangerous. Or database is taking some time to store and during that period another requests are handled which is causing the error.

I hope you are understanding my issue. So, I am wondering if you can let me know what's going on here and how to control it.

4

2 回答 2

1

您不能对浏览器发送(异步)请求的顺序、它们到达服务器的顺序以及它们由单个或多实例(线程、工作者)Django 应用程序处理的顺序做出假设。

所以你上面描述的是你实际上可能期望的。执行同步请求可能会有所帮助。最好的选择可能是(异步)在允许进一步点击之前等待服务器的响应。

于 2013-09-17T13:11:40.600 回答
0

你必须有一个类似于这样的流程:

-> User clicks button
-> check if user has already voted up
-> if no:
    -> vote up request goes
    -> vote up after validation
    -> response is sent back to browser
-> else:
    -> vote none request goes
    -> remove the vote after validation
    -> response is sent back to browser

如果您在请求已经发送并等待其响应时没有禁用该按钮,那么您将遇到这种情况。

比如说, request1 被认为是投票赞成并发送了请求。

在 request1 的响应到来并且用户再次点击之前,那么这个请求也将被视为投票,这不是您所期望的。

  • 您应该在进行 ajax 调用之前禁用该按钮,并在收到响应时再次启用它。
  • 或者每当用户单击时,您应该翻转按钮类型,即,即使在进行 ajax 调用之前,也可以将其设置为无投票请求,反之亦然。并且当收到 ajax 响应时,再次验证之前的操作。它只会在服务器端任何验证失败的情况下有所不同,例如用户可能无权投票。

如果您尝试对自己的帖子进行投票,您可以在 SO 中看到这种情况。它首先更改为 vote-none 模式,然后当从服务器接收到响应时,它会更改回来并给出错误消息。

PS:我试图投票支持我自己的帖子仅用于教育目的;)

于 2013-09-17T13:15:48.257 回答