I'm in the middle of writing a P2P tool with openssl+WEBrick. A lot of
things are supported out of the box, but one thing I miss is throttling
or some way to limit/cap the bandwidth.
For the client part, I can do good enough things with Net::HTTP, ranges and
pauses. For the server part I want to something too.
As webrick has no throttling capacity of its own I started to dig the
archives and found 'bwlimit' as a dead and lost RAA-project. Anyone have
a copy left? Any good parts to share?
Or should I hack into webricks HTTPResponse#send_body-functions?
Chunked with timing and pauses?
-- magnus
Magnus Bodin <magnus@bodin.org> writes:
As webrick has no throttling capacity of its own I started to dig the
archives and found 'bwlimit' as a dead and lost RAA-project. Anyone have
a copy left? Any good parts to share?
Hi,
I'm the author of the dead bwlimit. It is really a simple program and
does not really do exactly what you expect it to do. Partly because of
buffering done at the OS level.
Basically it boils down to the OS sending a TCP packet whenever it
wants to. This is part of the nagle algorithm designed to reduce
network congestion by coalescing several small transmision unit into a
single big packet. The only way to disable this is to disable the
nagle algorithm by using the TCP_NODELAY socket option (note, that
this is only an advisory to the OS, the OS still is not bound to send
the packet when you write to the socket). But that introduces a
significant increase in bandwidth consumption that is more difficult
to calculate and predict. For local network, this may not be a
detrimental reason. But if you have a 500Kbps (62.5KBps) connection to
Internet and over 60%-80% is consumed by the overhead of small packets
(yes, it is a reasonable overhead figure for small packets on that
kind of connection), then you may think twice.
There are various user-level bandwidth limiter out there, and all of
them have this same problem. Bittorrent's bwlimit option does not stay
on the target bandwidth. Freenet's bwlimit does not either. The one I
have found that have the tightest output rate is trickle, but that
too, often misses its mark.
IMO, the right way to do bandwidth limiting is at the kernel-level
where you have a precise control of what is being sent, and also a
guarantee that the data is sent when you want it sent.
Anyway, I am attaching bwlimit.rb here. May you find it useful.
Yours,
YS.
bwlimit.rb (8.19 KB)
Magnus,
Sorry, should have sent you to the URL:
http://microjet.ath.cx/download/bwlimit/
YS.