refactor(emqx_cm_sup): Internal functions to create workers.
This commit is contained in:
parent
c54847b6e9
commit
bcebe1de24
|
@ -22,49 +22,38 @@
|
||||||
|
|
||||||
-export([init/1]).
|
-export([init/1]).
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% API
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
start_link() ->
|
start_link() ->
|
||||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% Supervisor callbacks
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
Banned = #{id => banned,
|
|
||||||
start => {emqx_banned, start_link, []},
|
|
||||||
restart => permanent,
|
|
||||||
shutdown => 1000,
|
|
||||||
type => worker,
|
|
||||||
modules => [emqx_banned]},
|
|
||||||
Flapping = #{id => flapping,
|
|
||||||
start => {emqx_flapping, start_link, []},
|
|
||||||
restart => permanent,
|
|
||||||
shutdown => 1000,
|
|
||||||
type => worker,
|
|
||||||
modules => [emqx_flapping]},
|
|
||||||
%% Channel locker
|
|
||||||
Locker = #{id => locker,
|
|
||||||
start => {emqx_cm_locker, start_link, []},
|
|
||||||
restart => permanent,
|
|
||||||
shutdown => 5000,
|
|
||||||
type => worker,
|
|
||||||
modules => [emqx_cm_locker]
|
|
||||||
},
|
|
||||||
%% Channel registry
|
|
||||||
Registry = #{id => registry,
|
|
||||||
start => {emqx_cm_registry, start_link, []},
|
|
||||||
restart => permanent,
|
|
||||||
shutdown => 5000,
|
|
||||||
type => worker,
|
|
||||||
modules => [emqx_cm_registry]
|
|
||||||
},
|
|
||||||
%% Channel Manager
|
|
||||||
Manager = #{id => manager,
|
|
||||||
start => {emqx_cm, start_link, []},
|
|
||||||
restart => permanent,
|
|
||||||
shutdown => 5000,
|
|
||||||
type => worker,
|
|
||||||
modules => [emqx_cm]
|
|
||||||
},
|
|
||||||
SupFlags = #{strategy => one_for_one,
|
SupFlags = #{strategy => one_for_one,
|
||||||
intensity => 100,
|
intensity => 100,
|
||||||
period => 10
|
period => 10
|
||||||
},
|
},
|
||||||
|
Banned = child_spec(emqx_banned, 1000, worker),
|
||||||
|
Flapping = child_spec(emqx_flapping, 1000, worker),
|
||||||
|
Locker = child_spec(emqx_cm_locker, 5000, worker),
|
||||||
|
Registry = child_spec(emqx_cm_registry, 5000, worker),
|
||||||
|
Manager = child_spec(emqx_cm, 5000, worker),
|
||||||
{ok, {SupFlags, [Banned, Flapping, Locker, Registry, Manager]}}.
|
{ok, {SupFlags, [Banned, Flapping, Locker, Registry, Manager]}}.
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% Internal functions
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
child_spec(Mod, Shutdown, Type) ->
|
||||||
|
#{id => Mod,
|
||||||
|
start => {Mod, start_link, []},
|
||||||
|
restart => permanent,
|
||||||
|
shutdown => Shutdown,
|
||||||
|
type => Type,
|
||||||
|
modules => [Mod]
|
||||||
|
}.
|
||||||
|
|
|
@ -114,8 +114,8 @@ t_cm(_) ->
|
||||||
emqx_config:put_zone_conf(default, [mqtt, idle_timeout], 15000).
|
emqx_config:put_zone_conf(default, [mqtt, idle_timeout], 15000).
|
||||||
|
|
||||||
t_cm_registry(_) ->
|
t_cm_registry(_) ->
|
||||||
Info = supervisor:which_children(emqx_cm_sup),
|
Children = supervisor:which_children(emqx_cm_sup),
|
||||||
{_, Pid, _, _} = lists:keyfind(registry, 1, Info),
|
{_, Pid, _, _} = lists:keyfind(emqx_cm_registry, 1, Children),
|
||||||
ignored = gen_server:call(Pid, <<"Unexpected call">>),
|
ignored = gen_server:call(Pid, <<"Unexpected call">>),
|
||||||
gen_server:cast(Pid, <<"Unexpected cast">>),
|
gen_server:cast(Pid, <<"Unexpected cast">>),
|
||||||
Pid ! <<"Unexpected info">>.
|
Pid ! <<"Unexpected info">>.
|
||||||
|
|
|
@ -55,8 +55,8 @@ t_detect_check(_) ->
|
||||||
true = emqx_banned:check(ClientInfo),
|
true = emqx_banned:check(ClientInfo),
|
||||||
timer:sleep(3000),
|
timer:sleep(3000),
|
||||||
false = emqx_banned:check(ClientInfo),
|
false = emqx_banned:check(ClientInfo),
|
||||||
Childrens = supervisor:which_children(emqx_cm_sup),
|
Children = supervisor:which_children(emqx_cm_sup),
|
||||||
{flapping, Pid, _, _} = lists:keyfind(flapping, 1, Childrens),
|
{emqx_flapping, Pid, _, _} = lists:keyfind(emqx_flapping, 1, Children),
|
||||||
gen_server:call(Pid, unexpected_msg),
|
gen_server:call(Pid, unexpected_msg),
|
||||||
gen_server:cast(Pid, unexpected_msg),
|
gen_server:cast(Pid, unexpected_msg),
|
||||||
Pid ! test,
|
Pid ! test,
|
||||||
|
@ -72,4 +72,4 @@ t_expired_detecting(_) ->
|
||||||
(_) -> false end, ets:tab2list(emqx_flapping))),
|
(_) -> false end, ets:tab2list(emqx_flapping))),
|
||||||
timer:sleep(200),
|
timer:sleep(200),
|
||||||
?assertEqual(true, lists:all(fun({flapping, <<"client008">>, _, _, _}) -> false;
|
?assertEqual(true, lists:all(fun({flapping, <<"client008">>, _, _, _}) -> false;
|
||||||
(_) -> true end, ets:tab2list(emqx_flapping))).
|
(_) -> true end, ets:tab2list(emqx_flapping))).
|
||||||
|
|
Loading…
Reference in New Issue