[ejabberd] blocking send and latency

Jack Moffitt jack at chesspark.com
Tue Oct 28 07:55:10 MSK 2008


> prim_inet:send/2 is not blocking in general. It hangs when there is a lot of
> data in the sndbuf to be sent (this happens for example when a receiver is
> too slow to read its socket).

You could say the same thing for the send() system call in general.
It will execute pretty much instantly if the buffer has room.  You
still cannot depend on this behavior, and so the common design is to
use the nonblocking send which writes as much data as it can and
returns.  Usually some queue of data to be sent is kept, and a pointer
to where in the queue we are at.  Each time through the event loop you
try and send as much as you can, and receive as much as you can.

In Erlang, it seems like the recv part is already non-blocking.  You
get async messages whenever the socket receives data.  But sending
there seems to be no equivalent.  Sending to a separate process and
using the mailbox as a buffer appears to be the only way.

> To avoid this situation {send_timeout,
> Integer} option was introduced in Elang R11-B4: if prim_inet:send/2 hangs
> more than Integer milliseconds, gen_tcp:send/2 returns {error, timeout}.

This doesn't really help because you will have to attempt a resend,
which may also block, therefore you'd never be able to send that data
(potentially).

>> The solution to this is just to use another process and send it messages,
>> but I
>> noticed that ejabberd does not seem to do this.
>>
>
> I don't think this is a solution really, since you can easily overflow
> mailbox of sending process.

Is this worse than blocking the entire socket randomly?  Sure, in bad
situations you may overflow the mailbox, or queue too much data in
some other manner, but this is always a danger in any application that
uses the network.  In any case, the client is not able to participate,
and should be disconnected.

It could be I'm missing something.  I'm very familiar with
non-blocking server design in C and Python, but am relatively new to
Erlang.

jack.


More information about the ejabberd mailing list