服务器端 AJAX 请求的排队机制

发布于 2024-10-20 02:05:36 字数 455 浏览 1 评论 0原文

我一直在努力解决网站上用户点击生成 AJAX 请求的情况,例如,响应到来时会产生一些视觉效果。有时,用户不会等到请求完成(尽管他看到加载指示器)并开始疯狂地单击其他元素。因为原则上我不能也不想禁用这种可能性,所以我尝试(或多或少成功地)确保每当触发新请求时,前一个请求都会中止(客户端)并且其处理程序不再是被调用 - 最后到达的请求获胜。

虽然在客户端有些成功,但我现在想知道是否有某种方法可以在服务器上模拟类似的行为。由于我无法真正终止之前的请求,只能断开连接,它仍然会运行到最后并消耗宝贵的资源。如果用户点击 20 个元素,他只会看到最后一个请求的结果,但服务器上仍然会有 20 个请求浪费 CPU 做无用的工作。

是否有某种方法可以在 ASP.NET / IIS 中对同一资源的多个请求实施最后获胜策略?我想这些请求无论如何都是在内部排队的,我需要的是 IIS 当它尝试使下一个请求出队时,只需看看是否还有其他请求,并且只为同一会话中的最后一个请求提供服务。

I have been struggling with a situation on a site where users' clicks generate AJAX requests, e.g. with some visual effect when the response comes. It sometimes happens that the user doesn't wait until the request is done (although he sees a loading indicator) and starts clicking other elements like crazy. Because in principle I cannot and don't want to disable that possibility, I have tried (more or less successfully) to ensure that anytime a new request is fired, the previous one is aborted (client-side) and its handlers are no longer called - whichever request comes last wins.

Although somewhat successfull on client-side, I'm now wondering whether there is some way to simulate a similar behavior at the server. As I cannot really kill the previous request, only disconnect from it, it will still run to the end and consume valuable resources. If the user clicks on 20 elements, he will only see the result of the very last request, but there will still be 20 requests on the server wasting the CPU doing useless work.

Is there some way to implement a last-win strategy for multiple requests of the same resource in the ASP.NET / IIS ? I guess the requests are anyway internally queued, and what I would need is for the IIS when it tries to dequeue the next one, simply have a look whether there are some others and only serve the very last one from the same session.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2024-10-27 02:05:36

我知道在 IIS 上的 ASP 中,您可以测试 isClientConnected 并在客户端断开连接时中止。

我相信大多数平台上都会存在类似的东西。

但不知道ajax怎么用?

I know that in ASP on IIS you could test for isClientConnected and abort if client had disconnected.

I believe something similar would exist in most plattforms.

But I do not know how it works with ajax?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文