refactor(listeners): reformat the code for starting stopping listeners
This commit is contained in:
parent
6d92d87ae7
commit
10b01a34ef
|
@ -36,11 +36,7 @@
|
||||||
%% @doc Start all listeners.
|
%% @doc Start all listeners.
|
||||||
-spec(start() -> ok).
|
-spec(start() -> ok).
|
||||||
start() ->
|
start() ->
|
||||||
lists:foreach(fun({ZoneName, ZoneConf}) ->
|
foreach_listeners(fun start_listener/3).
|
||||||
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], #{}))).
|
|
||||||
|
|
||||||
-spec(start_listener(atom()) -> ok).
|
-spec(start_listener(atom()) -> ok).
|
||||||
start_listener(Id) ->
|
start_listener(Id) ->
|
||||||
|
@ -93,7 +89,12 @@ esockd_opts(Opts0) ->
|
||||||
Rate -> Opts1#{max_conn_rate => Rate}
|
Rate -> Opts1#{max_conn_rate => Rate}
|
||||||
end,
|
end,
|
||||||
Opts3 = Opts2#{access_rules => esockd_access_rules(maps:get(access_rules, Opts0, []))},
|
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) ->
|
ws_opts(ZoneName, ListenerName, Opts) ->
|
||||||
WsPaths = [{maps:get(mqtt_path, Opts, "/mqtt"), emqx_ws_connection,
|
WsPaths = [{maps:get(mqtt_path, Opts, "/mqtt"), emqx_ws_connection,
|
||||||
|
@ -128,11 +129,7 @@ esockd_access_rules(StrRules) ->
|
||||||
%% @doc Restart all listeners
|
%% @doc Restart all listeners
|
||||||
-spec(restart() -> ok).
|
-spec(restart() -> ok).
|
||||||
restart() ->
|
restart() ->
|
||||||
lists:foreach(fun({ZoneName, ZoneConf}) ->
|
foreach_listeners(fun restart_listener/3).
|
||||||
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], #{}))).
|
|
||||||
|
|
||||||
-spec(restart_listener(atom()) -> ok | {error, any()}).
|
-spec(restart_listener(atom()) -> ok | {error, any()}).
|
||||||
restart_listener(ListenerID) ->
|
restart_listener(ListenerID) ->
|
||||||
|
@ -150,11 +147,7 @@ restart_listener(ZoneName, ListenerName, Conf) ->
|
||||||
%% @doc Stop all listeners.
|
%% @doc Stop all listeners.
|
||||||
-spec(stop() -> ok).
|
-spec(stop() -> ok).
|
||||||
stop() ->
|
stop() ->
|
||||||
lists:foreach(fun({ZoneName, ZoneConf}) ->
|
foreach_listeners(fun stop_listener/3).
|
||||||
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], #{}))).
|
|
||||||
|
|
||||||
-spec(stop_listener(atom()) -> ok | {error, term()}).
|
-spec(stop_listener(atom()) -> ok | {error, term()}).
|
||||||
stop_listener(ListenerID) ->
|
stop_listener(ListenerID) ->
|
||||||
|
@ -205,3 +198,17 @@ tcp_opts(Opts) ->
|
||||||
|
|
||||||
is_ssl(Opts) ->
|
is_ssl(Opts) ->
|
||||||
emqx_map_lib:deep_get([ssl, enable], Opts, false).
|
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).
|
||||||
|
|
Loading…
Reference in New Issue