Code Review: Update the zone module

1. Add force_reload/1 API
2. Change the default reload interval to 5 minutes
This commit is contained in:
Feng Lee 2018-09-07 10:23:43 +08:00
parent 328d035dab
commit d9ad29476a
1 changed files with 6 additions and 9 deletions

View File

@ -26,10 +26,9 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]). code_change/3]).
-record(state, {timer}).
-define(TAB, ?MODULE). -define(TAB, ?MODULE).
-spec(start_link() -> emqx_types:startlink_ret()).
start_link() -> start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
@ -58,7 +57,7 @@ set_env(Zone, Key, Val) ->
init([]) -> init([]) ->
_ = emqx_tables:new(?TAB, [set, {read_concurrency, true}]), _ = emqx_tables:new(?TAB, [set, {read_concurrency, true}]),
{ok, element(2, handle_info(reload, #state{}))}. {ok, element(2, handle_info(reload, #{timer => undefined}))}.
handle_call(Req, _From, State) -> handle_call(Req, _From, State) ->
emqx_logger:error("[Zone] unexpected call: ~p", [Req]), emqx_logger:error("[Zone] unexpected call: ~p", [Req]),
@ -72,11 +71,9 @@ handle_cast(Msg, State) ->
emqx_logger:error("[Zone] unexpected cast: ~p", [Msg]), emqx_logger:error("[Zone] unexpected cast: ~p", [Msg]),
{noreply, State}. {noreply, State}.
handle_info(reload, State) -> handle_info({timeout, TRef, reload}, State = #{timer := TRef}) ->
lists:foreach( [ets:insert(?TAB, [{{Zone, Key}, Val} || {Key, Val} <- Opts])
fun({Zone, Opts}) -> || {Zone, Opts} <- emqx_config:get_env(zones, [])],
[ets:insert(?TAB, {{Zone, Key}, Val}) || {Key, Val} <- Opts]
end, emqx_config:get_env(zones, [])),
{noreply, ensure_reload_timer(State), hibernate}; {noreply, ensure_reload_timer(State), hibernate};
handle_info(Info, State) -> handle_info(Info, State) ->
@ -94,5 +91,5 @@ code_change(_OldVsn, State, _Extra) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
ensure_reload_timer(State) -> ensure_reload_timer(State) ->
State#state{timer = erlang:send_after(10000, self(), reload)}. State#{timer := emqx_misc:start_timer(timer:minutes(5), reload)}.