關於Erlang Socket的三種消息接收模式

轉載請註明,來自:http://blog.csdn.net/skyman_2001

erlang的socket有3種消息接收模式:active、passive和active once,可以在gen_tcp:connect/3或gen_tcp:listen/2裏設置{active, true | false |once}來實現,也可以用inet:setopts/2來動態設置。這3種模式的區別是:

1. active(主動消息接收):非阻塞。當數據到達時系統會向控制進程發送{tcp, Socket, Data}消息。控制進程無法控制消息流;

2. passive(被動消息接收):阻塞。控制進程必須主動調用recv()來接收消息。可以控制消息流;

3. active once(混合消息接收):半阻塞。這種模式是主動的但僅針對一個消息,在控制進程收到一個消息後,必須顯式調用inet:setopts(Socket, [{active, once}])來重新

激活以接收下一個消息。可以進行流量控制。這種模式相對於被動模式來說,有個優點是可以同時等待多個socket的數據。


Erlang官方推薦使用active once模式:

Active, once is the recommended way to implement a server in both UDP and
TCP.

The use of active once is superior to the other alternatives from a
programmatical standpoint, it is clean and robust. I don't recommend the
use of active true or active false just for performance reasons, because it
will cause other problems instead.

We are currently working with improvements regarding active once for TCP
since it obviously seems to be slower than necessary. Most probably it is
the same with UDP and we will look into that as well.

We don't think there need any significant performance difference between
active once and the other alternatives and hope to have a solution
confirming that soon (meaning r15b02 or 03)

Regards Kenneth Erlang/OTP, Ericsson

詳見:http://erlang.org/pipermail/erlang-questions/2012-April/065991.html


R15B02對active once進行了優化,降低延遲,最大吞吐量提高4~6倍。詳見:https://github.com/erlang/otp/commit/689ba6e5657867eb3efbcb9a2dbec4aa98ddac5d

發佈了184 篇原創文章 · 獲贊 13 · 訪問量 71萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章