Erlang R17 new socket options {active, N}

Source: Internet
Author: User

Erlang R17 introduces the new socket option {active, N}. Together with {active, once}, it provides traffic control for the application layer. Why does the option {active, once} effectively suppress a large number of socket messages?

We know that {active, once} has to reset the active option every time it receives the packet to continue receiving erlang message notifications. In fact, setting {active, once} means calling epoll_ctl once. If the request is too frequent, there will be a large number of epoll_ctl calls. Currently, only one thread in erlang can harvest epoll_wait events. epoll_wait needs to poll the ready ctl queue. If a large number of ctl events block epoll_wait operations, the network processing capability may decrease.

So, can we set whether to receive N socket messages and then execute epoll_ctl again, which can effectively reduce epoll_ctl calls, {active, N} is like this.

Next let's take a look at the description of {active, N }.

Add the {active, N} socket option for TCP, UDP, and SCTP, where N is an integer in the range-32768 .. 32767, to allow a caller to specify the number of data messages to be delivered to the controlling process. once the socket's delivered message count either reaches 0 or is explicitly set to 0 with inet: setopts/2 or by including {active, 0} as an option when the socket is created, the socket transitions to passive ({active, false}) mode and the socket's controlling process has es a message to inform it of the transition. TCP sockets receive {tcp_passive, Socket}, UDP sockets receive {udp_passive, Socket} and SCTP sockets receive {sctp_passive, Socket }.

The socket's delivered message counter defaults to 0, but it can be set using {active, N} via any gen_tcp, gen_udp, or gen_sctp function that takes socket options as arguments, or via inet: setopts/2. new N values are added to the socket's current counter value, and negative numbers can be used to reduce the counter value. specifying a number that wocould cause the socket's counter value to go abve 32767 causes an einval error. if a negative number is specified such that the counter value wocould become negative, the socket's counter value is set to 0 and the socket transitions to passive mode. if the counter value is already 0 and inet: setopts (Socket, [{active, 0}]) is specified, the counter value remains at 0 but the appropriate passive mode transition message is generated for the socket.

After the {active, N} Option is set, the process receives the {tcp_passive, Socket} message after receiving N Packets, meaning that the Socket enters the passive mode, you need to reset the active option.

Next, test this parameter in the actual example:

-module(server).-export([start/0]).-export([continue/1]).-define( PORT, 8888).start() ->  {ok, LSock} = gen_tcp:listen(?PORT, [binary, {packet, 0},{active, false}]),  io:format("socket listen: ~p on ~p ~n",[LSock, ?PORT]),  accept(LSock).accept(LSock) ->  {ok, ASock} = gen_tcp:accept(LSock),   Pid = spawn(fun() -> do_loop(ASock) end),  gen_tcp:controlling_process(ASock, Pid),  inet:setopts(ASock, [{active, 3}]),  accept(LSock).do_loop(ASock) ->  receive    {tcp, Socket, Data} ->       io:format("socket ~p recv: ~p ~n",[Socket, Data]);    {tcp_closed, Socket} ->       io:format("socket ~p close ~n",[Socket]);    {tcp_passive,Socket} ->       io:format("socket ~p is passive, please call continue/1 ~p ~n",[Socket, self()]);    release_passive ->       inet:setopts(ASock, [{active, 3}]);    Err ->       io:format("socket may error: ~p ~n",[Err])  end,  do_loop(ASock).continue(Pid) ->  Pid ! release_passive,  ok.
After compiling and starting this module, we create a client to request this server:

1> f(S), {ok,S} = gen_tcp:connect({127,0,0,1},8888,[{packet,0}]).{ok,#Port<0.526>}2> gen_tcp:send(S,<<"hello">>).ok3> gen_tcp:send(S,<<"hello">>).ok4> gen_tcp:send(S,<<"hello">>).ok5> gen_tcp:send(S,<<"hello">>).ok
The server console prints the following information:

D:\tmp>erl -s serversocket listen: #Port<0.422> on 8888Eshell V6.0  (abort with ^G)1> socket #Port<0.479> recv: <<"hello">>1> socket #Port<0.479> recv: <<"hello">>1> socket #Port<0.479> recv: <<"hello">>1> socket #Port<0.479> is passive, please call continue/1 <0.33.0>1> server:continue(pid(0,33,0)).socket #Port<0.479> recv: <<"hello">>ok2>
In the above example, we set the {active, 3} Option. After receiving the client three times of data, the socket enters the passive state and the {active, n} to continue receiving tcp messages.

So, how do I use the {active, N} Option in the actual project?

inet:setopts(Socket, [{active, 300}]),erlang:send_after(30 * 1000, self(), release_passive);
The general idea is that a maximum of 300 packets can be received within 30 seconds. If the number of packets exceeds 30 seconds, the packets will not be received. After the time is 30 seconds, the packets will be received again and again.

This can also be used to add one more counter. If you enter the passive status more than 10 times, it indicates that this Socket has a problem and there is an attack.


Refer:

Http://blog.csdn.net/mycwq/article/details/24814843
Http://www.erlang.org/download/otp_src_17.0.readme
Http://blog.yufeng.info/archives/2970

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.