Skip to content

Commit 48467d6

Browse files
Loïc Hoguinmichaelklishin
authored andcommitted
Keep Ranch ref in rabbit_reader v1 record
This is used to check if we're over connection limit and avoids risky attempts to retrieve the ref from socket host/port.
1 parent e56e26c commit 48467d6

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

deps/rabbit/src/rabbit_reader.erl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
-record(v1, {
6767
%% parent process
6868
parent,
69+
%% Ranch ref
70+
ranch_ref,
6971
%% socket
7072
sock,
7173
%% connection state, see connection record
@@ -168,7 +170,7 @@ init(Parent, HelperSup, Ref) ->
168170
{ok, Sock} = rabbit_networking:handshake(Ref,
169171
application:get_env(rabbit, proxy_protocol, false)),
170172
Deb = sys:debug_options([]),
171-
start_connection(Parent, HelperSup, Deb, Sock).
173+
start_connection(Parent, HelperSup, Ref, Deb, Sock).
172174

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

@@ -293,10 +295,10 @@ socket_op(Sock, Fun) ->
293295
exit(normal)
294296
end.
295297

296-
-spec start_connection(pid(), pid(), any(), rabbit_net:socket()) ->
298+
-spec start_connection(pid(), pid(), ranch:ref(), any(), rabbit_net:socket()) ->
297299
no_return().
298300

299-
start_connection(Parent, HelperSup, Deb, Sock) ->
301+
start_connection(Parent, HelperSup, RanchRef, Deb, Sock) ->
300302
process_flag(trap_exit, true),
301303
RealSocket = rabbit_net:unwrap_socket(Sock),
302304
Name = case rabbit_net:connection_string(Sock, inbound) of
@@ -314,6 +316,7 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
314316
socket_op(Sock, fun (S) -> rabbit_net:socket_ends(S, inbound) end),
315317
?store_proc_name(Name),
316318
State = #v1{parent = Parent,
319+
ranch_ref = RanchRef,
317320
sock = RealSocket,
318321
connection = #connection{
319322
name = Name,
@@ -1213,17 +1216,16 @@ handle_method0(#'connection.tune_ok'{frame_max = FrameMax,
12131216
heartbeater = Heartbeater};
12141217

12151218
handle_method0(#'connection.open'{virtual_host = VHost},
1216-
State = #v1{connection_state = opening,
1219+
State = #v1{ranch_ref = RanchRef,
1220+
connection_state = opening,
12171221
connection = Connection = #connection{
12181222
log_name = ConnName,
1219-
host = Addr,
1220-
port = Port,
12211223
user = User = #user{username = Username},
12221224
protocol = Protocol},
12231225
helper_sup = SupPid,
12241226
sock = Sock,
12251227
throttle = Throttle}) ->
1226-
ok = is_over_node_connection_limit(Addr, Port),
1228+
ok = is_over_node_connection_limit(RanchRef),
12271229
ok = is_over_vhost_connection_limit(VHost, User),
12281230
ok = is_over_user_connection_limit(User),
12291231
ok = rabbit_access_control:check_vhost_access(User, VHost, {socket, Sock}, #{}),
@@ -1324,13 +1326,12 @@ is_vhost_alive(VHostPath, User) ->
13241326
[VHostPath, User#user.username, VHostPath])
13251327
end.
13261328

1327-
is_over_node_connection_limit(Addr, Port) ->
1329+
is_over_node_connection_limit(RanchRef) ->
13281330
Limit = rabbit_misc:get_env(rabbit, connection_max, infinity),
13291331
case Limit of
13301332
infinity -> ok;
13311333
N when is_integer(N) ->
1332-
Ref = rabbit_networking:ranch_ref(Addr, Port),
1333-
#{active_connections := ActiveConns} = ranch:info(Ref),
1334+
#{active_connections := ActiveConns} = ranch:info(RanchRef),
13341335

13351336
case ActiveConns > Limit of
13361337
false -> ok;

0 commit comments

Comments
 (0)