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(LOCK, {?ROUTER, clean_routes}).
%%--------------------------------------------------------------------
%% Mnesia Bootstrap
%%--------------------------------------------------------------------
@ -231,7 +233,7 @@ handle_cast({add_local_route, Topic}, State) ->
%% why node()...?
ets:insert(mqtt_local_route, {Topic, node()}),
{noreply, State};
handle_cast({del_local_route, Topic}, State) ->
ets:delete(mqtt_local_route, Topic),
{noreply, State};
@ -240,8 +242,11 @@ handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({membership, {mnesia, down, Node}}, State) ->
clean_routes_(Node),
update_stats_(),
global:trans({?LOCK, self()},
fun() ->
clean_routes_(Node),
update_stats_()
end),
{noreply, State, hibernate};
handle_info({membership, _Event}, State) ->

View File

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