消息、接收-关于Erlang Socket的三种消息接收模式-by小雨

本篇文章笔者在深圳吃饭的时候突然想到的...最近就有想写几篇关于消息、接收-的笔记,所以回家到之后就奋笔疾书的写出来发布了

    转载请注明,来自: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

文章结束给大家分享下程序员的一些笑话语录: 据说有一位软件工程师,一位硬件工程师和一位项目经理同坐车参加研讨会。不幸在从盘山公路下山时坏在半路上了。于是两位工程师和一位经理就如何修车的问题展开了讨论。
硬件工程师说:“我可以用随身携带的瑞士军刀把车坏的部分拆下来,找出原因,排除故障。”
项目经理说:“根据经营管理学,应该召开会议,根据问题现状写出需求报告,制订计划,编写日程安排,逐步逼近,alpha测试,beta1测试和beta2测试解决问题。”
软件工程说:“咱们还是应该把车推回山顶再开下来,看看问题是否重复发生。”

你可能感兴趣的:(socket)