Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you rate-limit an IO operation?

Suppose you have a program which reads from a socket. How do you keep the download rate below a certain given threshold?

like image 876
blackwing Avatar asked Sep 18 '08 17:09

blackwing


1 Answers

At the application layer (using a Berkeley socket style API) you just watch the clock, and read or write data at the rate you want to limit at.

If you only read 10kbps on average, but the source is sending more than that, then eventually all the buffers between it and you will fill up. TCP/IP allows for this, and the protocol will arrange for the sender to slow down (at the application layer, probably all you need to know is that at the other end, blocking write calls will block, nonblocking writes will fail, and asynchronous writes won't complete, until you've read enough data to allow it).

At the application layer you can only be approximate - you can't guarantee hard limits such as "no more than 10 kb will pass a given point in the network in any one second". But if you keep track of what you've received, you can get the average right in the long run.

like image 68
Steve Jessop Avatar answered Oct 13 '22 11:10

Steve Jessop