Skip to content

Commit

Permalink
Keep Ranch ref in rabbit_reader v1 record
Browse files Browse the repository at this point in the history
This is used to check if we're over connection limit and avoids
risky attempts to retrieve the ref from socket host/port.
  • Loading branch information
lhoguin authored and michaelklishin committed Mar 31, 2023
1 parent e56e26c commit 48467d6
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 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,17 +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,
host = Addr,
port = Port,
user = User = #user{username = Username},
protocol = Protocol},
helper_sup = SupPid,
sock = Sock,
throttle = Throttle}) ->
ok = is_over_node_connection_limit(Addr, 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 @@ -1324,13 +1326,12 @@ is_vhost_alive(VHostPath, User) ->
[VHostPath, User#user.username, VHostPath])
end.

is_over_node_connection_limit(Addr, Port) ->
is_over_node_connection_limit(RanchRef) ->
Limit = rabbit_misc:get_env(rabbit, connection_max, infinity),
case Limit of
infinity -> ok;
N when is_integer(N) ->
Ref = rabbit_networking:ranch_ref(Addr, Port),
#{active_connections := ActiveConns} = ranch:info(Ref),
#{active_connections := ActiveConns} = ranch:info(RanchRef),

case ActiveConns > Limit of
false -> ok;
Expand Down

0 comments on commit 48467d6

Please sign in to comment.