Make it possible to tune socket buffer sizes

parent d49a451d
...@@ -63,11 +63,14 @@ ranch_init({Ref, Socket, Transport, _} = Opts) -> ...@@ -63,11 +63,14 @@ ranch_init({Ref, Socket, Transport, _} = Opts) ->
case init(Opts) of case init(Opts) of
{ok, State} -> {ok, State} ->
ok = ranch:accept_ack(Ref), ok = ranch:accept_ack(Ref),
ok = Transport:setopts(Socket, BufSize = application:get_env(?APP, upstream_socket_buffer_size,
?MAX_SOCK_BUF_SIZE),
ok = Transport:setopts(
Socket,
[{active, once}, [{active, once},
%% {recbuf, ?MAX_SOCK_BUF_SIZE}, %% {recbuf, ?MAX_SOCK_BUF_SIZE},
%% {sndbuf, ?MAX_SOCK_BUF_SIZE}, %% {sndbuf, ?MAX_SOCK_BUF_SIZE},
{buffer, ?MAX_SOCK_BUF_SIZE} {buffer, BufSize}
]), ]),
gen_server:enter_loop(?MODULE, [], State); gen_server:enter_loop(?MODULE, [], State);
error -> error ->
...@@ -361,10 +364,13 @@ handle_upstream_header(DcId, S) -> ...@@ -361,10 +364,13 @@ handle_upstream_header(DcId, S) ->
-define(SEND_TIMEOUT, 60 * 1000). -define(SEND_TIMEOUT, 60 * 1000).
connect(Host, Port) -> connect(Host, Port) ->
BufSize = application:get_env(?APP, downstream_socket_buffer_size,
?MAX_SOCK_BUF_SIZE),
SockOpts = [{active, once}, SockOpts = [{active, once},
{packet, raw}, {packet, raw},
binary, {mode, binary},
{send_timeout, ?SEND_TIMEOUT}, {send_timeout, ?SEND_TIMEOUT},
{buffer, BufSize},
%% {nodelay, true}, %% {nodelay, true},
{keepalive, true}], {keepalive, true}],
case mtp_metric:rt([?APP, downstream_connect_duration, seconds], case mtp_metric:rt([?APP, downstream_connect_duration, seconds],
...@@ -372,9 +378,6 @@ connect(Host, Port) -> ...@@ -372,9 +378,6 @@ connect(Host, Port) ->
gen_tcp:connect(Host, Port, SockOpts, ?CONN_TIMEOUT) gen_tcp:connect(Host, Port, SockOpts, ?CONN_TIMEOUT)
end) of end) of
{ok, Sock} -> {ok, Sock} ->
ok = inet:setopts(Sock, [%% {recbuf, ?MAX_SOCK_BUF_SIZE},
%% {sndbuf, ?MAX_SOCK_BUF_SIZE},
{buffer, ?MAX_SOCK_BUF_SIZE}]),
{ok, Sock}; {ok, Sock};
{error, _} = Err -> {error, _} = Err ->
Err Err
......
...@@ -60,6 +60,11 @@ ...@@ -60,6 +60,11 @@
%% module with function `notify/4' exported. %% module with function `notify/4' exported.
%% See mtp_metric:notify/4 for details %% See mtp_metric:notify/4 for details
%% {metric_backend, my_metric_backend} %% {metric_backend, my_metric_backend}
%% User-space recv socket buffer sizes. Set to higher if you have
%% enough RAM
%% {upstream_socket_buffer_size, 51200}, %50kb
%% {downstream_socket_buffer_size, 51200} %50kb
]}, ]},
{modules, []}, {modules, []},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment