From 10b01a34ef2492fb8104924e78f411f84266f6cc Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Mon, 5 Jul 2021 19:12:26 +0800 Subject: [PATCH] refactor(listeners): reformat the code for starting stopping listeners --- apps/emqx/src/emqx_listeners.erl | 39 +++++++++++++++++++------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/apps/emqx/src/emqx_listeners.erl b/apps/emqx/src/emqx_listeners.erl index aa4fbe7f1..b9830578a 100644 --- a/apps/emqx/src/emqx_listeners.erl +++ b/apps/emqx/src/emqx_listeners.erl @@ -36,11 +36,7 @@ %% @doc Start all listeners. -spec(start() -> ok). start() -> - lists:foreach(fun({ZoneName, ZoneConf}) -> - lists:foreach(fun({LName, LConf}) -> - start_listener(ZoneName, LName, LConf) - end, maps:to_list(maps:get(listeners, ZoneConf, #{}))) - end, maps:to_list(emqx_config:get([zones], #{}))). + foreach_listeners(fun start_listener/3). -spec(start_listener(atom()) -> ok). start_listener(Id) -> @@ -93,7 +89,12 @@ esockd_opts(Opts0) -> Rate -> Opts1#{max_conn_rate => Rate} end, Opts3 = Opts2#{access_rules => esockd_access_rules(maps:get(access_rules, Opts0, []))}, - maps:to_list(Opts3#{ssl_options => ssl_opts(Opts0), tcp_options => tcp_opts(Opts0)}). + maps:to_list(case is_ssl(Opts0) of + false -> + Opts3#{tcp_options => tcp_opts(Opts0)}; + true -> + Opts3#{ssl_options => ssl_opts(Opts0), tcp_options => tcp_opts(Opts0)} + end). ws_opts(ZoneName, ListenerName, Opts) -> WsPaths = [{maps:get(mqtt_path, Opts, "/mqtt"), emqx_ws_connection, @@ -128,11 +129,7 @@ esockd_access_rules(StrRules) -> %% @doc Restart all listeners -spec(restart() -> ok). restart() -> - lists:foreach(fun({ZoneName, ZoneConf}) -> - lists:foreach(fun({LName, LConf}) -> - restart_listener(ZoneName, LName, LConf) - end, maps:to_list(maps:get(listeners, ZoneConf, #{}))) - end, maps:to_list(emqx_config:get([zones], #{}))). + foreach_listeners(fun restart_listener/3). -spec(restart_listener(atom()) -> ok | {error, any()}). restart_listener(ListenerID) -> @@ -150,11 +147,7 @@ restart_listener(ZoneName, ListenerName, Conf) -> %% @doc Stop all listeners. -spec(stop() -> ok). stop() -> - lists:foreach(fun({ZoneName, ZoneConf}) -> - lists:foreach(fun({LName, LConf}) -> - stop_listener(ZoneName, LName, LConf) - end, maps:to_list(maps:get(listeners, ZoneConf, #{}))) - end, maps:to_list(emqx_config:get([zones], #{}))). + foreach_listeners(fun stop_listener/3). -spec(stop_listener(atom()) -> ok | {error, term()}). stop_listener(ListenerID) -> @@ -205,3 +198,17 @@ tcp_opts(Opts) -> is_ssl(Opts) -> emqx_map_lib:deep_get([ssl, enable], Opts, false). + +foreach_listeners(Do) -> + lists:foreach(fun({ZoneName, ZoneConf}) -> + lists:foreach(fun({LName, LConf}) -> + Do(ZoneName, LName, merge_zone_and_listener_confs(ZoneConf, LConf)) + end, maps:to_list(maps:get(listeners, ZoneConf, #{}))) + end, maps:to_list(emqx_config:get([zones], #{}))). + +%% 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. +merge_zone_and_listener_confs(ZoneConf, ListenerConf) -> + ConfsInZonesOnly = [listeners, overall_max_connections], + BaseConf = maps:without(ConfsInZonesOnly, ZoneConf), + emqx_map_lib:deep_merge(BaseConf, ListenerConf).