Merge pull request #1567 from terry-xiaoyu/clean_dead_session
clean dead persistent session on connect
This commit is contained in:
commit
2ee18ddebc
|
@ -93,7 +93,7 @@ format_variable(#mqtt_packet_connect{
|
||||||
|
|
||||||
format_variable(#mqtt_packet_connack{ack_flags = AckFlags,
|
format_variable(#mqtt_packet_connack{ack_flags = AckFlags,
|
||||||
return_code = ReturnCode}) ->
|
return_code = ReturnCode}) ->
|
||||||
io_lib:format("AckFlags=~p, RetainCode=~p", [AckFlags, ReturnCode]);
|
io_lib:format("AckFlags=~p, ReturnCode=~p", [AckFlags, ReturnCode]);
|
||||||
|
|
||||||
format_variable(#mqtt_packet_publish{topic_name = TopicName,
|
format_variable(#mqtt_packet_publish{topic_name = TopicName,
|
||||||
packet_id = PacketId}) ->
|
packet_id = PacketId}) ->
|
||||||
|
|
|
@ -228,7 +228,8 @@ process(?CONNECT_PACKET(Var), State0) ->
|
||||||
%% ACCEPT
|
%% ACCEPT
|
||||||
{?CONNACK_ACCEPT, SP, State2#proto_state{session = Session, is_superuser = IsSuperuser}};
|
{?CONNACK_ACCEPT, SP, State2#proto_state{session = Session, is_superuser = IsSuperuser}};
|
||||||
{error, Error} ->
|
{error, Error} ->
|
||||||
{stop, {shutdown, Error}, State2}
|
?LOG(error, "Username '~s' login failed for ~p", [Username, Error], State2),
|
||||||
|
{?CONNACK_SERVER, false, State2}
|
||||||
end;
|
end;
|
||||||
{error, Reason}->
|
{error, Reason}->
|
||||||
?LOG(error, "Username '~s' login failed for ~p", [Username, Reason], State1),
|
?LOG(error, "Username '~s' login failed for ~p", [Username, Reason], State1),
|
||||||
|
@ -593,4 +594,3 @@ unmount(MountPoint, Msg = #mqtt_message{topic = Topic}) ->
|
||||||
{MountPoint, Topic0} -> Msg#mqtt_message{topic = Topic0};
|
{MountPoint, Topic0} -> Msg#mqtt_message{topic = Topic0};
|
||||||
_ -> Msg
|
_ -> Msg
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
|
@ -183,15 +183,16 @@ handle_cast(Msg, State) ->
|
||||||
handle_info({'DOWN', MRef, process, DownPid, _Reason}, State) ->
|
handle_info({'DOWN', MRef, process, DownPid, _Reason}, State) ->
|
||||||
case dict:find(MRef, State#state.monitors) of
|
case dict:find(MRef, State#state.monitors) of
|
||||||
{ok, ClientId} ->
|
{ok, ClientId} ->
|
||||||
case mnesia:dirty_read({mqtt_session, ClientId}) of
|
NewState =
|
||||||
[] ->
|
case mnesia:dirty_read({mqtt_session, ClientId}) of
|
||||||
ok;
|
[] -> State;
|
||||||
[Sess = #mqtt_session{sess_pid = DownPid}] ->
|
[Sess = #mqtt_session{sess_pid = DownPid}] ->
|
||||||
mnesia:dirty_delete_object(Sess);
|
mnesia:dirty_delete_object(Sess),
|
||||||
[_Sess] ->
|
erase_monitor(MRef, State);
|
||||||
ok
|
[_Sess] ->
|
||||||
end,
|
State
|
||||||
{noreply, erase_monitor(MRef, State), hibernate};
|
end,
|
||||||
|
{noreply, NewState, hibernate};
|
||||||
error ->
|
error ->
|
||||||
lager:error("MRef of session ~p not found", [DownPid]),
|
lager:error("MRef of session ~p not found", [DownPid]),
|
||||||
{noreply, State}
|
{noreply, State}
|
||||||
|
@ -256,6 +257,7 @@ resume_session(Session = #mqtt_session{client_id = ClientId, sess_pid = SessPid}
|
||||||
{ok, SessPid};
|
{ok, SessPid};
|
||||||
false ->
|
false ->
|
||||||
?LOG(error, "Cannot resume ~p which seems already dead!", [SessPid], Session),
|
?LOG(error, "Cannot resume ~p which seems already dead!", [SessPid], Session),
|
||||||
|
remove_session(Session),
|
||||||
{error, session_died}
|
{error, session_died}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -305,4 +307,3 @@ monitor_session(ClientId, SessPid, State = #state{monitors = Monitors}) ->
|
||||||
erase_monitor(MRef, State = #state{monitors = Monitors}) ->
|
erase_monitor(MRef, State = #state{monitors = Monitors}) ->
|
||||||
erlang:demonitor(MRef, [flush]),
|
erlang:demonitor(MRef, [flush]),
|
||||||
State#state{monitors = dict:erase(MRef, Monitors)}.
|
State#state{monitors = dict:erase(MRef, Monitors)}.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue