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?
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