1

I'm running a Twisted server with the LineReceiver protocol. Sometimes clients will disconnect silently, so Twisted keeps the connection open. And because the server doesn't send anything unless requested of it, there's never a TCP timeout. In other words, some connections are never closed server-side.

How can I have Twisted close a connection that's been inactive for a few hours?

4

1 回答 1

2

您可以使用reactor.callLater. 基于此,有一个助手可以为协议添加超时,twisted.protocols.policies.TimeoutMixin.

另一种方法是使用 TCP keep-alives,您可以使用传输的setTcpKeepAlive方法启用它。

另一种方法是使用应用程序级保活。基本上偶尔发送一个“noop”。它不需要回应。如果连接丢失,发送缓冲区中的额外数据将导致 TCP 堆栈最终注意到。

另请参阅FAQ 条目

于 2012-08-11T11:08:48.140 回答