Skip to content

Avoid an exception during client connection with some environments, fixes #7787 #7788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions deps/rabbit/src/rabbit_reader.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
-record(v1, {
%% parent process
parent,
%% Ranch ref
ranch_ref,
%% socket
sock,
%% connection state, see connection record
Expand Down Expand Up @@ -168,7 +170,7 @@ init(Parent, HelperSup, Ref) ->
{ok, Sock} = rabbit_networking:handshake(Ref,
application:get_env(rabbit, proxy_protocol, false)),
Deb = sys:debug_options([]),
start_connection(Parent, HelperSup, Deb, Sock).
start_connection(Parent, HelperSup, Ref, Deb, Sock).

-spec system_continue(_,_,{[binary()], non_neg_integer(), #v1{}}) -> any().

Expand Down Expand Up @@ -293,10 +295,10 @@ socket_op(Sock, Fun) ->
exit(normal)
end.

-spec start_connection(pid(), pid(), any(), rabbit_net:socket()) ->
-spec start_connection(pid(), pid(), ranch:ref(), any(), rabbit_net:socket()) ->
no_return().

start_connection(Parent, HelperSup, Deb, Sock) ->
start_connection(Parent, HelperSup, RanchRef, Deb, Sock) ->
process_flag(trap_exit, true),
RealSocket = rabbit_net:unwrap_socket(Sock),
Name = case rabbit_net:connection_string(Sock, inbound) of
Expand All @@ -314,6 +316,7 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
socket_op(Sock, fun (S) -> rabbit_net:socket_ends(S, inbound) end),
?store_proc_name(Name),
State = #v1{parent = Parent,
ranch_ref = RanchRef,
sock = RealSocket,
connection = #connection{
name = Name,
Expand Down Expand Up @@ -1213,16 +1216,16 @@ handle_method0(#'connection.tune_ok'{frame_max = FrameMax,
heartbeater = Heartbeater};

handle_method0(#'connection.open'{virtual_host = VHost},
State = #v1{connection_state = opening,
State = #v1{ranch_ref = RanchRef,
connection_state = opening,
connection = Connection = #connection{
log_name = ConnName,
port = Port,
user = User = #user{username = Username},
protocol = Protocol},
helper_sup = SupPid,
sock = Sock,
throttle = Throttle}) ->
ok = is_over_node_connection_limit(Port),
ok = is_over_node_connection_limit(RanchRef),
ok = is_over_vhost_connection_limit(VHost, User),
ok = is_over_user_connection_limit(User),
ok = rabbit_access_control:check_vhost_access(User, VHost, {socket, Sock}, #{}),
Expand Down Expand Up @@ -1323,18 +1326,21 @@ is_vhost_alive(VHostPath, User) ->
[VHostPath, User#user.username, VHostPath])
end.

is_over_node_connection_limit(Port) ->
{Addr, _, _} = hd(rabbit_networking:tcp_listener_addresses(Port)),
Ref = rabbit_networking:ranch_ref(Addr, Port),
#{active_connections := ActiveConns} = ranch:info(Ref),
is_over_node_connection_limit(RanchRef) ->
Limit = rabbit_misc:get_env(rabbit, connection_max, infinity),
case ActiveConns > Limit of
false -> ok;
true ->
rabbit_misc:protocol_error(not_allowed,
"connection refused: "
"node connection limit (~tp) is reached",
[Limit])
case Limit of
infinity -> ok;
N when is_integer(N) ->
#{active_connections := ActiveConns} = ranch:info(RanchRef),

case ActiveConns > Limit of
false -> ok;
true ->
rabbit_misc:protocol_error(not_allowed,
"connection refused: "
"node connection limit (~tp) is reached",
[Limit])
end
end.

is_over_vhost_connection_limit(VHostPath, User) ->
Expand Down