refactor(emqx_listeners): simplify listing logic

This commit is contained in:
Zaiming Shi 2021-08-05 00:06:37 +02:00
parent e7cb9491c6
commit bdb871cb9a
1 changed files with 28 additions and 23 deletions

View File

@ -34,10 +34,15 @@
, stop_listener/3 , stop_listener/3
, restart_listener/1 , restart_listener/1
, restart_listener/3 , restart_listener/3
, has_listener_conf_by_type/1
]). ]).
%% @doc List configured listeners.
-spec(list() -> [{ListenerId :: atom(), ListenerConf :: map()}]). -spec(list() -> [{ListenerId :: atom(), ListenerConf :: map()}]).
list() -> list() ->
[{listener_id(ZoneName, LName), LConf} || {ZoneName, LName, LConf} <- do_list()].
do_list() ->
Zones = maps:to_list(emqx_config:get([zones], #{})), Zones = maps:to_list(emqx_config:get([zones], #{})),
lists:append([list(ZoneName, ZoneConf) || {ZoneName, ZoneConf} <- Zones]). lists:append([list(ZoneName, ZoneConf) || {ZoneName, ZoneConf} <- Zones]).
@ -45,26 +50,19 @@ list(ZoneName, ZoneConf) ->
Listeners = maps:to_list(maps:get(listeners, ZoneConf, #{})), Listeners = maps:to_list(maps:get(listeners, ZoneConf, #{})),
[ [
begin begin
ListenerId = listener_id(ZoneName, LName),
Running = is_running(ListenerId),
Conf = merge_zone_and_listener_confs(ZoneConf, LConf), Conf = merge_zone_and_listener_confs(ZoneConf, LConf),
{ListenerId, maps:put(running, Running, Conf)} Running = is_running(listener_id(ZoneName, LName), Conf),
{ZoneName , LName, maps:put(running, Running, Conf)}
end end
|| {LName, LConf} <- Listeners]. || {LName, LConf} <- Listeners, is_map(LConf)].
-spec is_running(ListenerId :: atom()) -> boolean() | {error, no_found}. -spec is_running(ListenerId :: atom()) -> boolean() | {error, no_found}.
is_running(ListenerId) -> is_running(ListenerId) ->
Zones = maps:to_list(emqx_config:get([zones], #{})), case lists:filtermap(fun({_Zone, Id, #{running := IsRunning}}) ->
Listeners = lists:append( Id =:= ListenerId andalso {true, IsRunning}
[ end, do_list()) of
[{listener_id(ZoneName, LName),merge_zone_and_listener_confs(ZoneConf, LConf)} [IsRunning] -> IsRunning;
|| {LName, LConf} <- maps:to_list(maps:get(listeners, ZoneConf, #{}))] [] -> {error, not_found}
|| {ZoneName, ZoneConf} <- Zones]),
case proplists:get_value(ListenerId, Listeners, undefined) of
undefined ->
{error, no_found};
Conf ->
is_running(ListenerId, Conf)
end. end.
is_running(ListenerId, #{type := tcp, bind := ListenOn})-> is_running(ListenerId, #{type := tcp, bind := ListenOn})->
@ -271,9 +269,11 @@ listener_id(ZoneName, ListenerName) ->
list_to_atom(lists:append([atom_to_list(ZoneName), ":", atom_to_list(ListenerName)])). list_to_atom(lists:append([atom_to_list(ZoneName), ":", atom_to_list(ListenerName)])).
decode_listener_id(Id) -> decode_listener_id(Id) ->
case string:split(atom_to_list(Id), ":", leading) of try
[Zone, Listen] -> {list_to_atom(Zone), list_to_atom(Listen)}; [Zone, Listen] = string:split(atom_to_list(Id), ":", leading),
_ -> error({invalid_listener_id, Id}) {list_to_existing_atom(Zone), list_to_existing_atom(Listen)}
catch
_ : _ -> error({invalid_listener_id, Id})
end. end.
ssl_opts(Opts) -> ssl_opts(Opts) ->
@ -291,11 +291,16 @@ is_ssl(Opts) ->
emqx_map_lib:deep_get([ssl, enable], Opts, false). emqx_map_lib:deep_get([ssl, enable], Opts, false).
foreach_listeners(Do) -> foreach_listeners(Do) ->
lists:foreach(fun({ZoneName, ZoneConf}) -> lists:foreach(
lists:foreach(fun({LName, LConf}) -> fun({ZoneName, LName, LConf}) ->
Do(ZoneName, LName, merge_zone_and_listener_confs(ZoneConf, LConf)) Do(ZoneName, LName, LConf)
end, maps:to_list(maps:get(listeners, ZoneConf, #{}))) end, do_list()).
end, maps:to_list(emqx_config:get([zones], #{}))).
has_listener_conf_by_type(Type) ->
lists:any(
fun({_Zone, _LName, LConf}) when is_map(LConf) ->
Type =:= maps:get(type, LConf)
end, do_list()).
%% merge the configs in zone and listeners in a manner that %% merge the configs in zone and listeners in a manner that
%% all config entries in the listener are prior to the ones in the zone. %% all config entries in the listener are prior to the ones in the zone.