[Erlang 0017] Erlang/OTP Basic module proc_lib

Source: Internet
Author: User

Hello, everyone. We will continue with the previous article on Saturday to share with you the mochiweb source code. Last article, we finally sawMochiweb_socket_server: Listen/3Function:

listen(Port, Opts, State=#mochiweb_socket_server{ssl=Ssl, ssl_opts=SslOpts}) ->    case mochiweb_socket:listen(Ssl, Port, Opts, SslOpts) of        {ok, Listen} ->            {ok, ListenPort} = mochiweb_socket:port(Listen),            {ok, new_acceptor_pool(                   Listen,                   State#mochiweb_socket_server{listen=Listen,                                                port=ListenPort})};        {error, Reason} ->            {stop, Reason}    end.

Let's take a look at the last call line.Mochiweb_socket_server: new_acceptor_pool/2Function, and returns {OK, returned result of this function}. The complete code of this function is as follows:

new_acceptor_pool(Listen,                  State=#mochiweb_socket_server{acceptor_pool=Pool,                                                acceptor_pool_size=Size,                                                loop=Loop}) ->    F = fun (_, S) ->                Pid = mochiweb_acceptor:start_link(self(), Listen, Loop),                sets:add_element(Pid, S)        end,    Pool1 = lists:foldl(F, Pool, lists:seq(1, Size)),    State#mochiweb_socket_server{acceptor_pool=Pool1}.

This function first defines the anonymous function F, which accepts two parameters. The first parameter is discarded, and the other parameter is S. Then let's look at the first line and callMochiweb_acceptor: start_link/3Function, return the process id pid, and then call the system function.Sets: add_element/2Let's take a look at this system function. Erlang Doc address: http://www.erlang.org/doc/man/sets.html#add_element-2,5, for example:

A new set is returned, which is formed by the set1 inserted element.

Now, let's figure out the logic of anonymous functions.Mochiweb_acceptor: start_link/3The specific internal logic of the function. Now you only need to know that it will return a process identifier.

Let's continue:

Pool1 = lists:foldl(F, Pool, lists:seq(1, Size)),

CallLists: seq/2Function to generate a list from 1 to size. Here: acceptor_pool_size = size = 16. The default value of the field of acceptor_pool_size recorded by mochiweb_socket_server is used. Then, call the anonymous function F for this list in sequence, and pass the element values in the list and the pool variable. Here: acceptor_pool = pool = sets: New (), the default value of the acceptor_pool field of the mochiweb_socket_server record is also obtained.Note:The first parameter of the previously Anonymous function f is the discarded value: _, indicating that the value of this element is not needed here, but the anonymous function is called cyclically for size times, each time, a new element is added based on the previous set and the returned element is returned.

Finally, change the value of the acceptor_pool field of State # mochiweb_socket_server to the latest set type variable pool1.

Okay, so far, we have understood the new_acceptor_pool function. Let's look back at it. We haven't read it in detail before.Mochiweb_acceptor: start_link/3Function:

start_link(Server, Listen, Loop) ->    proc_lib:spawn_link(?MODULE, init, [Server, Listen, Loop]).

This function is very simple, just a line of logic, calling the system function:Proc_lib: spawn_link/3, Erlang Doc address: http://www.erlang.org/doc/man/proc_lib.html?spawn_link-3,e. g:

This function is similar to the spawn_link function in the Erlang module. It initializes a process to execute the specified function in the specified module. What are the differences between processes created using proc_lib? I am not going to talk about it here. For details, refer to the article [Erlang 2002] Erlang/OTP Basic module proc_lib.

Next, let's take a look.Mochiweb_acceptor: init/3The complete code is as follows:

init(Server, Listen, Loop) ->    T1 = now(),    case catch mochiweb_socket:accept(Listen) of        {ok, Socket} ->            gen_server:cast(Server, {accepted, self(), timer:now_diff(now(), T1)}),            call_loop(Loop, Socket);        {error, closed} ->            exit(normal);        {error, timeout} ->            init(Server, Listen, Loop);        {error, esslaccept} ->            exit(normal);        Other ->            error_logger:error_report(              [{application, mochiweb},               "Accept failed error",               lists:flatten(io_lib:format("~p", [Other]))]),            exit({error, accept_failed})    end.

Next, let's take a closer look at the specific logic of this function:

T1 = now()

This line calls the system functionErlang: Now/0, Erlang Doc address: http://www.erlang.org/doc/man/erlang.html?now-0, for example:

The test is as follows:

  

Okay, this article is here. Next, we willMochiweb_socket: accept/1Continue.

Finally, thank you for your support. Good night.

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.