当使用套接字限制下载速度时,应该如何丢弃数据包?
这个问题可能听起来像是现有问题的重复,我已经阅读了漏桶
算法以及其他一些问题。
假设我的程序是单线程的,数据如下:
[已下载][当前缓冲区(正在下载)][未下载]
如果我在计算速度超出阈值时丢弃一些数据包,我该如何恢复该数据包?这样数据将是连续的。
那我该怎么丢包呢?
This question may sound like a duplicate of existing ones, I've read about Leaky bucket
algorithm, and some other ones.
Suppose my program is single-threaded, and the data is like:
[ downloaded ] [ current buffer (downloading) ] [ un-downloaded ]
If i drop some packet when the calculated speed went beyond the threshold, how am I going to recover that packet? Data will be in-continuous in that way.
How should I drop the packet then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TCP 旨在重新发送未确认的数据包。这是协议的一部分,也是漏桶算法的基础。
因此,我能想到的丢弃数据包的唯一方法是,如果您使用的库具有在收到数据包后不发送 ACK 的功能。如果是这种情况,只需在您想要保留的所有数据包上发送 ACK 并忽略其余数据包,因为它们稍后将重新发送。
话虽如此,大多数库不会支持此功能,因为它的级别非常低,并且它们在内部处理 TCP 部分。
如果您让我们知道您希望限制速率的特定语言(和套接字实现),某人可能知道更好的限制连接的方法
TCP is designed to resend unacknowledged packets. This happens as part of the protocol and is the basis of the leaky bucket algorithm.
Because of this the only way i can think of ditching packets is if the library you are using has a function to not send on an ACK after the packet is received. If this is the case simply send an ACK on all packets you want to keep and ignore the rest as they will be resent later.
Having said that most libraries will not support this function as it is very low level and they handle the TCP part internally.
If you let us know the specific language (and socket implementation) you wish to rate-limit in someone may know a better method of throttling a connection
当使用 TCP 套接字连接时,您的程序不应该“丢弃数据包”。您想要做的是阻止您的程序在超出下载速度限制时读取更多数据。
Your program should never "drop packets" when using a TCP socket connection. What you want to do is prevent your program from reading more data if it is exceeding the download speed limit.