twisted .loseConnection 不会立即失去连接?
我有一个服务器,有几个客户端连接到它。当按下 CTRL+C 时(即,反应器开始关闭),我想关闭所有连接,等到它们完全关闭,然后停止。我通过检查连接的客户端传输并调用 .loseConnection()
来完成此操作。对于本地连接的设备,它们会立即断开连接。然而,在通过互联网连接的设备上,连接不会立即丢失。通信停止 - 关闭客户端程序甚至不再告诉服务器连接已断开,尽管它在调用 .loseConnection()
之前这样做 - 但直到几分钟后连接才会被视为“丢失”稍后我从服务器发送了一些心跳请求后。
我知道如果连接中断,服务器将无法知道,除非它尝试发送一些数据。但是,如果我特别要求关闭连接,为什么它不立即关闭/断开连接呢?我调用了错误的函数吗?
I have a server with a few clients connected to it. When CTRL+C is hit (that is, reactor starts shutting down), I want to close all my connections, wait until they are cleanly closed, and then stop. I do this by going through the connected clients' transports and calling .loseConnection()
. On the ones that are connected locally, they immediately disconnect. However, on one that is connected through the internet, the connection is not immediately lost. Communication stops - and closing the client program no longer even tells the server that the connection has died, although it does before calling .loseConnection()
- but the connection is not deemed 'lost' until a few minutes later after I send a few heartbeat requests from the server.
I understand that if a connection dies, there's no way for the server to know unless it tries to send some data. But if I specifically ask for a connection to be closed, why does it not just close/disconnect immediately? Am I calling the wrong function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
loseConnection
在关闭连接之前刷新写入缓冲区。如果你不想等待这种情况发生,只需让反应堆在它发生之前停止即可。它们将以这种方式关闭,写入缓冲区中任何未发送的数据都会被丢弃。还有一项建议是添加一个用于丢弃写入缓冲区并断开连接的 API,abortConnection。
loseConnection
flushes the write buffer before shutting down the connection. If you don't want to wait for that to happen, just let the reactor stop before it does. They'll get closed that way, with any unsent data in their write buffer discarded.There's also a proposal to add an API for discarding the write buffer and disconnecting, abortConnection.