EMQX源码分析---esockd_acceptor_sup源码分析

该模块主要是启动一个监督者模块esockd_acceptor_sup,该模块主要是对esockd_acceptor模块的工作进程进程监督。该模块对外提供了三个函数,分别如下:
1、start_link(ConnSup, TuneFun, UpgradeFuns, StatsFun, LimitFun):该函数被esockd_listener_sup模块的start_link函数内部调用,所以esockd_acceptor_sup监督者模块被esockd_listener_sup模块监听。start_link(ConnSup, TuneFun, UpgradeFuns, StatsFun, LimitFun) 函数内部调用supervisor:start_link(?MODULE, [ConnSup, TuneFun, UpgradeFuns, StatsFun, LimitFun]).函数,然后会回到该模块的init([ConnSup, TuneFun, UpgradeFuns, StatsFun, LimitFun])函数。

2、start_acceptor(AcceptorSup, LSock): 该函数是启动一个socket接收进程。

3、count_acceptors(AcceptorSup):计算该接收socket的监听进程下启动了多少个accept工作进程,处理并发socket连接。

esockd_acceptor_sup的源码如下:

-module(esockd_acceptor_sup).
-behaviour(supervisor).

-export([start_link/5]).
-export([start_acceptor/2, count_acceptors/1]).

%% Supervisor callbacks
-export([init/1]).

%%------------------------------------------------------------------------------
%% API
%%------------------------------------------------------------------------------

%% @doc Start Acceptor Supervisor.
-spec(start_link(pid(), esockd:sock_fun(), [esockd:sock_fun()], fun(), fun()) -> {ok, pid()}).
start_link(ConnSup, TuneFun, UpgradeFuns, StatsFun, LimitFun) ->
    supervisor:start_link(?MODULE, [ConnSup, TuneFun, UpgradeFuns, StatsFun, LimitFun]).

%% @doc Start a acceptor. 启动一个socket接收器
-spec(start_acceptor(pid(), inet:socket()) -> {ok, pid()} | ignore | {error, term()}).
start_acceptor(AcceptorSup, LSock) ->
    supervisor:start_child(AcceptorSup, [LSock]).

%% @doc Count acceptors. 计算 acceptors 的个数
-spec(count_acceptors(AcceptorSup :: pid()) -> pos_integer()).
count_acceptors(AcceptorSup) ->
    length(supervisor:which_children(AcceptorSup)).

%%------------------------------------------------------------------------------
%% Supervisor callbacks
%% TuneFun 读写buffer调整函数
%% UpgradeFuns 升级函数
%% StatsFun 状态统计函数
%% LimitFun 限制函数
%%------------------------------------------------------------------------------

init([ConnSup, TuneFun, UpgradeFuns, StatsFun, LimitFun]) ->
%%  定义启动esockd_acceptor模块的进程规范,然后会回调esockd_acceptor的start_link方法
    Acceptor = #{id => acceptor, start => {esockd_acceptor, start_link, [ConnSup, TuneFun, UpgradeFuns, StatsFun, LimitFun]},
                 restart => transient, shutdown => 1000, type => worker, modules => [esockd_acceptor]},
    {ok, {{simple_one_for_one, 100, 3600}, [Acceptor]}}.

接下来将介绍 esockd_listener 模块。

你可能感兴趣的:(Erlang)