当取消客户端请求并且服务器仍在工作时,应用程序服务器(tomcat等)中会发生什么? (在其输出上写) [英] what happens in an application server (tomcat etc.) when a client request is cancelled and the server is still working ? (writing on its output)

查看:261
本文介绍了当取消客户端请求并且服务器仍在工作时,应用程序服务器(tomcat等)中会发生什么? (在其输出上写)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果客户端取消其请求,则应用程序服务器会抛出以下错误:

If a client cancel its request, the application server is suposed to throw the following error :

java.net.SocketException: Connection reset by peer: socket write error

但究竟发生了什么?

假设我在服务器端进行了非常昂贵的操作,每次我的服务器服务获得新结果(流式传输)时,我都会向输出流写入一些数据。
在此操作过程中,客户端取消请求。会发生什么?

Let's say I'm doing a very expensive operation on the server side, and I'm writing some data to the outputstream everytime my server service get a new result (kind of streaming). In the middle of this operation, the client cancel the request. What happens ?

操作停止,因为套接字在连接关闭时抛出此错误?如果它没有停止,那么之后在输出流中刷新的数据会发生什么?

The operation stops, because the socket throws this error when the connection closed ? If it's not stopped, what happens to the data flushed in the outputstream after that ?

谢谢

推荐答案

我不知道Tomcat在做什么,但这里发生了什么:

I can't tell what Tomcat is doing but here is what happens:


  1. 客户端正常关闭套接字(然后服务器会收到有关关闭的通知并关闭它的连接端,在这种情况下,任何准备发送的缓冲数据都会丢失);

  1. the client closed the socket gracefully (then the server is notified about the close and closes its side of the connection too, in which case any buffered data ready to be sent is lost);

客户端残忍地切断了套接字(然后服务器没有得到通知,它会在超时后或第一次尝试发送数据时检测到连接丢失 - 这将失败)。

the client cut the socket brutally (then the server is NOT notified and it will detect the connection loss after a timeout or at the first attempt to send data - this will fail).

因此,如果您的流式传输是常量,则服务器将始终受到保护以防止未检测到丢失的连接(第一次发送尝试将清除区域)。

So, if your streaming is "constant", the server will always be 'protected' against undetected lost connections (the first send attempt will clean-up the area).

如果此流不是常量,那么您应该为超时腾出空间,或使用TCP Keep-Alives确保连接状态定期测试。

If this streaming is not constant, then you should make room for a timeout, or use TCP Keep-Alives to make sure that the connection state is tested on a regulary basis.

希望它有所帮助。

这篇关于当取消客户端请求并且服务器仍在工作时,应用程序服务器(tomcat等)中会发生什么? (在其输出上写)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆