Websocket块没有超时客户端插拔/断电

时间:2018-02-10 00:24:23

标签: java sockets websocket spring-websocket

我的Tomcat + Spring websocket应用程序存在一个奇怪的问题。当用户断开连接而不发送"关闭"信号,由于断电或插头拔出,线程将在大约10秒后阻止

线程阻止此功能:

org.springframework.web.socket.WebSocketSession.sendMessage(WebSocketMessage<?> wsm) throws IOException;

我已经尝试在我的AppConfig中添加一行来尝试设置3秒的超时但它似乎无法正常工作,因为 块似乎持续了超过15分钟然后投掷例外

@Bean(name="servletServerContainerFactoryBean")
public int maxSessionIdleTimeout() {
  return 3000;
}

这是最终 SocketTimeoutException

之后的堆栈跟踪
Step: 2304
SendB -> test isOpen -> sendMes -> Done -> Finished Send.
SendB -> test2 isOpen -> sendMes -> User closed connection during packet sending: s01
Propogating exception up!
java.io.IOException: java.net.SocketTimeoutException
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:315)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:250)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendPartialString(WsRemoteEndpointImplBase.java:223)
    at org.apache.tomcat.websocket.WsRemoteEndpointBasic.sendText(WsRemoteEndpointBasic.java:49)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketSession.sendTextMessage(StandardWebSocketSession.java:197)
    at org.springframework.web.socket.adapter.AbstractWebSocketSession.sendMessage(AbstractWebSocketSession.java:102)
    at org.infpls.noxio.game.module.game.session.NoxioSession.sendPacket(NoxioSession.java:40)
    at org.infpls.noxio.game.module.game.dao.lobby.GameLobby.step(GameLobby.java:117)
    at org.infpls.noxio.game.module.game.dao.lobby.GameLobby$GameLoop.run(GameLobby.java:274)
Caused by: java.net.SocketTimeoutException
    at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.doWrite(WsRemoteEndpointImplServer.java:81)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.writeMessagePart(WsRemoteEndpointImplBase.java:494)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:309)
    ... 8 more
## CRITICAL ## Ejecting player: test2 :: Exception thrown on packet send...
java.io.IOException: java.net.SocketTimeoutException
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:315)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:250)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendPartialString(WsRemoteEndpointImplBase.java:223)
    at org.apache.tomcat.websocket.WsRemoteEndpointBasic.sendText(WsRemoteEndpointBasic.java:49)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketSession.sendTextMessage(StandardWebSocketSession.java:197)
    at org.springframework.web.socket.adapter.AbstractWebSocketSession.sendMessage(AbstractWebSocketSession.java:102)
    at org.infpls.noxio.game.module.game.session.NoxioSession.sendPacket(NoxioSession.java:40)
    at org.infpls.noxio.game.module.game.dao.lobby.GameLobby.step(GameLobby.java:117)
    at org.infpls.noxio.game.module.game.dao.lobby.GameLobby$GameLoop.run(GameLobby.java:274)
Caused by: java.net.SocketTimeoutException
    at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.doWrite(WsRemoteEndpointImplServer.java:81)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.writeMessagePart(WsRemoteEndpointImplBase.java:494)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:309)
    ... 8 more
Finished Send. Step Finished.

一次阻塞线程15分钟是一个主要问题。任何关于为什么会发生这种情况以及如何修复它的信息将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

最后找到答案。它实际上是一个系统设置。

Source

How many times to retry before deciding that something is wrong and
it is necessary to report this suspicion to network layer. Minimal RFC
value is 3, it is default, which corresponds to  3sec-8min depending on
RTO.
/proc/sys/net/ipv4/tcp_retries2