Skip to content

Commit dbe1dea

Browse files
Merge pull request #1747 from rabbitmq/ranch_proxy_header
Use the built-in Ranch PROXY protocol support
2 parents cb0429e + 6888585 commit dbe1dea

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

rabbitmq-components.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ dep_jsx = hex 2.9.0
116116
dep_lager = hex 3.6.5
117117
dep_ra = git https://github.com/rabbitmq/ra.git master
118118
dep_ranch = hex 1.7.1
119-
dep_ranch_proxy_protocol = hex 2.1.1
120119
dep_recon = hex 2.3.6
121120

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

src/rabbit_connection_sup.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
%%--------------------------------------------------------------------------
4444

45-
start_link(Ref, Sock, _Transport, _Opts) ->
45+
start_link(Ref, _Sock, _Transport, _Opts) ->
4646
{ok, SupPid} = supervisor2:start_link(?MODULE, []),
4747
%% We need to get channels in the hierarchy here so they get shut
4848
%% down after the reader, so the reader gets a chance to terminate
@@ -62,7 +62,7 @@ start_link(Ref, Sock, _Transport, _Opts) ->
6262
{ok, ReaderPid} =
6363
supervisor2:start_child(
6464
SupPid,
65-
{reader, {rabbit_reader, start_link, [HelperSup, Ref, Sock]},
65+
{reader, {rabbit_reader, start_link, [HelperSup, Ref]},
6666
intrinsic, ?WORKER_WAIT, worker, [rabbit_reader]}),
6767
{ok, SupPid, ReaderPid}.
6868

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, 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,
@@ -121,7 +121,6 @@ boot() ->
121121
%% Failures will throw exceptions
122122
_ = boot_listeners(fun boot_tcp/1, application:get_env(rabbit, num_tcp_acceptors, 10), "TCP"),
123123
_ = boot_listeners(fun boot_tls/1, application:get_env(rabbit, num_ssl_acceptors, 10), "TLS"),
124-
_ = maybe_start_proxy_protocol(),
125124
ok.
126125

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

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

@@ -263,12 +256,9 @@ start_listener0(Address, NumAcceptors, Protocol, Label, Opts) ->
263256
end.
264257

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

274264

@@ -368,16 +358,31 @@ close_connection(Pid, Explanation) ->
368358
ok
369359
end.
370360

361+
handshake(Ref, ProxyProtocol) ->
362+
case ProxyProtocol of
363+
true ->
364+
{ok, ProxyInfo} = ranch:recv_proxy_header(Ref, 1000),
365+
{ok, Sock} = ranch:handshake(Ref),
366+
tune_buffer_size(Sock),
367+
ok = file_handle_cache:obtain(),
368+
{ok, {rabbit_proxy_socket, Sock, ProxyInfo}};
369+
false ->
370+
ranch:handshake(Ref)
371+
end.
372+
371373
accept_ack(Ref, Sock) ->
372374
ok = ranch:accept_ack(Ref),
373-
case tune_buffer_size(Sock) of
375+
tune_buffer_size(Sock),
376+
ok = file_handle_cache:obtain().
377+
378+
tune_buffer_size(Sock) ->
379+
case tune_buffer_size1(Sock) of
374380
ok -> ok;
375381
{error, _} -> rabbit_net:fast_close(Sock),
376382
exit(normal)
377-
end,
378-
ok = file_handle_cache:obtain().
383+
end.
379384

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

src/rabbit_reader.erl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@
5757
-include("rabbit_framing.hrl").
5858
-include("rabbit.hrl").
5959

60-
-export([start_link/3, info_keys/0, info/1, info/2,
60+
-export([start_link/2, info_keys/0, info/1, info/2,
6161
shutdown/2]).
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

@@ -157,7 +157,7 @@
157157

158158
%%--------------------------------------------------------------------------
159159

160-
-spec start_link(pid(), any(), rabbit_net:socket()) -> rabbit_types:ok(pid()).
160+
-spec start_link(pid(), any()) -> rabbit_types:ok(pid()).
161161
-spec info_keys() -> rabbit_types:info_keys().
162162
-spec info(pid()) -> rabbit_types:infos().
163163
-spec info(pid(), rabbit_types:info_keys()) -> rabbit_types:infos().
@@ -170,7 +170,7 @@
170170
rabbit_framing:amqp_table().
171171

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

@@ -181,18 +181,18 @@
181181

182182
%%--------------------------------------------------------------------------
183183

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

187187
{ok, Pid}.
188188

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

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

0 commit comments

Comments
 (0)