aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/src/manual/cowboy_http.asciidoc7
-rw-r--r--doc/src/manual/cowboy_http2.asciidoc7
-rw-r--r--src/cowboy_http.erl39
-rw-r--r--src/cowboy_http2.erl41
-rw-r--r--test/http2_SUITE.erl21
-rw-r--r--test/http_SUITE.erl21
6 files changed, 103 insertions, 33 deletions
diff --git a/doc/src/manual/cowboy_http.asciidoc b/doc/src/manual/cowboy_http.asciidoc
index ccddc55..0640aa9 100644
--- a/doc/src/manual/cowboy_http.asciidoc
+++ b/doc/src/manual/cowboy_http.asciidoc
@@ -21,6 +21,7 @@ opts() :: #{
chunked => boolean(),
connection_type => worker | supervisor,
dynamic_buffer => false | {pos_integer(), pos_integer()},
+ hibernate => boolean(),
http10_keepalive => boolean(),
idle_timeout => timeout(),
inactivity_timeout => timeout(),
@@ -87,6 +88,10 @@ The dynamic buffer size functionality can be disabled by
setting this option to `false`. Cowboy will also disable
it by default when the `buffer` transport option is configured.
+hibernate (false)::
+
+Whether the connection process will hibernate automatically.
+
http10_keepalive (true)::
Whether keep-alive is enabled for HTTP/1.0 connections.
@@ -179,7 +184,7 @@ Ordered list of stream handlers that will handle all stream events.
== Changelog
* *2.13*: The `active_n` default value was changed to `1`.
-* *2.13*: The `dynamic_buffer` option was added.
+* *2.13*: The `dynamic_buffer` and `hibernate` options were added.
* *2.11*: The `reset_idle_timeout_on_send` option was added.
* *2.8*: The `active_n` option was added.
* *2.7*: The `initial_stream_flow_size` and `logger` options were added.
diff --git a/doc/src/manual/cowboy_http2.asciidoc b/doc/src/manual/cowboy_http2.asciidoc
index be48502..0dcfe2c 100644
--- a/doc/src/manual/cowboy_http2.asciidoc
+++ b/doc/src/manual/cowboy_http2.asciidoc
@@ -25,6 +25,7 @@ opts() :: #{
enable_connect_protocol => boolean(),
goaway_initial_timeout => timeout(),
goaway_complete_timeout => timeout(),
+ hibernate => boolean(),
idle_timeout => timeout(),
inactivity_timeout => timeout(),
initial_connection_window_size => 65535..16#7fffffff,
@@ -122,6 +123,10 @@ goaway_complete_timeout (3000)::
Time in ms to wait for ongoing streams to complete before closing the connection
during a graceful shutdown.
+hibernate (false)::
+
+Whether the connection process will hibernate automatically.
+
idle_timeout (60000)::
Time in ms with no data received before Cowboy closes the connection.
@@ -302,7 +307,7 @@ too many `WINDOW_UPDATE` frames.
== Changelog
* *2.13*: The `active_n` default value was changed to `1`.
-* *2.13*: The `dynamic_buffer` option was added.
+* *2.13*: The `dynamic_buffer` and `hibernate` options were added.
* *2.11*: Websocket over HTTP/2 is now considered stable.
* *2.11*: The `reset_idle_timeout_on_send` option was added.
* *2.11*: Add the option `max_cancel_stream_rate` to protect
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index 1914454..f940e47 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -17,6 +17,7 @@
-module(cowboy_http).
-export([init/6]).
+-export([loop/1]).
-export([system_continue/3]).
-export([system_terminate/4]).
@@ -32,6 +33,7 @@
dynamic_buffer_initial_average => non_neg_integer(),
dynamic_buffer_initial_size => pos_integer(),
env => cowboy_middleware:env(),
+ hibernate => boolean(),
http10_keepalive => boolean(),
idle_timeout => timeout(),
inactivity_timeout => timeout(),
@@ -192,7 +194,7 @@ init(Parent, Ref, Socket, Transport, ProxyHeader, Opts) ->
dynamic_buffer_moving_average=maps:get(dynamic_buffer_initial_average, Opts, 0),
last_streamid=maps:get(max_keepalive, Opts, 1000)},
safe_setopts_active(State),
- loop(set_timeout(State, request_timeout)).
+ before_loop(set_timeout(State, request_timeout)).
-include("cowboy_dynamic_buffer.hrl").
@@ -223,6 +225,13 @@ flush_passive(Socket, Messages) ->
ok
end.
+before_loop(State=#state{opts=#{hibernate := true}}) ->
+ proc_lib:hibernate(?MODULE, loop, [State]);
+before_loop(State) ->
+ loop(State).
+
+-spec loop(#state{}) -> ok.
+
loop(State=#state{parent=Parent, socket=Socket, transport=Transport, opts=Opts,
buffer=Buffer, timer=TimerRef, children=Children, in_streamid=InStreamID,
last_streamid=LastStreamID}) ->
@@ -233,7 +242,7 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, opts=Opts,
%% we want to process was received fully.
{OK, Socket, Data} when OK =:= element(1, Messages), InStreamID > LastStreamID ->
State1 = maybe_resize_buffer(State, Data),
- loop(State1);
+ before_loop(State1);
%% Socket messages.
{OK, Socket, Data} when OK =:= element(1, Messages) ->
State1 = maybe_resize_buffer(State, Data),
@@ -246,37 +255,37 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, opts=Opts,
%% Hardcoded for compatibility with Ranch 1.x.
Passive =:= tcp_passive; Passive =:= ssl_passive ->
safe_setopts_active(State),
- loop(State);
+ before_loop(State);
%% Timeouts.
{timeout, Ref, {shutdown, Pid}} ->
cowboy_children:shutdown_timeout(Children, Ref, Pid),
- loop(State);
+ before_loop(State);
{timeout, TimerRef, Reason} ->
timeout(State, Reason);
{timeout, _, _} ->
- loop(State);
+ before_loop(State);
%% System messages.
{'EXIT', Parent, shutdown} ->
Reason = {stop, {exit, shutdown}, 'Parent process requested shutdown.'},
- loop(initiate_closing(State, Reason));
+ before_loop(initiate_closing(State, Reason));
{'EXIT', Parent, Reason} ->
terminate(State, {stop, {exit, Reason}, 'Parent process terminated.'});
{system, From, Request} ->
sys:handle_system_msg(Request, From, Parent, ?MODULE, [], State);
%% Messages pertaining to a stream.
{{Pid, StreamID}, Msg} when Pid =:= self() ->
- loop(info(State, StreamID, Msg));
+ before_loop(info(State, StreamID, Msg));
%% Exit signal from children.
Msg = {'EXIT', Pid, _} ->
- loop(down(State, Pid, Msg));
+ before_loop(down(State, Pid, Msg));
%% Calls from supervisor module.
{'$gen_call', From, Call} ->
cowboy_children:handle_supervisor_call(Call, From, Children, ?MODULE),
- loop(State);
+ before_loop(State);
%% Unknown messages.
Msg ->
cowboy:log(warning, "Received stray message ~p.~n", [Msg], Opts),
- loop(State)
+ before_loop(State)
after InactivityTimeout ->
terminate(State, {internal_error, timeout, 'No message or data received before timeout.'})
end.
@@ -362,12 +371,12 @@ timeout(State, idle_timeout) ->
'Connection idle longer than configuration allows.'}).
parse(<<>>, State) ->
- loop(State#state{buffer= <<>>});
+ before_loop(State#state{buffer= <<>>});
%% Do not process requests that come in after the last request
%% and discard the buffer if any to save memory.
parse(_, State=#state{in_streamid=InStreamID, in_state=#ps_request_line{},
last_streamid=LastStreamID}) when InStreamID > LastStreamID ->
- loop(State#state{buffer= <<>>});
+ before_loop(State#state{buffer= <<>>});
parse(Buffer, State=#state{in_state=#ps_request_line{empty_lines=EmptyLines}}) ->
after_parse(parse_request(Buffer, State, EmptyLines));
parse(Buffer, State=#state{in_state=PS=#ps_header{headers=Headers, name=undefined}}) ->
@@ -442,7 +451,7 @@ after_parse({data, _, IsFin, _, State=#state{buffer=Buffer}}) ->
nofin -> idle_timeout
end));
after_parse({more, State}) ->
- loop(set_timeout(State, idle_timeout)).
+ before_loop(set_timeout(State, idle_timeout)).
update_flow(fin, _, State) ->
%% This function is only called after parsing, therefore we
@@ -1622,12 +1631,12 @@ terminate_linger_loop(State=#state{socket=Socket}, TimerRef, Messages) ->
-spec system_continue(_, _, #state{}) -> ok.
system_continue(_, _, State) ->
- loop(State).
+ before_loop(State).
-spec system_terminate(any(), _, _, #state{}) -> no_return().
system_terminate(Reason0, _, _, State) ->
Reason = {stop, {exit, Reason0}, 'sys:terminate/2,3 was called.'},
- loop(initiate_closing(State, Reason)).
+ before_loop(initiate_closing(State, Reason)).
-spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::{#state{}, binary()}.
system_code_change(Misc, _, _, _) ->
diff --git a/src/cowboy_http2.erl b/src/cowboy_http2.erl
index a6ffca7..207a967 100644
--- a/src/cowboy_http2.erl
+++ b/src/cowboy_http2.erl
@@ -17,6 +17,7 @@
-export([init/6]).
-export([init/10]).
-export([init/12]).
+-export([loop/2]).
-export([system_continue/3]).
-export([system_terminate/4]).
@@ -36,6 +37,7 @@
env => cowboy_middleware:env(),
goaway_initial_timeout => timeout(),
goaway_complete_timeout => timeout(),
+ hibernate => boolean(),
idle_timeout => timeout(),
inactivity_timeout => timeout(),
initial_connection_window_size => 65535..16#7fffffff,
@@ -188,7 +190,7 @@ init(Parent, Ref, Socket, Transport, ProxyHeader, Opts, Peer, Sock, Cert, Buffer
http2_status=sequence, http2_machine=HTTP2Machine}), 0),
safe_setopts_active(State),
case Buffer of
- <<>> -> loop(State, Buffer);
+ <<>> -> before_loop(State, Buffer);
_ -> parse(State, Buffer)
end.
@@ -250,7 +252,7 @@ init(Parent, Ref, Socket, Transport, ProxyHeader, Opts, Peer, Sock, Cert, Buffer
ok = maybe_socket_error(State, Transport:send(Socket, Preface)),
safe_setopts_active(State),
case Buffer of
- <<>> -> loop(State, Buffer);
+ <<>> -> before_loop(State, Buffer);
_ -> parse(State, Buffer)
end.
@@ -267,6 +269,13 @@ setopts_active(#state{socket=Socket, transport=Transport, opts=Opts}) ->
safe_setopts_active(State) ->
ok = maybe_socket_error(State, setopts_active(State)).
+before_loop(State=#state{opts=#{hibernate := true}}, Buffer) ->
+ proc_lib:hibernate(?MODULE, loop, [State, Buffer]);
+before_loop(State, Buffer) ->
+ loop(State, Buffer).
+
+-spec loop(#state{}, binary()) -> ok.
+
loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
opts=Opts, timer=TimerRef, children=Children}, Buffer) ->
Messages = Transport:messages(),
@@ -288,11 +297,11 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
%% Hardcoded for compatibility with Ranch 1.x.
Passive =:= tcp_passive; Passive =:= ssl_passive ->
safe_setopts_active(State),
- loop(State, Buffer);
+ before_loop(State, Buffer);
%% System messages.
{'EXIT', Parent, shutdown} ->
Reason = {stop, {exit, shutdown}, 'Parent process requested shutdown.'},
- loop(initiate_closing(State, Reason), Buffer);
+ before_loop(initiate_closing(State, Reason), Buffer);
{'EXIT', Parent, Reason} ->
terminate(State, {stop, {exit, Reason}, 'Parent process terminated.'});
{system, From, Request} ->
@@ -302,27 +311,27 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
tick_idle_timeout(State, Buffer);
{timeout, Ref, {shutdown, Pid}} ->
cowboy_children:shutdown_timeout(Children, Ref, Pid),
- loop(State, Buffer);
+ before_loop(State, Buffer);
{timeout, TRef, {cow_http2_machine, Name}} ->
- loop(timeout(State, Name, TRef), Buffer);
+ before_loop(timeout(State, Name, TRef), Buffer);
{timeout, TimerRef, {goaway_initial_timeout, Reason}} ->
- loop(closing(State, Reason), Buffer);
+ before_loop(closing(State, Reason), Buffer);
{timeout, TimerRef, {goaway_complete_timeout, Reason}} ->
terminate(State, {stop, stop_reason(Reason),
'Graceful shutdown timed out.'});
%% Messages pertaining to a stream.
{{Pid, StreamID}, Msg} when Pid =:= self() ->
- loop(info(State, StreamID, Msg), Buffer);
+ before_loop(info(State, StreamID, Msg), Buffer);
%% Exit signal from children.
Msg = {'EXIT', Pid, _} ->
- loop(down(State, Pid, Msg), Buffer);
+ before_loop(down(State, Pid, Msg), Buffer);
%% Calls from supervisor module.
{'$gen_call', From, Call} ->
cowboy_children:handle_supervisor_call(Call, From, Children, ?MODULE),
- loop(State, Buffer);
+ before_loop(State, Buffer);
Msg ->
cowboy:log(warning, "Received stray message ~p.", [Msg], Opts),
- loop(State, Buffer)
+ before_loop(State, Buffer)
after InactivityTimeout ->
terminate(State, {internal_error, timeout, 'No message or data received before timeout.'})
end.
@@ -331,7 +340,7 @@ tick_idle_timeout(State=#state{idle_timeout_num=?IDLE_TIMEOUT_TICKS}, _) ->
terminate(State, {stop, timeout,
'Connection idle longer than configuration allows.'});
tick_idle_timeout(State=#state{idle_timeout_num=TimeoutNum}, Buffer) ->
- loop(set_idle_timeout(State, TimeoutNum + 1), Buffer).
+ before_loop(set_idle_timeout(State, TimeoutNum + 1), Buffer).
set_idle_timeout(State=#state{http2_status=Status, timer=TimerRef}, _)
when Status =:= closing_initiated orelse Status =:= closing,
@@ -372,7 +381,7 @@ parse(State=#state{http2_status=sequence}, Data) ->
{ok, Rest} ->
parse(State#state{http2_status=settings}, Rest);
more ->
- loop(State, Data);
+ before_loop(State, Data);
Error = {connection_error, _, _} ->
terminate(State, Error)
end;
@@ -391,7 +400,7 @@ parse(State=#state{http2_status=Status, http2_machine=HTTP2Machine, streams=Stre
more when Status =:= closing, Streams =:= #{} ->
terminate(State, {stop, normal, 'The connection is going away.'});
more ->
- loop(State, Data)
+ before_loop(State, Data)
end.
%% Frame rate flood protection.
@@ -1379,12 +1388,12 @@ terminate_stream_handler(#state{opts=Opts}, StreamID, Reason, StreamState) ->
-spec system_continue(_, _, {#state{}, binary()}) -> ok.
system_continue(_, _, {State, Buffer}) ->
- loop(State, Buffer).
+ before_loop(State, Buffer).
-spec system_terminate(any(), _, _, {#state{}, binary()}) -> no_return().
system_terminate(Reason0, _, _, {State, Buffer}) ->
Reason = {stop, {exit, Reason0}, 'sys:terminate/2,3 was called.'},
- loop(initiate_closing(State, Reason), Buffer).
+ before_loop(initiate_closing(State, Reason), Buffer).
-spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::{#state{}, binary()}.
system_code_change(Misc, _, _, _) ->
diff --git a/test/http2_SUITE.erl b/test/http2_SUITE.erl
index d3dc51a..f8c5a09 100644
--- a/test/http2_SUITE.erl
+++ b/test/http2_SUITE.erl
@@ -51,6 +51,27 @@ do_handshake(Settings, Config) ->
{ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
{ok, Socket}.
+hibernate(Config) ->
+ doc("Ensure that we can enable hibernation for HTTP/1.1 connections."),
+ {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
+ env => #{dispatch => init_dispatch(Config)},
+ hibernate => true
+ }),
+ Port = ranch:get_port(?FUNCTION_NAME),
+ try
+ ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
+ {ok, http2} = gun:await_up(ConnPid),
+ StreamRef1 = gun:get(ConnPid, "/"),
+ StreamRef2 = gun:get(ConnPid, "/"),
+ StreamRef3 = gun:get(ConnPid, "/"),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef1),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef2),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef3),
+ gun:close(ConnPid)
+ after
+ cowboy:stop_listener(?FUNCTION_NAME)
+ end.
+
idle_timeout(Config) ->
doc("Terminate when the idle timeout is reached."),
ProtoOpts = #{
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 4b8fc8c..f8ba530 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -199,6 +199,27 @@ do_chunked_body(ChunkSize0, Data, Acc) ->
do_chunked_body(ChunkSize, Rest,
[iolist_to_binary(cow_http_te:chunk(Chunk))|Acc]).
+hibernate(Config) ->
+ doc("Ensure that we can enable hibernation for HTTP/1.1 connections."),
+ {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
+ env => #{dispatch => init_dispatch(Config)},
+ hibernate => true
+ }),
+ Port = ranch:get_port(?FUNCTION_NAME),
+ try
+ ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
+ {ok, http} = gun:await_up(ConnPid),
+ StreamRef1 = gun:get(ConnPid, "/"),
+ StreamRef2 = gun:get(ConnPid, "/"),
+ StreamRef3 = gun:get(ConnPid, "/"),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef1),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef2),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef3),
+ gun:close(ConnPid)
+ after
+ cowboy:stop_listener(?FUNCTION_NAME)
+ end.
+
http10_keepalive_false(Config) ->
doc("Confirm the option http10_keepalive => false disables keep-alive "
"completely for HTTP/1.0 connections."),