Skip to content

Commit a594f36

Browse files
committed
WIP: Use the built-in Ranch PROXY protocol support
Note that this makes Ranch depend on master. This will have to be changed before the PR can be merged.
1 parent 2a61385 commit a594f36

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

rabbitmq-components.mk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ dep_cowboy = hex 2.4.0
115115
dep_cowlib = hex 2.3.0
116116
dep_jsx = hex 2.9.0
117117
dep_lager = hex 3.6.5
118-
dep_ranch = hex 1.6.2
119-
dep_ranch_proxy_protocol = hex 2.1.1
118+
dep_ranch = git https://github.com/ninenines/ranch master
120119
dep_recon = hex 2.3.6
121120

122121
dep_sockjs = git https://github.com/rabbitmq/sockjs-erlang.git 405990ea62353d98d36dbf5e1e64942d9b0a1daf

src/rabbit_networking.erl

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
connection_info_all/0, connection_info_all/1,
3737
emit_connection_info_all/4, emit_connection_info_local/3,
3838
close_connection/2, force_connection_event_refresh/1, accept_ack/2,
39-
tcp_host/1]).
39+
handshake/2, tcp_host/1]).
4040

4141
%% Used by TCP-based transports, e.g. STOMP adapter
4242
-export([tcp_listener_addresses/1, tcp_listener_spec/9,
@@ -122,7 +122,6 @@ boot() ->
122122
%% Failures will throw exceptions
123123
_ = boot_listeners(fun boot_tcp/1, application:get_env(rabbit, num_tcp_acceptors, 10), "TCP"),
124124
_ = boot_listeners(fun boot_tls/1, application:get_env(rabbit, num_ssl_acceptors, 10), "TLS"),
125-
_ = maybe_start_proxy_protocol(),
126125
ok.
127126

128127
boot_listeners(Fun, NumAcceptors, Type) ->
@@ -191,12 +190,6 @@ log_poodle_fail(Context) ->
191190
"'rabbit' section of your configuration file.~n",
192191
[rabbit_misc:otp_release(), Context]).
193192

194-
maybe_start_proxy_protocol() ->
195-
case application:get_env(rabbit, proxy_protocol, false) of
196-
false -> ok;
197-
true -> application:start(ranch_proxy_protocol)
198-
end.
199-
200193
fix_ssl_options(Config) ->
201194
rabbit_ssl_options:fix(Config).
202195

@@ -264,12 +257,9 @@ start_listener0(Address, NumAcceptors, Protocol, Label, Opts) ->
264257
end.
265258

266259
transport(Protocol) ->
267-
ProxyProtocol = application:get_env(rabbit, proxy_protocol, false),
268-
case {Protocol, ProxyProtocol} of
269-
{amqp, false} -> ranch_tcp;
270-
{amqp, true} -> ranch_proxy;
271-
{'amqp/ssl', false} -> ranch_ssl;
272-
{'amqp/ssl', true} -> ranch_proxy_ssl
260+
case Protocol of
261+
amqp -> ranch_tcp;
262+
'amqp/ssl' -> ranch_ssl
273263
end.
274264

275265

@@ -373,16 +363,31 @@ force_connection_event_refresh(Ref) ->
373363
[rabbit_reader:force_event_refresh(C, Ref) || C <- connections()],
374364
ok.
375365

366+
handshake(Ref, ProxyProtocol) ->
367+
case ProxyProtocol of
368+
true ->
369+
{ok, ProxyInfo} = ranch:recv_proxy_header(Ref, 1000),
370+
{ok, Sock} = ranch:handshake(Ref),
371+
tune_buffer_size(Sock),
372+
ok = file_handle_cache:obtain(),
373+
{ok, {rabbit_proxy_socket, Sock, ProxyInfo}};
374+
false ->
375+
ranch:handshake(Ref)
376+
end.
377+
376378
accept_ack(Ref, Sock) ->
377379
ok = ranch:accept_ack(Ref),
378-
case tune_buffer_size(Sock) of
380+
tune_buffer_size(Sock),
381+
ok = file_handle_cache:obtain().
382+
383+
tune_buffer_size(Sock) ->
384+
case tune_buffer_size1(Sock) of
379385
ok -> ok;
380386
{error, _} -> rabbit_net:fast_close(Sock),
381387
exit(normal)
382-
end,
383-
ok = file_handle_cache:obtain().
388+
end.
384389

385-
tune_buffer_size(Sock) ->
390+
tune_buffer_size1(Sock) ->
386391
case rabbit_net:getopts(Sock, [sndbuf, recbuf, buffer]) of
387392
{ok, BufSizes} -> BufSz = lists:max([Sz || {_Opt, Sz} <- BufSizes]),
388393
rabbit_net:setopts(Sock, [{buffer, BufSz}]);

src/rabbit_reader.erl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
-export([system_continue/3, system_terminate/4, system_code_change/4]).
6464

65-
-export([init/4, mainloop/4, recvloop/4]).
65+
-export([init/3, mainloop/4, recvloop/4]).
6666

6767
-export([conserve_resources/3, server_properties/1]).
6868

@@ -171,7 +171,7 @@
171171
rabbit_framing:amqp_table().
172172

173173
%% These specs only exists to add no_return() to keep dialyzer happy
174-
-spec init(pid(), pid(), any(), rabbit_net:socket()) -> no_return().
174+
-spec init(pid(), pid(), any()) -> no_return().
175175
-spec start_connection(pid(), pid(), any(), rabbit_net:socket()) ->
176176
no_return().
177177

@@ -182,18 +182,18 @@
182182

183183
%%--------------------------------------------------------------------------
184184

185-
start_link(HelperSup, Ref, Sock) ->
186-
Pid = proc_lib:spawn_link(?MODULE, init, [self(), HelperSup, Ref, Sock]),
185+
start_link(HelperSup, Ref, _Sock) ->
186+
Pid = proc_lib:spawn_link(?MODULE, init, [self(), HelperSup, Ref]),
187187

188188
{ok, Pid}.
189189

190190
shutdown(Pid, Explanation) ->
191191
gen_server:call(Pid, {shutdown, Explanation}, infinity).
192192

193-
init(Parent, HelperSup, Ref, Sock) ->
193+
init(Parent, HelperSup, Ref) ->
194194
?LG_PROCESS_TYPE(reader),
195-
RealSocket = rabbit_net:unwrap_socket(Sock),
196-
rabbit_networking:accept_ack(Ref, RealSocket),
195+
{ok, Sock} = rabbit_networking:handshake(Ref,
196+
application:get_env(rabbit, proxy_protocol, false)),
197197
Deb = sys:debug_options([]),
198198
start_connection(Parent, HelperSup, Deb, Sock).
199199

0 commit comments

Comments
 (0)