Send - buffer overrun

Claes Wikstrom klacke@REDACTED
Tue Apr 4 19:29:45 CEST 2006

Mickael Remond wrote:
> Hello Eduardo,
> * Inswitch Solutions <erlang@REDACTED> [2006-04-04 12:17:34 -0300]:
>>I have a TCP/IP Client in Erlang and doing some stressing on it I've noticed that the gen_tcp:send/2 function blocks.
>>About send function in MSDN doc:
>>    "If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in a nonblocking mode"
>>In Erlang I can't use select or other async functions. Is there any solution for this in Erlang?
> The state of the discussion can be found in the following mail from
> Matthias Lang:  

Well, there is a bit more to it. If the client writes faster than
the reader can read, eventually TCP write would block.
The erlang driver, inet_drv.c use nonblocking IO and detects
this condition. It then signals the port (this is the actual
socket on the erlang side) as a "busy port".

Any erlang process which tries to write, i.e.

Port ! {self(), {command, Data}}

or any of the equivalents will be automatically suspended
until the port is marked as "not busy" by the driver.

The driver on it's side, will continue to try to write the pending
buffers, when eventually the reader actually reads, or the socket
is closed, the driver will be able to either write, in which case
it will signal the port as "not busy" and the producer (the writer)
is released from suspension, or the socket gets closed, and also
in this case the producer gets released.

The downside of this, is that it's not possible to write an erlang
program that detects that the reader isn't reading fast enough. (At least
not easily). The upside is that users don't have to bother with user space
flow control, but can rely on the allready excellent flowcontrol in TCP.
The only thing that gets suspended is the fast writer Pid, the rest of the
erlang node keeps running fine.



Claes Wikstrom                        -- Caps lock is nowhere and                  -- everything is under control
cellphone: +46 70 2097763

More information about the erlang-questions mailing list