-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic support for system messages #754
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,10 @@ received first. | |
|
||
A socket error ocurred. | ||
|
||
: {shutdown, Reason} | ||
|
||
Parent process exited with reason `Reason`. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussion needed here. Is it only ever the parent process doing this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with the Note I view the current cowboy API as confusing because [1] http://www.erlang.org/documentation/doc-4.9.1/doc/design_principles/sup_princ.html#shutdown There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I will change shutdown into stop later today/tomorrow. I may also change REST's halt into stop for consistency. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only |
||
:: Callbacks | ||
|
||
: info(Info, Req, State) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
::: cowboy_sys | ||
|
||
The `cowboy_sys` behaviour defines the interface used to handle system | ||
messages in Cowboy middleware and sub protocol modules. | ||
|
||
:: Callbacks | ||
|
||
: sys_continue(Req, State) | ||
-> {ok, Req, Env} | ||
| {suspend, Module, Function, Args} | ||
| {system, From, Msg, Module, Req, State} | ||
| {halt, Req} | ||
|
||
Types: | ||
|
||
* Req = cowboy_req:req() | ||
* Env = env() | ||
* Module = module() | ||
* Function = atom() | ||
* Args = [any()] | ||
* From = {pid(), any()} | ||
* Msg = any() | ||
* State = any() | ||
|
||
Continue processsing after handling system messages. | ||
|
||
Please refer to the `cowboy_middleware` manual for possible return values. | ||
|
||
: sys_terminate(Reason, Req, State) | ||
-> no_return(). | ||
|
||
Types: | ||
|
||
* Reason = any() | ||
* Req = cowboy_req:req() | ||
* State = any() | ||
|
||
Terminate due to exit signal from parent while handling system messages. | ||
|
||
The process should exit with `Reason` after doing any cleanup or miscellaneous | ||
operations. The process should not continue as normal. Usually the reason will | ||
be `shutdown` when the parent process is terminating itself. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,12 @@ | |
%% <em>{error, overflow}</em> reason if this threshold is reached. | ||
-module(cowboy_loop). | ||
-behaviour(cowboy_sub_protocol). | ||
-behaviour(cowboy_sys). | ||
|
||
-export([upgrade/6]). | ||
-export([loop/4]). | ||
-export([sys_continue/2]). | ||
-export([sys_terminate/3]). | ||
|
||
-callback init(Req, any()) | ||
-> {ok | module(), Req, any()} | ||
|
@@ -42,6 +45,7 @@ | |
|
||
-record(state, { | ||
env :: cowboy_middleware:env(), | ||
parent :: pid(), | ||
hibernate = false :: boolean(), | ||
buffer_size = 0 :: non_neg_integer(), | ||
max_buffer = 5000 :: non_neg_integer() | infinity, | ||
|
@@ -51,14 +55,20 @@ | |
}). | ||
|
||
-spec upgrade(Req, Env, module(), any(), timeout(), run | hibernate) | ||
-> {ok, Req, Env} | {suspend, module(), atom(), [any()]} | ||
-> {ok, Req, cowboy_middleware:env()} | ||
| {suspend, module(), atom(), [any()]} | ||
| {system, {pid(), any()}, any(), ?MODULE, Req, {#state{}, module(), any()}} | ||
when Req::cowboy_req:req(), Env::cowboy_middleware:env(). | ||
upgrade(Req, Env, Handler, HandlerState, Timeout, run) -> | ||
State = #state{env=Env, max_buffer=get_max_buffer(Env), timeout=Timeout}, | ||
{_, Parent} = lists:keyfind(parent, 1, Env), | ||
State = #state{env=Env, parent=Parent, max_buffer=get_max_buffer(Env), | ||
timeout=Timeout}, | ||
State2 = timeout(State), | ||
after_call(Req, State2, Handler, HandlerState); | ||
upgrade(Req, Env, Handler, HandlerState, Timeout, hibernate) -> | ||
State = #state{env=Env, max_buffer=get_max_buffer(Env), hibernate=true, timeout=Timeout}, | ||
Parent = proplists:get_value(parent, Env, self()), | ||
State = #state{env=Env, parent=Parent, max_buffer=get_max_buffer(Env), | ||
hibernate=true, timeout=Timeout}, | ||
State2 = timeout(State), | ||
after_call(Req, State2, Handler, HandlerState). | ||
|
||
|
@@ -102,9 +112,11 @@ timeout(State=#state{timeout=Timeout, | |
State#state{timeout_ref=TRef}. | ||
|
||
-spec loop(Req, #state{}, module(), any()) | ||
-> {ok, Req, cowboy_middleware:env()} | {suspend, module(), atom(), [any()]} | ||
-> {ok, Req, cowboy_middleware:env()} | ||
| {suspend, module(), atom(), [any()]} | ||
| {system, {pid(), any()}, any(), ?MODULE, Req, {#state{}, module(), any()}} | ||
when Req::cowboy_req:req(). | ||
loop(Req, State=#state{buffer_size=NbBytes, | ||
loop(Req, State=#state{parent=Parent, buffer_size=NbBytes, | ||
max_buffer=Threshold, timeout_ref=TRef, | ||
resp_sent=RespSent}, Handler, HandlerState) -> | ||
[Socket, Transport] = cowboy_req:get([socket, transport], Req), | ||
|
@@ -131,6 +143,10 @@ loop(Req, State=#state{buffer_size=NbBytes, | |
after_loop(Req, State, Handler, HandlerState, timeout); | ||
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) -> | ||
loop(Req, State, Handler, HandlerState); | ||
{system, From, Msg} -> | ||
{system, From, Msg, ?MODULE, Req, {State, Handler, HandlerState}}; | ||
{'EXIT', Parent, Reason} -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this only for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it is but the handler might trap exits. It is important that OTP processes always exit if their parent does. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes but maybe it should be left to the handler to decide that? What is gen_server doing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the behaviour used by [1] http://www.erlang.org/documentation/doc-4.9.1/doc/design_principles/spec_proc.html#7.2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. |
||
sys_terminate(Reason, Req, {State, Handler, HandlerState}); | ||
Message -> | ||
%% We set the socket back to {active, false} mode in case | ||
%% the handler is going to call recv. We also flush any | ||
|
@@ -161,7 +177,8 @@ call(Req, State=#state{resp_sent=RespSent}, | |
cowboy_req:maybe_reply(Stacktrace, Req) | ||
end, | ||
cowboy_handler:terminate({crash, Class, Reason}, Req, HandlerState, Handler), | ||
erlang:Class([ | ||
exit([ | ||
{class, Class}, | ||
{reason, Reason}, | ||
{mfa, {Handler, info, 3}}, | ||
{stacktrace, Stacktrace}, | ||
|
@@ -203,3 +220,17 @@ flush_timeouts() -> | |
after 0 -> | ||
ok | ||
end. | ||
|
||
-spec sys_continue(Req, {#state{}, module(), any()}) | ||
-> {ok, Req, cowboy_middleware:env()} | ||
| {suspend, module(), atom(), [any()]} | ||
| {system, {pid(), any()}, any(), ?MODULE, Req, {#state{}, module(), any()}} | ||
when Req::cowboy_req:req(). | ||
sys_continue(Req, {State, Handler, HandlerState}) -> | ||
loop(Req, State, Handler, HandlerState). | ||
|
||
-spec sys_terminate(any(), Req, {#state{}, module(), any()}) -> no_return() | ||
when Req::cowboy_req:req(). | ||
sys_terminate(Reason, Req, {_, Handler, HandlerState}) -> | ||
_ = cowboy_handler:terminate(Reason, Req, HandlerState, Handler), | ||
exit(Reason). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
%% Copyright (c) 2014, James Fish <[email protected]> | ||
%% | ||
%% Permission to use, copy, modify, and/or distribute this software for any | ||
%% purpose with or without fee is hereby granted, provided that the above | ||
%% copyright notice and this permission notice appear in all copies. | ||
%% | ||
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
-module(cowboy_proc). | ||
|
||
%% API. | ||
-export([spawn_link/3]). | ||
-export([hibernate/3]). | ||
-export([continue/3]). | ||
|
||
%% Internal. | ||
-export([init/3]). | ||
|
||
%% API. | ||
|
||
-spec spawn_link(module(), atom(), [any()]) -> pid(). | ||
spawn_link(Module, Fun, Args) -> | ||
proc_lib:spawn_link(?MODULE, init, [Module, Fun, Args]). | ||
|
||
-spec hibernate(module(), atom(), [any()]) -> no_return(). | ||
hibernate(Module, Fun, Args) -> | ||
proc_lib:hibernate(?MODULE, continue, [Module, Fun, Args]). | ||
|
||
-spec continue(module(), atom(), [any()]) -> any(). | ||
continue(Module, Fun, Args) -> | ||
try | ||
apply(Module, Fun, Args) | ||
catch | ||
error:Reason -> | ||
exit({Reason, erlang:get_stacktrace()}); | ||
throw:Value -> | ||
exit({{nocatch, Value}, erlang:get_stacktrace()}) | ||
end. | ||
|
||
%% Internal. | ||
|
||
-spec init(module(), atom(), [any()]) -> any(). | ||
init(Module, Fun, Args) -> | ||
_ = put('$initial_call', {Module, Fun, length(Args)}), | ||
try | ||
apply(Module, Fun, Args) | ||
catch | ||
error:Reason -> | ||
exit({Reason, erlang:get_stacktrace()}); | ||
throw:Value -> | ||
exit({{nocatch, Value}, erlang:get_stacktrace()}) | ||
end. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe name the tuple sys too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A system message is of the form
{system, From, Msg}
so I am worried turning it in to{sys, From, Msg, ..}
will confuse people.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough.