Skip to content

Commit

Permalink
Rename 'shutdown' close reason and tuples to 'stop'
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
essen committed Nov 7, 2014
1 parent 903f6f4 commit 8cbd8c1
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 47 deletions.
6 changes: 3 additions & 3 deletions doc/src/guide/loop_handlers.ezdoc
Expand Up @@ -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}.
```
Expand All @@ -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.
Expand All @@ -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};
Expand Down
6 changes: 3 additions & 3 deletions doc/src/guide/ws_handlers.ezdoc
Expand Up @@ -42,7 +42,7 @@ init(Req, _Opts) ->
<<"mychat2">>, Req),
{ok, Req2, #state{}};
false ->
{shutdown, Req, undefined}
{stop, Req, undefined}
end
end.
```
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions doc/src/manual/cowboy_loop.ezdoc
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions doc/src/manual/cowboy_websocket.ezdoc
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand All @@ -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.

Expand All @@ -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:

Expand All @@ -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.

Expand Down
6 changes: 3 additions & 3 deletions src/cowboy_loop.erl
Expand Up @@ -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.

Expand Down Expand Up @@ -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 ->
Expand Down
41 changes: 20 additions & 21 deletions src/cowboy_websocket.erl
Expand Up @@ -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()}.
Expand All @@ -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.

Expand Down Expand Up @@ -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;
Expand All @@ -592,17 +592,17 @@ 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;
{reply, Payload, Req2, HandlerState2} ->
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;
Expand All @@ -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([
Expand Down Expand Up @@ -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})
Expand All @@ -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),
Expand All @@ -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.

Expand All @@ -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 >>);
Expand Down
4 changes: 2 additions & 2 deletions test/handlers/long_polling_h.erl
Expand Up @@ -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.
4 changes: 2 additions & 2 deletions test/handlers/loop_handler_body_h.erl
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion test/handlers/loop_handler_timeout_h.erl
Expand Up @@ -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.
4 changes: 2 additions & 2 deletions test/http_SUITE_data/http_loop_stream_recv.erl
Expand Up @@ -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.
Expand All @@ -30,5 +30,5 @@ parse_id(Req, ID, Data) ->
stream(Req, ID, Data)
end.

terminate(shutdown, _, _) ->
terminate(stop, _, _) ->
ok.

0 comments on commit 8cbd8c1

Please sign in to comment.