33
33
close_all_user_connections /2 ,
34
34
force_connection_event_refresh /1 , force_non_amqp_connection_event_refresh /1 ,
35
35
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 ,
37
37
listener_of_protocol /1 , stop_ranch_listener_of_protocol /1 ,
38
38
list_local_connections_of_protocol /1 ]).
39
39
@@ -233,6 +233,21 @@ ranch_ref(IPAddress, Port) ->
233
233
ranch_ref_of_protocol (Protocol ) ->
234
234
ranch_ref (listener_of_protocol (Protocol )).
235
235
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
+
236
251
-spec listener_of_protocol (atom ()) -> # listener {}.
237
252
listener_of_protocol (Protocol ) ->
238
253
MatchSpec = # listener {
@@ -547,7 +562,9 @@ failed_to_recv_proxy_header(Ref, Error) ->
547
562
end ,
548
563
rabbit_log :debug (Msg , [Error ]),
549
564
% The following call will clean up resources then exit
550
- _ = ranch :handshake (Ref ),
565
+ _ = try ranch :handshake (Ref ) catch
566
+ _ :_ -> ok
567
+ end ,
551
568
exit ({shutdown , failed_to_recv_proxy_header }).
552
569
553
570
handshake (Ref , ProxyProtocolEnabled ) ->
@@ -562,16 +579,33 @@ handshake(Ref, ProxyProtocolEnabled, BufferStrategy) ->
562
579
{error , protocol_error , Error } ->
563
580
failed_to_recv_proxy_header (Ref , Error );
564
581
{ok , ProxyInfo } ->
565
- {ok , Sock } = ranch : handshake (Ref ),
582
+ {ok , Sock } = ranch_handshake (Ref ),
566
583
ok = tune_buffer_size (Sock , BufferStrategy ),
567
584
{ok , {rabbit_proxy_socket , Sock , ProxyInfo }}
568
585
end ;
569
586
false ->
570
- {ok , Sock } = ranch : handshake (Ref ),
587
+ {ok , Sock } = ranch_handshake (Ref ),
571
588
ok = tune_buffer_size (Sock , BufferStrategy ),
572
589
{ok , Sock }
573
590
end .
574
591
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
+
575
609
tune_buffer_size (Sock , dynamic_buffer ) ->
576
610
case rabbit_net :setopts (Sock , [{buffer , 128 }]) of
577
611
ok -> ok ;
0 commit comments