Use global:trans/2 to clean routes and sessions

This commit is contained in:
Feng Lee 2017-07-20 14:22:18 +08:00
parent 8277707c1b
commit 7f22265e31
2 changed files with 12 additions and 5 deletions

View File

@ -48,6 +48,8 @@
-define(ROUTER, ?MODULE). -define(ROUTER, ?MODULE).
-define(LOCK, {?ROUTER, clean_routes}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Mnesia Bootstrap %% Mnesia Bootstrap
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -240,8 +242,11 @@ handle_cast(_Msg, State) ->
{noreply, State}. {noreply, State}.
handle_info({membership, {mnesia, down, Node}}, State) -> handle_info({membership, {mnesia, down, Node}}, State) ->
global:trans({?LOCK, self()},
fun() ->
clean_routes_(Node), clean_routes_(Node),
update_stats_(), update_stats_()
end),
{noreply, State, hibernate}; {noreply, State, hibernate};
handle_info({membership, _Event}, State) -> handle_info({membership, _Event}, State) ->

View File

@ -36,6 +36,8 @@
-record(state, {stats_fun, ticker}). -record(state, {stats_fun, ticker}).
-define(LOCK, {?MODULE, clean_sessions}).
%% @doc Start a session helper %% @doc Start a session helper
-spec(start_link(fun()) -> {ok, pid()} | ignore | {error, any()}). -spec(start_link(fun()) -> {ok, pid()} | ignore | {error, any()}).
start_link(StatsFun) -> start_link(StatsFun) ->
@ -59,8 +61,8 @@ handle_info({membership, {mnesia, down, Node}}, State) ->
[{'==', {node, '$2'}, Node}], ['$1']}]), [{'==', {node, '$2'}, Node}], ['$1']}]),
lists:foreach(fun(ClientId) -> mnesia:delete({mqtt_session, ClientId}) end, ClientIds) lists:foreach(fun(ClientId) -> mnesia:delete({mqtt_session, ClientId}) end, ClientIds)
end, end,
mnesia:async_dirty(Fun), global:trans({?LOCK, self()}, fun() -> mnesia:async_dirty(Fun) end),
{noreply, State}; {noreply, State, hibernate};
handle_info({membership, _Event}, State) -> handle_info({membership, _Event}, State) ->
{noreply, State}; {noreply, State};