aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMaria Scott <[email protected]>2021-09-03 14:03:08 +0200
committerLoïc Hoguin <[email protected]>2021-09-06 10:28:57 +0200
commit3303dac8ba3415569ca42bdc2eeccc6d5ee2c845 (patch)
treee2d6add8383005aa25df6ccbbe4cf3377356dc95 /src
parent9cbc272ddbff2da9c8ac18c2c6b0f29d22500cd5 (diff)
downloadranch-3303dac8ba3415569ca42bdc2eeccc6d5ee2c845.tar.gz
ranch-3303dac8ba3415569ca42bdc2eeccc6d5ee2c845.tar.bz2
ranch-3303dac8ba3415569ca42bdc2eeccc6d5ee2c845.zip
Enable usage of experimental inet_backend option for TCP listeners
Diffstat (limited to 'src')
-rw-r--r--src/ranch_acceptors_sup.erl7
-rw-r--r--src/ranch_tcp.erl20
2 files changed, 15 insertions, 12 deletions
diff --git a/src/ranch_acceptors_sup.erl b/src/ranch_acceptors_sup.erl
index 76155b8..7e43179 100644
--- a/src/ranch_acceptors_sup.erl
+++ b/src/ranch_acceptors_sup.erl
@@ -60,12 +60,7 @@ start_listen_sockets(Ref, NumListenSockets, Transport, TransOpts0, Logger) when
[];
{_, Port} ->
SocketOpts = maps:get(socket_opts, TransOpts0, []),
- SocketOpts1 = case lists:keyfind(port, 1, SocketOpts) of
- {port, Port} ->
- SocketOpts;
- _ ->
- [{port, Port}|lists:keydelete(port, 1, SocketOpts)]
- end,
+ SocketOpts1 = lists:keystore(port, 1, SocketOpts, {port, Port}),
TransOpts1 = TransOpts0#{socket_opts => SocketOpts1},
[{N, start_listen_socket(Ref, Transport, TransOpts1, Logger)}
|| N <- lists:seq(2, NumListenSockets)]
diff --git a/src/ranch_tcp.erl b/src/ranch_tcp.erl
index 41f59c9..060f1ca 100644
--- a/src/ranch_tcp.erl
+++ b/src/ranch_tcp.erl
@@ -90,16 +90,24 @@ messages() -> {tcp, tcp_closed, tcp_error, tcp_passive}.
listen(TransOpts) ->
ok = cleanup(TransOpts),
Logger = maps:get(logger, TransOpts, logger),
- SocketOpts0 = maps:get(socket_opts, TransOpts, []),
+ SocketOpts = maps:get(socket_opts, TransOpts, []),
+ %% We set the port to 0 because it is given in the Opts directly.
+ %% The port in the options takes precedence over the one in the
+ %% first argument.
+ gen_tcp:listen(0, prepare_socket_opts(SocketOpts, Logger)).
+
+prepare_socket_opts([Backend = {inet_backend, _}|SocketOpts], Logger) ->
+ %% In OTP/23, the inet_backend option may be used to activate the
+ %% experimental socket backend for inet/gen_tcp. If present, it must
+ %% be the first option in the list.
+ [Backend|prepare_socket_opts(SocketOpts, Logger)];
+prepare_socket_opts(SocketOpts0, Logger) ->
SocketOpts1 = ranch:set_option_default(SocketOpts0, backlog, 1024),
SocketOpts2 = ranch:set_option_default(SocketOpts1, nodelay, true),
SocketOpts3 = ranch:set_option_default(SocketOpts2, send_timeout, 30000),
SocketOpts4 = ranch:set_option_default(SocketOpts3, send_timeout_close, true),
- %% We set the port to 0 because it is given in the Opts directly.
- %% The port in the options takes precedence over the one in the
- %% first argument.
- gen_tcp:listen(0, ranch:filter_options(SocketOpts4, disallowed_listen_options(),
- [binary, {active, false}, {packet, raw}, {reuseaddr, true}], Logger)).
+ ranch:filter_options(SocketOpts4, disallowed_listen_options(),
+ [binary, {active, false}, {packet, raw}, {reuseaddr, true}], Logger).
%% 'binary' and 'list' are disallowed but they are handled
%% specifically as they do not have 2-tuple equivalents.