问题描述
限时送ChatGPT账号..我正在使用 LineReceiver 协议运行 Twisted 服务器.有时客户端会悄悄断开连接,因此 Twisted 保持连接打开.并且因为服务器不会发送任何东西,除非请求它,所以永远不会有 TCP 超时.换句话说,有些连接永远不会在服务器端关闭.
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.
如何让 Twisted 关闭几个小时不活动的连接?
How can I have Twisted close a connection that's been inactive for a few hours?
推荐答案
您可以使用 reactor.callLater
安排定时事件.基于此,有一个用于向协议添加超时的帮助程序,twisted.protocols.policies.TimeoutMixin
.
You can schedule timed events using reactor.callLater
. Based on this, there's a helper for adding timeouts to protocols, twisted.protocols.policies.TimeoutMixin
.
另一种方法是使用 TCP 保持连接,您可以使用传输的 setTcpKeepAlive
方法启用它.
Another approach is to use TCP keep-alives, which you can enable using the transport's setTcpKeepAlive
method.
另一种方法是使用应用程序级保活.基本上是偶尔发送一个noop".它不需要回应.如果连接已经丢失,发送缓冲区中的额外数据最终会导致 TCP 堆栈注意到.
And another approach is to use application-level keep-alives. Essentially send a ''noop'' once in a while. It doesn't need a response. If the connection has been lost, the extra data in the send buffer will cause the TCP stack to eventually notice.
另请参阅常见问题解答.
这篇关于关闭 Twisted 中的非活动连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论