Implement better websocket exit mechanism

This commit is contained in:
GilbertWong 2019-07-18 17:40:25 +08:00 committed by Shawn
parent 86333802b9
commit 953d320667
2 changed files with 19 additions and 9 deletions

View File

@ -331,6 +331,10 @@ update_expiry_interval(SPid, Interval) ->
close(SPid) -> close(SPid) ->
gen_server:call(SPid, close). gen_server:call(SPid, close).
-spec(close(spid(), atom()) -> ok).
close(SPid, Reason) ->
gen_server:call(SPid, {close, Reason}).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% gen_server callbacks %% gen_server callbacks
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -457,6 +461,9 @@ handle_call({pubrel, PacketId, _ReasonCode}, _From, State = #state{awaiting_rel
handle_call(close, _From, State) -> handle_call(close, _From, State) ->
{stop, normal, ok, State}; {stop, normal, ok, State};
handle_call({close, Reason}, _From, State) ->
{stop, Reason, ok, State};
handle_call(Req, _From, State) -> handle_call(Req, _From, State) ->
?LOG(error, "Unexpected call: ~p", [Req]), ?LOG(error, "Unexpected call: ~p", [Req]),
{reply, ignored, State}. {reply, ignored, State}.

View File

@ -303,20 +303,23 @@ websocket_info(Info, State) ->
?LOG(error, "Unexpected info: ~p", [Info]), ?LOG(error, "Unexpected info: ~p", [Info]),
{ok, State}. {ok, State}.
terminate(SockError, _Req, #state{keepalive = Keepalive, terminate(WsReason, _Req, #state{keepalive = Keepalive,
proto_state = ProtoState, proto_state = ProtoState,
shutdown = Shutdown}) -> shutdown = Shutdown}) ->
?LOG(debug, "Terminated for ~p, sockerror: ~p", ?LOG(debug, "Terminated for ~p, websocket reason: ~p",
[Shutdown, SockError]), [Shutdown, WsReason]),
emqx_keepalive:cancel(Keepalive), emqx_keepalive:cancel(Keepalive),
case {ProtoState, Shutdown} of case {ProtoState, Shutdown} of
{undefined, _} -> ok; {undefined, _} -> ok;
{_, {shutdown, Reason}} -> {_, {shutdown, Reason}} ->
SessionPid = emqx_protocol:session(ProtoState),
emqx_protocol:terminate(Reason, ProtoState), emqx_protocol:terminate(Reason, ProtoState),
exit(Reason); SessionPid ! {'EXIT', self(), Reason};
{_, Error} -> {_, _Error} ->
emqx_protocol:terminate(Error, ProtoState), ?LOG(info, "Terminate for unexpected error: ~p", [WsReason]),
exit({error, SockError}) SessionPid = emqx_protocol:session(ProtoState),
emqx_protocol:terminate(unknown, ProtoState),
SessionPid ! {'EXIT', self(), unknown}
end. end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------