@@ -168,7 +168,32 @@ port_command(Sock, Data) when ?IS_SSL(Sock) ->
168168 {error , Reason } -> erlang :error (Reason )
169169 end ;
170170port_command (Sock , Data ) when is_port (Sock ) ->
171- erlang :port_command (Sock , Data ).
171+ Fun = case persistent_term :get (rabbit_net_tcp_send , undefined ) of
172+ undefined ->
173+ Rel = list_to_integer (erlang :system_info (otp_release )),
174+ % % gen_tcp:send/2 does a selective receive of
175+ % % {inet_reply, Sock, Status[, CallerTag]}
176+ F = if Rel >= 26 ->
177+ % % Selective receive is optimised:
178+ % % https://github.com/erlang/otp/issues/6455
179+ fun gen_tcp_send /2 ;
180+ Rel < 26 ->
181+ % % Avoid costly selective receive.
182+ fun erlang :port_command /2
183+ end ,
184+ ok = persistent_term :put (rabbit_net_tcp_send , F ),
185+ F ;
186+ F ->
187+ F
188+ end ,
189+ Fun (Sock , Data ).
190+
191+ gen_tcp_send (Sock , Data ) ->
192+ case gen_tcp :send (Sock , Data ) of
193+ ok -> self () ! {inet_reply , Sock , ok },
194+ true ;
195+ {error , Reason } -> erlang :error (Reason )
196+ end .
172197
173198getopts (Sock , Options ) when ? IS_SSL (Sock ) ->
174199 ssl :getopts (Sock , Options );
0 commit comments