On Jan 17, 2010, at 5:50 AM, Philliam A. wrote:
While this is an extreme example, a similar thing would happen if you
just wrote one line at a time to the socket in a while loop because it
would all happen so fast. So that’s my question, is there a limit of
what you should write to a socket, or how fast you should do it?
The short answer is that it doesn’t matter since the Ruby runtime,
the underlying OS library, and the OS itself will all work together
to buffer data and send it as fast as the network will allow.
If you write data faster than the network can drain it, your thread
will block waiting for the data to drain. That is true if you do
a single puts of 20Mb or 20 of 1Mb or …
To better understand this realize that every network connection has
an internal buffer, usually less than 64k*, and that Ruby can’t add
data to be transmitted once that buffer is full. It will be drained
as the data is sent and acknowledge by the other side and Ruby
will continue to “top it off” as long as Ruby has more data to write
(i.e. a call to puts with a 20Mb chunk of data). Your thread will
block until Ruby has been able to hand off all the data into these
NOTE: Just because data has been accepted doesn’t mean it has been
received by the other side!
- See RFC 1323 for ways in which these buffers can be larger.