aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http_protocol.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-10-10 17:27:52 +0200
committerLoïc Hoguin <[email protected]>2011-10-10 17:27:52 +0200
commit5e006be01fb9af2cbb6b62bec695a2c160733cf4 (patch)
tree3e8cac6cd23f0db2024aad3d4d6758d2ac6860e7 /src/cowboy_http_protocol.erl
parent25ae2028d6a9ce516b01f0ec126abeab00eb329d (diff)
downloadcowboy-5e006be01fb9af2cbb6b62bec695a2c160733cf4.tar.gz
cowboy-5e006be01fb9af2cbb6b62bec695a2c160733cf4.tar.bz2
cowboy-5e006be01fb9af2cbb6b62bec695a2c160733cf4.zip
Add support for loops in standard HTTP handlers
Now init/3 can return one of the following values to enable loops: - {loop, Req, State} - {loop, Req, State, hibernate} - {loop, Req, State, Timeout} - {loop, Req, State, Timeout, hibernate} Returning one of these tuples will activate looping in the HTTP handler. When looping, handle/2 is never called. Instead, Cowboy will listen for Erlang messages and forward them to the info/3 function of the handler. If a timeout is defined, Cowboy will also close the connection when no message has been received for Timeout milliseconds. The info/3 function is defined as info(Msg, Req, State). It can return either of the following tuples: - {ok, Req, State} - {loop, Req, State} - {loop, Req, State, hibernate} The first one ends the connection, calling terminate/2 before closing. The others continue the loop. Loops are useful when writing long-polling handlers that need to wait and don't expect to receive anything. Therefore it is recommended to set a timeout to close the connection if nothing arrives after a while and to enable hibernate everywhere. Normal HTTP handlers shouldn't need to use this and as such info/3 was made optional.
Diffstat (limited to 'src/cowboy_http_protocol.erl')
-rw-r--r--src/cowboy_http_protocol.erl83
1 files changed, 75 insertions, 8 deletions
diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl
index 50860d7..290007b 100644
--- a/src/cowboy_http_protocol.erl
+++ b/src/cowboy_http_protocol.erl
@@ -33,7 +33,7 @@
-behaviour(cowboy_protocol).
-export([start_link/4]). %% API.
--export([init/4, parse_request/1]). %% FSM.
+-export([init/4, parse_request/1, handler_loop/3]). %% FSM.
-include("include/http.hrl").
-include_lib("eunit/include/eunit.hrl").
@@ -47,7 +47,10 @@
req_empty_lines = 0 :: integer(),
max_empty_lines :: integer(),
timeout :: timeout(),
- buffer = <<>> :: binary()
+ buffer = <<>> :: binary(),
+ hibernate = false :: boolean(),
+ loop_timeout = infinity :: timeout(),
+ loop_timeout_ref :: undefined | reference()
}).
%% API.
@@ -204,7 +207,18 @@ handler_init(Req, State=#state{listener=ListenerPid,
transport=Transport, handler={Handler, Opts}}) ->
try Handler:init({Transport:name(), http}, Req, Opts) of
{ok, Req2, HandlerState} ->
- handler_loop(HandlerState, Req2, State);
+ handler_handle(HandlerState, Req2, State);
+ {loop, Req, HandlerState} ->
+ handler_before_loop(HandlerState, Req, State);
+ {loop, Req, HandlerState, hibernate} ->
+ handler_before_loop(HandlerState, Req,
+ State#state{hibernate=true});
+ {loop, Req, HandlerState, Timeout} ->
+ handler_before_loop(HandlerState, Req,
+ State#state{loop_timeout=Timeout});
+ {loop, Req, HandlerState, Timeout, hibernate} ->
+ handler_before_loop(HandlerState, Req,
+ State#state{hibernate=true, loop_timeout=Timeout});
{shutdown, Req2, HandlerState} ->
handler_terminate(HandlerState, Req2, State);
%% @todo {upgrade, transport, Module}
@@ -220,8 +234,8 @@ handler_init(Req, State=#state{listener=ListenerPid,
[Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()])
end.
--spec handler_loop(any(), #http_req{}, #state{}) -> ok.
-handler_loop(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
+-spec handler_handle(any(), #http_req{}, #state{}) -> ok.
+handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
try Handler:handle(Req, HandlerState) of
{ok, Req2, HandlerState2} ->
next_request(HandlerState2, Req2, State)
@@ -237,7 +251,61 @@ handler_loop(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
terminate(State)
end.
--spec handler_terminate(any(), #http_req{}, #state{}) -> ok | error.
+%% We don't listen for Transport closes because that would force us
+%% to receive data and buffer it indefinitely.
+-spec handler_before_loop(any(), #http_req{}, #state{}) -> ok.
+handler_before_loop(HandlerState, Req, State=#state{hibernate=true}) ->
+ State2 = handler_loop_timeout(State),
+ erlang:hibernate(?MODULE, handler_loop, [HandlerState, Req, State2]);
+handler_before_loop(HandlerState, Req, State) ->
+ State2 = handler_loop_timeout(State),
+ handler_loop(HandlerState, Req, State2).
+
+%% Almost the same code can be found in cowboy_http_websocket.
+-spec handler_loop_timeout(#state{}) -> #state{}.
+handler_loop_timeout(State=#state{loop_timeout=infinity}) ->
+ State#state{loop_timeout_ref=undefined};
+handler_loop_timeout(State=#state{loop_timeout=Timeout,
+ loop_timeout_ref=PrevRef}) ->
+ _ = case PrevRef of undefined -> ignore; PrevRef ->
+ erlang:cancel_timer(PrevRef) end,
+ TRef = make_ref(),
+ erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}),
+ State#state{loop_timeout_ref=TRef}.
+
+-spec handler_loop(any(), #http_req{}, #state{}) -> ok.
+handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) ->
+ receive
+ {?MODULE, timeout, TRef} ->
+ next_request(HandlerState, Req, State);
+ {?MODULE, timeout, OlderTRef} when is_reference(OlderTRef) ->
+ handler_loop(HandlerState, Req, State);
+ Message ->
+ handler_call(HandlerState, Req, State, Message)
+ end.
+
+-spec handler_call(any(), #http_req{}, #state{}, any()) -> ok.
+handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}},
+ Message) ->
+ try Handler:info(Message, Req, HandlerState) of
+ {ok, Req2, HandlerState2} ->
+ next_request(HandlerState2, Req2, State);
+ {loop, Req2, HandlerState2} ->
+ handler_before_loop(HandlerState2, Req2, State);
+ {loop, Req2, HandlerState2, hibernate} ->
+ handler_before_loop(HandlerState2, Req2,
+ State#state{hibernate=true})
+ catch Class:Reason ->
+ error_logger:error_msg(
+ "** Handler ~p terminating in info/3~n"
+ " for the reason ~p:~p~n"
+ "** Options were ~p~n** Handler state was ~p~n"
+ "** Request was ~p~n** Stacktrace: ~p~n~n",
+ [Handler, Class, Reason, Opts,
+ HandlerState, Req, erlang:get_stacktrace()])
+ end.
+
+-spec handler_terminate(any(), #http_req{}, #state{}) -> ok.
handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
try
Handler:terminate(Req#http_req{resp_state=locked}, HandlerState)
@@ -248,8 +316,7 @@ handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
"** Options were ~p~n** Handler state was ~p~n"
"** Request was ~p~n** Stacktrace: ~p~n~n",
[Handler, Class, Reason, Opts,
- HandlerState, Req, erlang:get_stacktrace()]),
- error
+ HandlerState, Req, erlang:get_stacktrace()])
end.
-spec next_request(any(), #http_req{}, #state{}) -> ok.