0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Rename 'shutdown' close reason and tuples to 'stop'

The 'shutdown' atom has a specific meaning inside OTP. We are
instead going to use 'stop' which is pretty much the equivalent
of what we actually do. 'shutdown' is now reserved for future
special processes implementation.
This commit is contained in:
Loïc Hoguin 2014-11-07 19:22:36 +02:00
parent 903f6f4c7d
commit 8cbd8c1882
10 changed files with 46 additions and 47 deletions

View file

@ -61,7 +61,7 @@ message otherwise.
``` erlang
info({reply, Body}, Req, State) ->
Req2 = cowboy_req:reply(200, [], Body, Req),
{shutdown, Req2, State};
{stop, Req2, State};
info(_Msg, Req, State) ->
{ok, Req, State, hibernate}.
```
@ -76,7 +76,7 @@ return a tuple indicating if more messages are to be expected.
The callback may also choose to do nothing at all and just
skip the message received.
If a reply is sent, then the `shutdown` tuple should be returned.
If a reply is sent, then the `stop` tuple should be returned.
This will instruct Cowboy to end the request.
Otherwise an `ok` tuple should be returned.
@ -99,7 +99,7 @@ init(Req, Opts) ->
{cowboy_loop, Req2, Opts}.
info(eof, Req, State) ->
{shutdown, Req, State};
{stop, Req, State};
info({chunk, Chunk}, Req, State) ->
cowboy_req:chunk(Chunk, Req),
{ok, Req, State};

View file

@ -42,7 +42,7 @@ init(Req, _Opts) ->
<<"mychat2">>, Req),
{ok, Req2, #state{}};
false ->
{shutdown, Req, undefined}
{stop, Req, undefined}
end
end.
```
@ -75,7 +75,7 @@ ping or pong frame arrives from the client. Note that in the
case of ping and pong frames, no action is expected as Cowboy
automatically replies to ping frames.
The handler can decide to send frames to the socket, shutdown
The handler can decide to send frames to the socket, stop
or just continue without sending anything.
The following snippet echoes back any text frame received and
@ -93,7 +93,7 @@ websocket_handle(_Frame, Req, State) ->
Cowboy will call `websocket_info/3` whenever an Erlang message
arrives.
The handler can decide to send frames to the socket, shutdown
The handler can decide to send frames to the socket, stop
or just continue without sending anything.
The following snippet forwards any `log` message to the socket

View file

@ -29,10 +29,10 @@ The connection was closed normally before switching to the
loop sub protocol. This typically happens if an `ok` tuple is
returned from the `init/2` callback.
: shutdown
: stop
The handler requested to close the connection by returning
a `shutdown` tuple.
a `stop` tuple.
: timeout
@ -72,7 +72,7 @@ A socket error ocurred.
: info(Info, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {shutdown, Req, State}
| {stop, Req, State}
Types:
@ -85,7 +85,7 @@ Handle the Erlang message received.
This function will be called every time an Erlang message
has been received. The message can be any Erlang term.
The `shutdown` return value can be used to stop the receive loop,
The `stop` return value can be used to stop the receive loop,
typically because a response has been sent.
The `hibernate` option will hibernate the process until

View file

@ -69,10 +69,10 @@ further details.
The remote endpoint closed the connection with the given
`Code` and `Payload` as the reason.
: shutdown
: stop
The handler requested to close the connection, either by returning
a `shutdown` tuple or by sending a `close` frame.
a `stop` tuple or by sending a `close` frame.
: timeout
@ -111,7 +111,7 @@ A socket error ocurred.
| {ok, Req, State, hibernate}
| {reply, OutFrame | [OutFrame], Req, State}
| {reply, OutFrame | [OutFrame], Req, State, hibernate}
| {shutdown, Req, State}
| {stop, Req, State}
Types:
@ -125,7 +125,7 @@ Handle the data received from the Websocket connection.
This function will be called every time data is received
from the Websocket connection.
The `shutdown` return value can be used to close the
The `stop` return value can be used to close the
connection. A close reply will also result in the connection
being closed.
@ -138,7 +138,7 @@ Erlang message.
| {ok, Req, State, hibernate}
| {reply, OutFrame | [OutFrame], Req, State}
| {reply, OutFrame | [OutFrame], Req, State, hibernate}
| {shutdown, Req, State}
| {stop, Req, State}
Types:
@ -152,7 +152,7 @@ Handle the Erlang message received.
This function will be called every time an Erlang message
has been received. The message can be any Erlang term.
The `shutdown` return value can be used to close the
The `stop` return value can be used to close the
connection. A close reply will also result in the connection
being closed.

View file

@ -36,7 +36,7 @@
-callback info(any(), Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {shutdown, Req, State}
| {stop, Req, State}
when Req::cowboy_req:req(), State::any().
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
@ -153,8 +153,8 @@ call(Req, State=#state{resp_sent=RespSent},
after_call(Req2, State, Handler, HandlerState2);
{ok, Req2, HandlerState2, hibernate} ->
after_call(Req2, State#state{hibernate=true}, Handler, HandlerState2);
{shutdown, Req2, HandlerState2} ->
after_loop(Req2, State, Handler, HandlerState2, shutdown)
{stop, Req2, HandlerState2} ->
after_loop(Req2, State, Handler, HandlerState2, stop)
catch Class:Reason ->
Stacktrace = erlang:get_stacktrace(),
if RespSent -> ok; true ->

View file

@ -33,7 +33,7 @@
-type frag_state() :: undefined
| {nofin, opcode(), binary()} | {fin, opcode(), binary()}.
-type rsv() :: << _:3 >>.
-type terminate_reason() :: normal | shutdown | timeout
-type terminate_reason() :: normal | stop | timeout
| remote | {remote, close_code(), binary()}
| {error, badencoding | badframe | closed | atom()}
| {crash, error | exit | throw, any()}.
@ -49,14 +49,14 @@
| {ok, Req, State, hibernate}
| {reply, frame() | [frame()], Req, State}
| {reply, frame() | [frame()], Req, State, hibernate}
| {shutdown, Req, State}
| {stop, Req, State}
when Req::cowboy_req:req(), State::any().
-callback websocket_info(any(), Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {reply, frame() | [frame()], Req, State}
| {reply, frame() | [frame()], Req, State, hibernate}
| {shutdown, Req, State}
| {stop, Req, State}
when Req::cowboy_req:req(), State::any().
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
@ -581,8 +581,8 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
case websocket_send_many(Payload, State) of
{ok, State2} ->
NextState(State2, Req2, HandlerState2, RemainingData);
{shutdown, State2} ->
handler_terminate(State2, Req2, HandlerState2, shutdown);
{stop, State2} ->
handler_terminate(State2, Req2, HandlerState2, stop);
{{error, _} = Error, State2} ->
handler_terminate(State2, Req2, HandlerState2, Error)
end;
@ -592,8 +592,8 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
{ok, State2} ->
NextState(State2#state{hibernate=true},
Req2, HandlerState2, RemainingData);
{shutdown, State2} ->
handler_terminate(State2, Req2, HandlerState2, shutdown);
{stop, State2} ->
handler_terminate(State2, Req2, HandlerState2, stop);
{{error, _} = Error, State2} ->
handler_terminate(State2, Req2, HandlerState2, Error)
end;
@ -601,8 +601,8 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
case websocket_send(Payload, State) of
{ok, State2} ->
NextState(State2, Req2, HandlerState2, RemainingData);
{shutdown, State2} ->
handler_terminate(State2, Req2, HandlerState2, shutdown);
{stop, State2} ->
handler_terminate(State2, Req2, HandlerState2, stop);
{{error, _} = Error, State2} ->
handler_terminate(State2, Req2, HandlerState2, Error)
end;
@ -611,13 +611,13 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
{ok, State2} ->
NextState(State2#state{hibernate=true},
Req2, HandlerState2, RemainingData);
{shutdown, State2} ->
handler_terminate(State2, Req2, HandlerState2, shutdown);
{stop, State2} ->
handler_terminate(State2, Req2, HandlerState2, stop);
{{error, _} = Error, State2} ->
handler_terminate(State2, Req2, HandlerState2, Error)
end;
{shutdown, Req2, HandlerState2} ->
websocket_close(State, Req2, HandlerState2, shutdown)
{stop, Req2, HandlerState2} ->
websocket_close(State, Req2, HandlerState2, stop)
catch Class:Reason ->
_ = websocket_close(State, Req, HandlerState, {crash, Class, Reason}),
erlang:Class([
@ -652,12 +652,11 @@ websocket_deflate_frame(_, Payload, State=#state{deflate_state = Deflate}) ->
{Deflated1, << 1:1, 0:2 >>, State}.
-spec websocket_send(frame(), #state{})
-> {ok, #state{}} | {shutdown, #state{}} | {{error, atom()}, #state{}}.
websocket_send(Type, State=#state{socket=Socket, transport=Transport})
when Type =:= close ->
-> {ok, #state{}} | {stop, #state{}} | {{error, atom()}, #state{}}.
websocket_send(Type = close, State=#state{socket=Socket, transport=Transport}) ->
Opcode = websocket_opcode(Type),
case Transport:send(Socket, << 1:1, 0:3, Opcode:4, 0:8 >>) of
ok -> {shutdown, State};
ok -> {stop, State};
Error -> {Error, State}
end;
websocket_send(Type, State=#state{socket=Socket, transport=Transport})
@ -675,7 +674,7 @@ websocket_send({Type = close, StatusCode, Payload}, State=#state{
BinLen = payload_length_to_binary(Len),
Transport:send(Socket,
[<< 1:1, 0:3, Opcode:4, 0:1, BinLen/bits, StatusCode:16 >>, Payload]),
{shutdown, State};
{stop, State};
websocket_send({Type, Payload0}, State=#state{socket=Socket, transport=Transport}) ->
Opcode = websocket_opcode(Type),
{Payload, Rsv, State2} = websocket_deflate_frame(Opcode, iolist_to_binary(Payload0), State),
@ -700,13 +699,13 @@ payload_length_to_binary(N) ->
end.
-spec websocket_send_many([frame()], #state{})
-> {ok, #state{}} | {shutdown, #state{}} | {{error, atom()}, #state{}}.
-> {ok, #state{}} | {stop, #state{}} | {{error, atom()}, #state{}}.
websocket_send_many([], State) ->
{ok, State};
websocket_send_many([Frame|Tail], State) ->
case websocket_send(Frame, State) of
{ok, State2} -> websocket_send_many(Tail, State2);
{shutdown, State2} -> {shutdown, State2};
{stop, State2} -> {stop, State2};
{Error, State2} -> {Error, State2}
end.
@ -716,7 +715,7 @@ websocket_send_many([Frame|Tail], State) ->
websocket_close(State=#state{socket=Socket, transport=Transport},
Req, HandlerState, Reason) ->
case Reason of
Normal when Normal =:= shutdown; Normal =:= timeout ->
Normal when Normal =:= stop; Normal =:= timeout ->
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>);
{error, badframe} ->
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1002:16 >>);

View file

@ -14,12 +14,12 @@ init(Req, _) ->
{cowboy_loop, Req, 2, 5000, hibernate}.
info(timeout, Req, 0) ->
{shutdown, cowboy_req:reply(102, Req), 0};
{stop, cowboy_req:reply(102, Req), 0};
info(timeout, Req, Count) ->
erlang:send_after(200, self(), timeout),
{ok, Req, Count - 1, hibernate}.
terminate(shutdown, _, 0) ->
terminate(stop, _, 0) ->
ok;
terminate({error, overflow}, _, _) ->
ok.

View file

@ -16,7 +16,7 @@ init(Req, _) ->
info(timeout, Req, State) ->
{ok, Body, Req2} = cowboy_req:body(Req),
100000 = byte_size(Body),
{shutdown, cowboy_req:reply(200, Req2), State}.
{stop, cowboy_req:reply(200, Req2), State}.
terminate(shutdown, _, _) ->
terminate(stop, _, _) ->
ok.

View file

@ -15,7 +15,7 @@ init(Req, _) ->
{cowboy_loop, Req, undefined, 200, hibernate}.
info(timeout, Req, State) ->
{shutdown, cowboy_req:reply(500, Req), State}.
{stop, cowboy_req:reply(500, Req), State}.
terminate(timeout, _, _) ->
ok.

View file

@ -17,7 +17,7 @@ info(stream, Req, undefined) ->
stream(Req, ID, Acc) ->
case cowboy_req:body(Req) of
{ok, <<>>, Req2} ->
{shutdown, cowboy_req:reply(200, Req2), undefined};
{stop, cowboy_req:reply(200, Req2), undefined};
{_, Data, Req2} ->
parse_id(Req2, ID, << Acc/binary, Data/binary >>)
end.
@ -30,5 +30,5 @@ parse_id(Req, ID, Data) ->
stream(Req, ID, Data)
end.
terminate(shutdown, _, _) ->
terminate(stop, _, _) ->
ok.