mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Implement dynamic socket buffer sizes
Cowboy will set the socket's buffer size dynamically to better fit the current workload. When the incoming data is small, a low buffer size reduces the memory footprint and improves responsiveness and therefore performance. When the incoming data is large, such as large HTTP request bodies, a larger buffer size helps us avoid doing too many binary appends and related allocations. Setting a large buffer size for all use cases is sub-optimal because allocating more than needed necessarily results in a performance hit (not just increased memory usage). By default Cowboy starts with a buffer size of 8192 bytes. It then doubles or halves the buffer size depending on the size of the data it receives from the socket. It stops decreasing at 8192 and increasing at 131072 by default. To keep track of the size of the incoming data Cowboy maintains a moving average. It allows Cowboy to avoid changing the buffer too often but still react quickly when necessary. Cowboy will increase the buffer size when the moving average is above 90% of the current buffer size, and decrease when the moving average is below 40% of the current buffer size. The current buffer size and moving average are propagated when switching protocols. The dynamic buffer is implemented in HTTP/1, HTTP/2 and HTTP/1 Websocket. HTTP/2 Websocket has it disabled because it doesn't interact directly with the socket; in that case it is HTTP/2 that has a dynamic buffer. The dynamic buffer provides a very large performance improvement in many scenarios, at minimal cost for others. Because it largely depend on the underlying protocol the improvements are no all equal. TLS and compression also impact the results. The improvement when reading a large request body, with the requests repeated in a fast loop are: * HTTP: 6x to 20x faster * HTTPS: 2x to 6x faster * H2: 4x to 5x faster * H2C: 20x to 40x faster I am not sure why H2C's performance was so bad, especially compared to H2, when using default buffer sizes. Dynamic buffers make H2C a lot more viable with default settings. The performance impact on "hello world" type requests is minimal, it goes from -5% to +5% roughly. Websocket improvements vary again depending on the protocol, but also depending on whether compression is enabled: * HTTP echo: roughly 2x faster * HTTP send: roughly 4x faster * H2C echo: roughly 2x faster * H2C send: 3x to 4x faster In the echo test we reply back, and Gun doesn't have the dynamic buffer optimisation, so that probably explains the x2 difference. With compression however there isn't much improvement. The results are roughly within -10% to +10% of each other. Zlib compression seems to be a bottleneck, or at least to modify the performance profile to such an extent that the size of the buffer does not matter. This happens to randomly generated binary data as well so it is probably not caused by the test data.
This commit is contained in:
parent
fcab905eca
commit
49be0f57cf
12 changed files with 451 additions and 85 deletions
|
@ -28,6 +28,9 @@
|
|||
compress_buffering => boolean(),
|
||||
compress_threshold => non_neg_integer(),
|
||||
connection_type => worker | supervisor,
|
||||
dynamic_buffer => false | {pos_integer(), pos_integer()},
|
||||
dynamic_buffer_initial_average => non_neg_integer(),
|
||||
dynamic_buffer_initial_size => pos_integer(),
|
||||
env => cowboy_middleware:env(),
|
||||
http10_keepalive => boolean(),
|
||||
idle_timeout => timeout(),
|
||||
|
@ -137,6 +140,10 @@
|
|||
%% Flow requested for the current stream.
|
||||
flow = infinity :: non_neg_integer() | infinity,
|
||||
|
||||
%% Dynamic buffer moving average and current buffer size.
|
||||
dynamic_buffer_size :: pos_integer() | false,
|
||||
dynamic_buffer_moving_average :: non_neg_integer(),
|
||||
|
||||
%% Identifier for the stream currently being written.
|
||||
%% Note that out_streamid =< in_streamid.
|
||||
out_streamid = 1 :: pos_integer(),
|
||||
|
@ -181,12 +188,16 @@ init(Parent, Ref, Socket, Transport, ProxyHeader, Opts) ->
|
|||
parent=Parent, ref=Ref, socket=Socket,
|
||||
transport=Transport, proxy_header=ProxyHeader, opts=Opts,
|
||||
peer=Peer, sock=Sock, cert=Cert,
|
||||
dynamic_buffer_size=init_dynamic_buffer_size(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)).
|
||||
|
||||
-include("cowboy_dynamic_buffer.hrl").
|
||||
|
||||
setopts_active(#state{socket=Socket, transport=Transport, opts=Opts}) ->
|
||||
N = maps:get(active_n, Opts, 100),
|
||||
N = maps:get(active_n, Opts, 1),
|
||||
Transport:setopts(Socket, [{active, N}]).
|
||||
|
||||
safe_setopts_active(State) ->
|
||||
|
@ -220,11 +231,13 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, opts=Opts,
|
|||
receive
|
||||
%% Discard data coming in after the last request
|
||||
%% we want to process was received fully.
|
||||
{OK, Socket, _} when OK =:= element(1, Messages), InStreamID > LastStreamID ->
|
||||
loop(State);
|
||||
{OK, Socket, Data} when OK =:= element(1, Messages), InStreamID > LastStreamID ->
|
||||
State1 = maybe_resize_buffer(State, Data),
|
||||
loop(State1);
|
||||
%% Socket messages.
|
||||
{OK, Socket, Data} when OK =:= element(1, Messages) ->
|
||||
parse(<< Buffer/binary, Data/binary >>, State);
|
||||
State1 = maybe_resize_buffer(State, Data),
|
||||
parse(<< Buffer/binary, Data/binary >>, State1);
|
||||
{Closed, Socket} when Closed =:= element(2, Messages) ->
|
||||
terminate(State, {socket_error, closed, 'The socket has been closed.'});
|
||||
{Error, Socket, Reason} when Error =:= element(3, Messages) ->
|
||||
|
@ -885,12 +898,12 @@ is_http2_upgrade(_, _) ->
|
|||
|
||||
%% Prior knowledge upgrade, without an HTTP/1.1 request.
|
||||
http2_upgrade(State=#state{parent=Parent, ref=Ref, socket=Socket, transport=Transport,
|
||||
proxy_header=ProxyHeader, opts=Opts, peer=Peer, sock=Sock, cert=Cert}, Buffer) ->
|
||||
proxy_header=ProxyHeader, peer=Peer, sock=Sock, cert=Cert}, Buffer) ->
|
||||
case Transport:secure() of
|
||||
false ->
|
||||
_ = cancel_timeout(State),
|
||||
cowboy_http2:init(Parent, Ref, Socket, Transport,
|
||||
ProxyHeader, Opts, Peer, Sock, Cert, Buffer);
|
||||
cowboy_http2:init(Parent, Ref, Socket, Transport, ProxyHeader,
|
||||
opts_for_upgrade(State), Peer, Sock, Cert, Buffer);
|
||||
true ->
|
||||
error_terminate(400, State, {connection_error, protocol_error,
|
||||
'Clients that support HTTP/2 over TLS MUST use ALPN. (RFC7540 3.4)'})
|
||||
|
@ -898,7 +911,7 @@ http2_upgrade(State=#state{parent=Parent, ref=Ref, socket=Socket, transport=Tran
|
|||
|
||||
%% Upgrade via an HTTP/1.1 request.
|
||||
http2_upgrade(State=#state{parent=Parent, ref=Ref, socket=Socket, transport=Transport,
|
||||
proxy_header=ProxyHeader, opts=Opts, peer=Peer, sock=Sock, cert=Cert},
|
||||
proxy_header=ProxyHeader, peer=Peer, sock=Sock, cert=Cert},
|
||||
Buffer, HTTP2Settings, Req) ->
|
||||
%% @todo
|
||||
%% However if the client sent a body, we need to read the body in full
|
||||
|
@ -907,13 +920,22 @@ http2_upgrade(State=#state{parent=Parent, ref=Ref, socket=Socket, transport=Tran
|
|||
try cow_http_hd:parse_http2_settings(HTTP2Settings) of
|
||||
Settings ->
|
||||
_ = cancel_timeout(State),
|
||||
cowboy_http2:init(Parent, Ref, Socket, Transport,
|
||||
ProxyHeader, Opts, Peer, Sock, Cert, Buffer, Settings, Req)
|
||||
cowboy_http2:init(Parent, Ref, Socket, Transport, ProxyHeader,
|
||||
opts_for_upgrade(State), Peer, Sock, Cert, Buffer, Settings, Req)
|
||||
catch _:_ ->
|
||||
error_terminate(400, State, {connection_error, protocol_error,
|
||||
'The HTTP2-Settings header must contain a base64 SETTINGS payload. (RFC7540 3.2, RFC7540 3.2.1)'})
|
||||
end.
|
||||
|
||||
opts_for_upgrade(#state{opts=Opts, dynamic_buffer_size=false}) ->
|
||||
Opts;
|
||||
opts_for_upgrade(#state{opts=Opts, dynamic_buffer_size=Size,
|
||||
dynamic_buffer_moving_average=MovingAvg}) ->
|
||||
Opts#{
|
||||
dynamic_buffer_initial_average => MovingAvg,
|
||||
dynamic_buffer_initial_size => Size
|
||||
}.
|
||||
|
||||
%% Request body parsing.
|
||||
|
||||
parse_body(Buffer, State=#state{in_streamid=StreamID, in_state=
|
||||
|
@ -1210,7 +1232,7 @@ commands(State0=#state{socket=Socket, transport=Transport, streams=Streams, out_
|
|||
commands(State, StreamID, Tail);
|
||||
%% Protocol takeover.
|
||||
commands(State0=#state{ref=Ref, parent=Parent, socket=Socket, transport=Transport,
|
||||
out_state=OutState, opts=Opts, buffer=Buffer, children=Children}, StreamID,
|
||||
out_state=OutState, buffer=Buffer, children=Children}, StreamID,
|
||||
[{switch_protocol, Headers, Protocol, InitialState}|_Tail]) ->
|
||||
%% @todo If there's streams opened after this one, fail instead of 101.
|
||||
State1 = cancel_timeout(State0),
|
||||
|
@ -1234,7 +1256,8 @@ commands(State0=#state{ref=Ref, parent=Parent, socket=Socket, transport=Transpor
|
|||
%% Turn off the trap_exit process flag
|
||||
%% since this process will no longer be a supervisor.
|
||||
process_flag(trap_exit, false),
|
||||
Protocol:takeover(Parent, Ref, Socket, Transport, Opts, Buffer, InitialState);
|
||||
Protocol:takeover(Parent, Ref, Socket, Transport,
|
||||
opts_for_upgrade(State), Buffer, InitialState);
|
||||
%% Set options dynamically.
|
||||
commands(State0=#state{overriden_opts=Opts},
|
||||
StreamID, [{set_options, SetOpts}|Tail]) ->
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue