首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > 云计算 >

关于Erlang Socket的三种讯息接收模式

2013-04-12 
关于Erlang Socket的三种消息接收模式转载请注明,来自:http://blog.csdn.net/skyman_2001erlang的socket有

关于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

热点排行