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) ->
233233ranch_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 {}.
237252listener_of_protocol (Protocol ) ->
238253 MatchSpec = # listener {
@@ -547,7 +562,7 @@ 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+ _ = catch ranch :handshake (Ref ),
551566 exit ({shutdown , failed_to_recv_proxy_header }).
552567
553568handshake (Ref , ProxyProtocolEnabled ) ->
@@ -562,16 +577,36 @@ handshake(Ref, ProxyProtocolEnabled, BufferStrategy) ->
562577 {error , protocol_error , Error } ->
563578 failed_to_recv_proxy_header (Ref , Error );
564579 {ok , ProxyInfo } ->
565- { ok , Sock } = ranch : handshake (Ref ),
580+ Sock = try_ranch_handshake (Ref ),
566581 ok = tune_buffer_size (Sock , BufferStrategy ),
567582 {ok , {rabbit_proxy_socket , Sock , ProxyInfo }}
568583 end ;
569584 false ->
570- { ok , Sock } = ranch : handshake (Ref ),
585+ Sock = try_ranch_handshake (Ref ),
571586 ok = tune_buffer_size (Sock , BufferStrategy ),
572587 {ok , Sock }
573588 end .
574589
590+ try_ranch_handshake (Ref ) ->
591+ try ranch :handshake (Ref ) of
592+ {ok , Sock } ->
593+ Sock
594+ catch
595+ % % Don't log on Reason = closed to prevent flooding the log
596+ % % specially since a TCP health check, such as the default
597+ % % (with cluster-operator) readinessProbe periodically opens
598+ % % and closes a connection, as mentioned in
599+ % % https://github.com/rabbitmq/rabbitmq-server/pull/12304
600+ exit :{shutdown , {closed , _ } = Reason } ->
601+ exit ({shutdown , Reason });
602+ exit :{shutdown , {Reason , {PeerIp , PeerPort } = PeerInfo }} ->
603+ PeerAddress = io_lib :format (" ~ts :~tp " , [rabbit_misc :ntoab (PeerIp ), PeerPort ]),
604+ Protocol = ranch_ref_to_protocol (Ref ),
605+ rabbit_log :error (" ~p error during handshake for protocol ~p and peer ~ts " ,
606+ [Reason , Protocol , PeerAddress ]),
607+ exit ({shutdown , {Reason , PeerInfo }})
608+ end .
609+
575610tune_buffer_size (Sock , dynamic_buffer ) ->
576611 case rabbit_net :setopts (Sock , [{buffer , 128 }]) of
577612 ok -> ok ;
0 commit comments