Skip to content

Commit 795e66c

Browse files
Merge pull request #13979 from cloudamqp/tls-handshake-logging
Log TCP acceptor (Ranch) timeout and TLS [handshake-related] errors
2 parents 8806e56 + 2cdf6f8 commit 795e66c

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

deps/rabbit/src/rabbit_networking.erl

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
close_all_user_connections/2,
3434
force_connection_event_refresh/1, force_non_amqp_connection_event_refresh/1,
3535
handshake/2, handshake/3, tcp_host/1,
36-
ranch_ref/1, ranch_ref/2, ranch_ref_of_protocol/1,
36+
ranch_ref/1, ranch_ref/2, ranch_ref_of_protocol/1, ranch_ref_to_protocol/1,
3737
listener_of_protocol/1, stop_ranch_listener_of_protocol/1,
3838
list_local_connections_of_protocol/1]).
3939

@@ -233,6 +233,21 @@ ranch_ref(IPAddress, Port) ->
233233
ranch_ref_of_protocol(Protocol) ->
234234
ranch_ref(listener_of_protocol(Protocol)).
235235

236+
-spec ranch_ref_to_protocol(ranch:ref()) -> atom() | undefined.
237+
ranch_ref_to_protocol({acceptor, IPAddress, Port}) ->
238+
MatchSpec = #listener{
239+
node = node(),
240+
ip_address = IPAddress,
241+
port = Port,
242+
_ = '_'
243+
},
244+
case ets:match_object(?ETS_TABLE, MatchSpec) of
245+
[] -> undefined;
246+
[Row] -> Row#listener.protocol
247+
end;
248+
ranch_ref_to_protocol(_) ->
249+
undefined.
250+
236251
-spec listener_of_protocol(atom()) -> #listener{}.
237252
listener_of_protocol(Protocol) ->
238253
MatchSpec = #listener{
@@ -547,7 +562,9 @@ failed_to_recv_proxy_header(Ref, Error) ->
547562
end,
548563
rabbit_log:debug(Msg, [Error]),
549564
% The following call will clean up resources then exit
550-
_ = ranch:handshake(Ref),
565+
_ = try ranch:handshake(Ref) catch
566+
_:_ -> ok
567+
end,
551568
exit({shutdown, failed_to_recv_proxy_header}).
552569

553570
handshake(Ref, ProxyProtocolEnabled) ->
@@ -562,16 +579,33 @@ handshake(Ref, ProxyProtocolEnabled, BufferStrategy) ->
562579
{error, protocol_error, Error} ->
563580
failed_to_recv_proxy_header(Ref, Error);
564581
{ok, ProxyInfo} ->
565-
{ok, Sock} = ranch:handshake(Ref),
582+
{ok, Sock} = ranch_handshake(Ref),
566583
ok = tune_buffer_size(Sock, BufferStrategy),
567584
{ok, {rabbit_proxy_socket, Sock, ProxyInfo}}
568585
end;
569586
false ->
570-
{ok, Sock} = ranch:handshake(Ref),
587+
{ok, Sock} = ranch_handshake(Ref),
571588
ok = tune_buffer_size(Sock, BufferStrategy),
572589
{ok, Sock}
573590
end.
574591

592+
ranch_handshake(Ref) ->
593+
try ranch:handshake(Ref) catch
594+
%% Don't log on Reason = closed to prevent flooding the log
595+
%% specially since a TCP health check, such as the default
596+
%% (with cluster-operator) readinessProbe periodically opens
597+
%% and closes a connection, as mentioned in
598+
%% https://github.com/rabbitmq/rabbitmq-server/pull/12304
599+
exit:{shutdown, {closed, _}} = Error:Stacktrace ->
600+
erlang:raise(exit, Error, Stacktrace);
601+
exit:{shutdown, {Reason, {PeerIp, PeerPort}}} = Error:Stacktrace ->
602+
PeerAddress = io_lib:format("~ts:~tp", [rabbit_misc:ntoab(PeerIp), PeerPort]),
603+
Protocol = ranch_ref_to_protocol(Ref),
604+
rabbit_log:error("~p error during handshake for protocol ~p and peer ~ts",
605+
[Reason, Protocol, PeerAddress]),
606+
erlang:raise(exit, Error, Stacktrace)
607+
end.
608+
575609
tune_buffer_size(Sock, dynamic_buffer) ->
576610
case rabbit_net:setopts(Sock, [{buffer, 128}]) of
577611
ok -> ok;

0 commit comments

Comments
 (0)