Ajax 请求和竞争条件(客户端和服务器端)
让我们想象一下这样的情况:我们一一向服务器发送了两个相似(几乎相似)的异步ajax请求。由于网络延迟,第二个请求在第一个请求之前执行。
Ajax request #1: /change/?object_id=1&position=5
Ajax request #2: /change/?object_id=1&position=6
结果,我们将 object_id=1
位置设置为 position=5
,但我们想要 position=6
因为 Ajax 请求 #2
是在我们的 Ajax request #1
之后执行的。
在服务器端和客户端避免这种情况的最佳实践是什么?
Let's imagine the situation that we've sent two similar (almost similar) async ajax requests to server one by one. Because of lag in network, the second request was executed before first request.
Ajax request #1: /change/?object_id=1&position=5
Ajax request #2: /change/?object_id=1&position=6
In result, we have object_id=1
position set to position=5
, but we want position=6
because Ajax request #2
was executed after Ajax request #1
by us.
What is the best practice to avoid this on server side and client side?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否担心来自同一客户端或多个客户端的比赛条件?
如果来自同一个客户端,我认为最安全的选择是在 ajax 请求中包含 unix 时间戳并将该值记录在服务器上。如果请求附带的时间戳早于上次记录的值,请忽略该请求(或向浏览器发送警告)。
我不确定你将如何处理时钟不同步的多个客户端......
Are you worried about racing conditions from the same client or from multiple clients?
If from the same client, I would think the safest bet would be to include a unix timestamp in the ajax request and log this value on the server. If a request comes with a timestamp that is older than the last logged value, ignore the request (or send a warning back to the browser).
I'm not sure how you would handle multiple clients with unsynchronized clocks...
对于这种情况,我通常会在成功处理程序中进行检查,以确保返回的值仍然是我想要的值。这将需要发送您在结果对象中搜索的参数。
例如:
我不知道您的用例的详细信息,但这个一般模式应该有所帮助。
For situations like this, I usually put a check in my success handler to make sure that the value being returned is still the one that I want. This will require sending up the parameter you're searching across in the results object.
For example:
I don't know the particulars of your use case, but this general pattern should help.
在服务器上:
在客户端:
请注意,我建议的这种实现要求您来回发送数据(例如 JSON),而不是像您希望的那样发送表示代码(例如 HTML 片段)需要检查客户端的忽略标志。
这个答案类似于@Farray 使用时间戳的建议。
On the server :
On the client:
Note that this implementation that I suggested requires you to send back and forth data (such as JSON) and not the presentation code (such as HTML fragment) as you would need to check for the ignore flag on the client side.
This answer is similar to what @Farray suggestion of using timestamp.