Skip to content

Commit e71f77b

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. (cherry picked from commit 48467d6)
1 parent 9507fcb commit e71f77b

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
@@ -162,7 +164,7 @@ init(Parent, HelperSup, Ref) ->
162164
{ok, Sock} = rabbit_networking:handshake(Ref,
163165
application:get_env(rabbit, proxy_protocol, false)),
164166
Deb = sys:debug_options([]),
165-
start_connection(Parent, HelperSup, Deb, Sock).
167+
start_connection(Parent, HelperSup, Ref, Deb, Sock).
166168

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

@@ -289,10 +291,10 @@ socket_op(Sock, Fun) ->
289291
exit(normal)
290292
end.
291293

292-
-spec start_connection(pid(), pid(), any(), rabbit_net:socket()) ->
294+
-spec start_connection(pid(), pid(), ranch:ref(), any(), rabbit_net:socket()) ->
293295
no_return().
294296

295-
start_connection(Parent, HelperSup, Deb, Sock) ->
297+
start_connection(Parent, HelperSup, RanchRef, Deb, Sock) ->
296298
process_flag(trap_exit, true),
297299
RealSocket = rabbit_net:unwrap_socket(Sock),
298300
Name = case rabbit_net:connection_string(Sock, inbound) of
@@ -310,6 +312,7 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
310312
socket_op(Sock, fun (S) -> rabbit_net:socket_ends(S, inbound) end),
311313
?store_proc_name(Name),
312314
State = #v1{parent = Parent,
315+
ranch_ref = RanchRef,
313316
sock = RealSocket,
314317
connection = #connection{
315318
name = Name,
@@ -1209,17 +1212,16 @@ handle_method0(#'connection.tune_ok'{frame_max = FrameMax,
12091212
heartbeater = Heartbeater};
12101213

12111214
handle_method0(#'connection.open'{virtual_host = VHost},
1212-
State = #v1{connection_state = opening,
1215+
State = #v1{ranch_ref = RanchRef,
1216+
connection_state = opening,
12131217
connection = Connection = #connection{
12141218
log_name = ConnName,
1215-
host = Addr,
1216-
port = Port,
12171219
user = User = #user{username = Username},
12181220
protocol = Protocol},
12191221
helper_sup = SupPid,
12201222
sock = Sock,
12211223
throttle = Throttle}) ->
1222-
ok = is_over_node_connection_limit(Addr, Port),
1224+
ok = is_over_node_connection_limit(RanchRef),
12231225
ok = is_over_vhost_connection_limit(VHost, User),
12241226
ok = is_over_user_connection_limit(User),
12251227
ok = rabbit_access_control:check_vhost_access(User, VHost, {socket, Sock}, #{}),
@@ -1320,13 +1322,12 @@ is_vhost_alive(VHostPath, User) ->
13201322
[VHostPath, User#user.username, VHostPath])
13211323
end.
13221324

1323-
is_over_node_connection_limit(Addr, Port) ->
1325+
is_over_node_connection_limit(RanchRef) ->
13241326
Limit = rabbit_misc:get_env(rabbit, connection_max, infinity),
13251327
case Limit of
13261328
infinity -> ok;
13271329
N when is_integer(N) ->
1328-
Ref = rabbit_networking:ranch_ref(Addr, Port),
1329-
#{active_connections := ActiveConns} = ranch:info(Ref),
1330+
#{active_connections := ActiveConns} = ranch:info(RanchRef),
13301331

13311332
case ActiveConns > Limit of
13321333
false -> ok;

0 commit comments

Comments
 (0)