[erlang-questions] gen_tcp send non-blocking in erlang?

Geoff Cant nem@REDACTED
Mon Mar 17 19:09:53 CET 2014

gen_tcp is a wrapper around functionality provided by a prim_inet port - and in fact gen_tcp implements a synchronous call interface to the underlying asynchronous message based port API, so if you are prepare to thoroughly void your warranty, you can do async tcp sends in Erlang.

A testament to my sins^W^W^W^Wworked example can be seen here: https://github.com/heroku/logplex/blob/master/src/logplex_tcpsyslog_drain.erl#L565, https://github.com/heroku/logplex/blob/master/src/logplex_tcpsyslog_drain.erl#L224

* erlang:port_command(TcpSocket, DataToSend, []) % Start async send
* receive {inet_reply, TcpSocket, Result} -> something() end % finish async send - result is 'ok' or {'error', Why}

You buy yourself a whole lot of hurt with this - you have to manage all your own timeouts and error detection behaviour. The upside is that the process doing the sending doesn't need to block on a selective receive, so can continue doing work (clearing a noisy incoming message queue in my example above).

tl;dr: yes, it can be done but think twice before doing so, and don't bug the OTP team if it goes wrong - they tried to warn us :)


On 2014-03-17, at 00:15 , Vinoth Kumar <vinothsparrow@REDACTED> wrote:

> Does gen_tcp supports non-blocking send ?
> I have tried gen_tcp:send which returns the ok while the packets has not been sent to the receiver. Whether any mechanism in erlang to check the sent message delivary?
> Thanks,
> Vinoth Kumar J

More information about the erlang-questions mailing list