From d52e84bdd97b93d7d9cea827de57bd4a0edea9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 10 Oct 2019 11:33:35 +0200 Subject: Add shutdown_reason Websocket command This allows changing the normal exit reason of Websocket processes, providing a way to signal other processes of why the exit occurred. --- src/cowboy_websocket.erl | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/cowboy_websocket.erl') diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl index ad0dad5..31103ac 100644 --- a/src/cowboy_websocket.erl +++ b/src/cowboy_websocket.erl @@ -35,6 +35,7 @@ | {active, boolean()} | {deflate, boolean()} | {set_options, map()} + | {shutdown_reason, any()} ]. -export_type([commands/0]). @@ -95,7 +96,8 @@ utf8_state :: cow_ws:utf8_state(), deflate = true :: boolean(), extensions = #{} :: map(), - req = #{} :: map() + req = #{} :: map(), + shutdown_reason = normal :: any() }). %% Because the HTTP/1.1 and HTTP/2 handshakes are so different, @@ -546,6 +548,8 @@ commands([{set_options, SetOpts}|Tail], State0=#state{opts=Opts}, Data) -> State0 end, commands(Tail, State, Data); +commands([{shutdown_reason, ShutdownReason}|Tail], State, Data) -> + commands(Tail, State#state{shutdown_reason=ShutdownReason}, Data); commands([Frame|Tail], State, Data0) -> Data = [frame(Frame, State)|Data0], case is_close_frame(Frame) of @@ -623,9 +627,12 @@ frame(Frame, #state{extensions=Extensions}) -> cow_ws:frame(Frame, Extensions). -spec terminate(#state{}, any(), terminate_reason()) -> no_return(). -terminate(State, HandlerState, Reason) -> +terminate(State=#state{shutdown_reason=Shutdown}, HandlerState, Reason) -> handler_terminate(State, HandlerState, Reason), - exit(normal). + case Shutdown of + normal -> exit(normal); + _ -> exit({shutdown, Shutdown}) + end. handler_terminate(#state{handler=Handler, req=Req}, HandlerState, Reason) -> cowboy_handler:terminate(Reason, Req, HandlerState, Handler). -- cgit v1.2.3