http 服务器可以检测到客户端已取消其请求吗?
我的网络应用程序必须处理和提供大量数据才能显示某些页面。有时,用户关闭或刷新页面,而服务器仍在忙于处理该页面。这意味着服务器将继续处理数据几分钟,然后将其发送给不再侦听的客户端。
是否可以检测到连接已断开并做出反应?
在这个特定的项目中,我们使用 Django 和 NginX,或者 Apache。我认为这是可能的,因为 Django 开发服务器似乎通过打印 Broken Pipe 异常来对取消的请求做出反应。我希望它引发一个我的应用程序代码可以捕获的异常。 看来 JSP 可以做到这一点。 node.js 也可以 此处。
或者,我可以在相关页面上注册一个卸载事件处理程序,让它执行同步 XHR 请求取消来自该用户的先前请求,并执行某种进程间通信来实现此目的。也许如果将较慢的数据处理交给另一个进程,我可以更轻松地识别和杀死它,而无需杀死响应的进程......
My web app must process and serve a lot of data to display certain pages. Sometimes, the user closes or refreshes a page while the server is still busy processing it. This means the server will continue to process data for several minutes only to send it to a client who is no longer listening.
Is it possible to detect that the connection has been broken, and react to it?
In this particular project, we're using Django and NginX, or Apache. I assumed this is possible because the Django development server appears to react to cancelled requests by printing Broken Pipe exceptions. I'd love to have it raise an exception that my application code could catch. It appears JSP can do this. So can node.js here.
Alternatively, I could register an unload event handler on the page in question, have it do a synchronous XHR requesting that the previous request from this user be cancelled, and do some kind of inter-process communication to make it so. Perhaps if the slower data processing were handed to another process that I could more easily identify and kill, without killing the responding process...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然 @Oded 认为 HTTP 在请求之间是无状态的,但应用服务器确实可以检测到正在处理的请求的底层 TCP/IP 连接何时中断。这是为什么呢?因为 TCP 是一种用于可靠连接的有状态协议。
.Net Web 应用程序处理资源密集型请求的常用技术是检查 Response.IsClientConnected (docs) 在开始资源密集型工作之前。浪费 CPU 周期向不再存在的客户端发送昂贵的响应是没有意义的。
请回复您的目标应用程序服务器堆栈,以便我可以提供更相关的示例。
关于使用 XHR 将客户端页面卸载事件发布到服务器的第二种选择,@Oded 关于 HTTP 在请求之间是无状态的评论是正确的。这不太可能起作用,尤其是在具有多个服务器的场中。
While @Oded is correct that HTTP is stateless between requests, app servers can indeed detect when the underlying TCP/IP connection has broken for the request being processed. Why is this? Because TCP is a stateful protocol for reliable connections.
A common technique for .Net web apps processing a resource intensive request is to check
Response.IsClientConnected
(docs) before starting the resource intensive work. There is no point in wasting CPU cycles to send an expensive response to a client that isn't there anymore.Please reply with your target app server stack so I can provide a more relevant example.
Regarding your 2nd alternative to use XHR to post client page unload events to the server, @Oded's comment about HTTP being stateless between requests is spot on. This is unlikely to work, especially in a farm with multiple servers.
HTTP 是无状态的,因此检测断开连接的客户端的唯一方法是通过超时。
请参阅这个 SO问题的答案(Java Servlet:如何检测浏览器关闭?)。
HTTP is stateless, hence the only way to detect a disconnected client is via timeouts.
See the answers to this SO question (Java Servlet : How to detect browser closing ?).