| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
%% These functions are common to cowboy_http, cowboy_http2 and |
| 16 |
%% cowboy_websocket. It requires the options and the state |
| 17 |
%% to use the same field names. |
| 18 |
|
| 19 |
%% Experiments have shown that the size of the 'buffer' can greatly |
| 20 |
%% impact performance: a buffer too small leads to more messages |
| 21 |
%% being handled and typically more binary appends; and a buffer |
| 22 |
%% too large results in inefficient use of memory which in turn |
| 23 |
%% reduces the throughput, presumably because large binary appends |
| 24 |
%% are not as efficient as smaller ones, and because while the |
| 25 |
%% buffer gets allocated only when there is data, the allocated |
| 26 |
%% size remains until the binary is GC and so under-use hurts. |
| 27 |
%% |
| 28 |
%% The performance of a given 'buffer' size will also depend on |
| 29 |
%% how the client is sending data, and on the protocol. For example, |
| 30 |
%% HTTP/1.1 doesn't need a very large 'buffer' size for reading |
| 31 |
%% request headers, but it does need one for reading large request |
| 32 |
%% bodies. At the same time, HTTP/2 performs best reading large |
| 33 |
%% request bodies when the 'buffer' size is about half that of |
| 34 |
%% HTTP/1.1. |
| 35 |
%% |
| 36 |
%% It therefore becomes important to resize the buffer dynamically |
| 37 |
%% depending on what is currently going on. We do this based on |
| 38 |
%% the size of data packets we received from the transport. We |
| 39 |
%% maintain a moving average and when that moving average is |
| 40 |
%% 90% of the current 'buffer' size, we double the 'buffer' size. |
| 41 |
%% When things slow down and the moving average falls below |
| 42 |
%% 40% of the current 'buffer' size, we halve the 'buffer' size. |
| 43 |
%% |
| 44 |
%% To calculate the moving average we do (MovAvg + DataLen) div 2. |
| 45 |
%% This means that the moving average will change very quickly when |
| 46 |
%% DataLen increases or decreases rapidly. That's OK, we want to |
| 47 |
%% be reactive, but also setting the buffer size is a pretty fast |
| 48 |
%% operation. The formula could be changed to the following if it |
| 49 |
%% became a problem: (MovAvg * N + DataLen) div (N + 1). |
| 50 |
%% |
| 51 |
%% Note that this works best when active,N uses low values of N. |
| 52 |
%% We don't want to accumulate too much data because we resize |
| 53 |
%% the buffer. |
| 54 |
|
| 55 |
init_dynamic_buffer_size(#{dynamic_buffer_initial_size := DynamicBuffer}) -> |
| 56 |
DynamicBuffer; |
| 57 |
init_dynamic_buffer_size(#{dynamic_buffer := {LowDynamicBuffer, _}}) -> |
| 58 |
LowDynamicBuffer; |
| 59 |
init_dynamic_buffer_size(_) -> |
| 60 |
false. |
| 61 |
|
| 62 |
maybe_resize_buffer(State=#state{dynamic_buffer_size=false}, _) -> |
| 63 |
State; |
| 64 |
maybe_resize_buffer(State=#state{transport=Transport, socket=Socket, |
| 65 |
opts=#{dynamic_buffer := {LowDynamicBuffer, HighDynamicBuffer}}, |
| 66 |
dynamic_buffer_size=BufferSize0, dynamic_buffer_moving_average=MovingAvg0}, Data) -> |
| 67 |
DataLen = byte_size(Data), |
| 68 |
MovingAvg = (MovingAvg0 * 7 + DataLen) / 8, |
| 69 |
if |
| 70 |
BufferSize0 < HighDynamicBuffer andalso MovingAvg > BufferSize0 * 0.9 -> |
| 71 |
BufferSize = min(BufferSize0 * 2, HighDynamicBuffer), |
| 72 |
ok = maybe_socket_error(State, Transport:setopts(Socket, [{buffer, BufferSize}])), |
| 73 |
State#state{dynamic_buffer_moving_average=MovingAvg, dynamic_buffer_size=BufferSize}; |
| 74 |
BufferSize0 > LowDynamicBuffer andalso MovingAvg < BufferSize0 * 0.4 -> |
| 75 |
BufferSize = max(BufferSize0 div 2, LowDynamicBuffer), |
| 76 |
ok = maybe_socket_error(State, Transport:setopts(Socket, [{buffer, BufferSize}])), |
| 77 |
State#state{dynamic_buffer_moving_average=MovingAvg, dynamic_buffer_size=BufferSize}; |
| 78 |
true -> |
| 79 |
State#state{dynamic_buffer_moving_average=MovingAvg} |
| 80 |
end. |