Tcp协议在twisted中的数据丢失问题
我用twisted.internet模块编写了一个基于tcp的服务器。这是一个高并发环境。
我通常通过 protocol.Protocol 实例发送数据,但遇到了问题。 一些tcp连接可能因超时而关闭,而且我似乎无法收到任何通知,因此我在关闭连接中写入的数据可能会丢失。
并且数据丢失问题可能是由其他原因引起的。
有什么好的方法可以控制吗? (socket.send可以返回一个状态,transport.write似乎没有返回)
I wrote a tcp based server with the twisted.internet module. It's a high concurrency environment.
I usually send data by the instance of protocol.Protocol, and I got a problem with that.
Some of the tcp connections may be closed caused by timeout, and it seems I cannot get any notification so that the data I have written in the closed connection may be lost.
And the data loss problem may caused by some other way.
Is there any good way to control it? (socket.send could return a state, transport.write seems have no return)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此问题并非 Twisted 特有。如果您想知道数据已收到,您的协议必须对已收到数据进行某种确认。
send()
的结果不会告诉您该数据已被对等方权威地接收;它只是说它已被内核排队等待传输。从应用程序的角度来看,数据是否由 Twisted、C 运行时、内核、中间下游交换机、对等方的内核或其他任何方式排队并不重要。也许已发送,也许未发送。换句话说,transport.write()
负责send()
不负责的额外缓冲,保证它始终缓冲所有字节,而send()
只会缓冲一些。如果您关心网络对等方是否已看到您的数据,您绝对需要有应用程序级确认消息。
This problem is not specific to Twisted. Your protocol must have some acknowledgement that data was received, if you want to know that it was received.
The result from
send()
does not tell you that the data was authoritatively received by the peer; it just says that it was queued by the kernel for transport. From your application's point of view, it really doesn't matter whether the data was queued by Twisted, or by your C runtime, or by your kernel, or an intermediary downstream switch, or the peer's kernel, or whatever. Maybe it's sent, maybe it's not. Put another way,transport.write()
takes care of additional buffering thatsend()
doesn't, guaranteeing that it always buffers all of your bytes, whereassend()
will only buffer some.You absolutely need to have an application-level acknowledgement message if you care about whether a network peer has seen your data or not.