Skip to content

Commit da5d85d

Browse files
Merge pull request #7962 from rabbitmq/mergify/bp/v3.12.x/pr-7927
Make tcp send OTP 26 compatible (backport #7927)
2 parents 0d9c4df + dcc6190 commit da5d85d

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

deps/rabbit_common/src/rabbit_net.erl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,32 @@ port_command(Sock, Data) when ?IS_SSL(Sock) ->
168168
{error, Reason} -> erlang:error(Reason)
169169
end;
170170
port_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

173198
getopts(Sock, Options) when ?IS_SSL(Sock) ->
174199
ssl:getopts(Sock, Options);

0 commit comments

Comments
 (0)