Skip to content

Commit 19a7518

Browse files
SimonUngeansd
authored andcommitted
Remove checks to vhost-limit as that is now handled by rabbit_queue_type:declare
Add new error return tuple when queue limit is exceed
1 parent 698cdc5 commit 19a7518

8 files changed

+54
-72
lines changed

deps/rabbit/src/rabbit_amqp_management.erl

+4-12
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ handle_http_req(HttpMethod = <<"PUT">>,
142142
{ok, Result} ->
143143
Result;
144144
{error, not_found} ->
145-
ok = check_vhost_queue_limit(QName),
146145
PermCache2 = check_dead_letter_exchange(QName, QArgs, User, PermCache1),
147146
case rabbit_amqqueue:declare(
148147
QName, Durable, AutoDelete, QArgs, Owner, Username) of
@@ -160,6 +159,10 @@ handle_http_req(HttpMethod = <<"PUT">>,
160159
%% Must have been created in the meantime. Loop around again.
161160
handle_http_req(HttpMethod, PathSegments, Query, ReqPayload,
162161
Vhost, User, ConnPid, {PermCache2, TopicPermCache});
162+
{error, queue_limit_exceeded, Reason, ReasonArgs} ->
163+
throw(<<"403">>,
164+
Reason,
165+
ReasonArgs);
163166
{protocol_error, _ErrorType, Reason, ReasonArgs} ->
164167
throw(<<"400">>, Reason, ReasonArgs)
165168
end;
@@ -676,17 +679,6 @@ prohibit_reserved_amq(Res = #resource{name = <<"amq.", _/binary>>}) ->
676679
prohibit_reserved_amq(#resource{}) ->
677680
ok.
678681

679-
check_vhost_queue_limit(QName = #resource{virtual_host = Vhost}) ->
680-
case rabbit_vhost_limit:is_over_queue_limit(Vhost) of
681-
false ->
682-
ok;
683-
{true, Limit} ->
684-
throw(<<"403">>,
685-
"refused to declare ~ts because vhost queue limit ~b is reached",
686-
[rabbit_misc:rs(QName), Limit])
687-
end.
688-
689-
690682
check_dead_letter_exchange(QName = #resource{virtual_host = Vhost}, QArgs, User, PermCache0) ->
691683
case rabbit_misc:r_arg(Vhost, exchange, QArgs, ?DEAD_LETTER_EXCHANGE_KEY) of
692684
undefined ->

deps/rabbit/src/rabbit_amqp_session.erl

+6-13
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
-export([init/1,
7272
terminate/2,
7373
handle_call/3,
74-
handle_cast/2,
74+
handle_cast/2,
7575
handle_info/2,
7676
format_status/1]).
7777

@@ -2612,7 +2612,6 @@ declare_queue(QNameBin,
26122612
PermCache0) ->
26132613
QName = rabbit_misc:r(Vhost, queue, QNameBin),
26142614
PermCache = check_resource_access(QName, configure, User, PermCache0),
2615-
check_vhost_queue_limit(Vhost, QName),
26162615
rabbit_core_metrics:queue_declared(QName),
26172616
Q0 = amqqueue:new(QName,
26182617
_Pid = none,
@@ -2628,6 +2627,11 @@ declare_queue(QNameBin,
26282627
rabbit_core_metrics:queue_created(QName);
26292628
{existing, _Q} ->
26302629
ok;
2630+
{error, queue_limit_exceeded, Reason, ReasonArgs} ->
2631+
protocol_error(
2632+
?V_1_0_AMQP_ERROR_RESOURCE_LIMIT_EXCEEDED,
2633+
Reason,
2634+
ReasonArgs);
26312635
Other ->
26322636
protocol_error(?V_1_0_AMQP_ERROR_INTERNAL_ERROR,
26332637
"Failed to declare ~s: ~p",
@@ -2867,17 +2871,6 @@ check_topic_authorisation(#exchange{type = topic,
28672871
check_topic_authorisation(_, _, _, _, Cache) ->
28682872
Cache.
28692873

2870-
check_vhost_queue_limit(Vhost, QName) ->
2871-
case rabbit_vhost_limit:is_over_queue_limit(Vhost) of
2872-
false ->
2873-
ok;
2874-
{true, Limit} ->
2875-
protocol_error(
2876-
?V_1_0_AMQP_ERROR_RESOURCE_LIMIT_EXCEEDED,
2877-
"cannot declare ~ts: vhost queue limit (~p) is reached",
2878-
[rabbit_misc:rs(QName), Limit])
2879-
end.
2880-
28812874
check_user_id(Mc, User) ->
28822875
case rabbit_access_control:check_user_id(Mc, User) of
28832876
ok ->

deps/rabbit/src/rabbit_amqqueue.erl

+2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ find_recoverable_queues() ->
201201
{'new' | 'existing' | 'owner_died', amqqueue:amqqueue()} |
202202
{'new', amqqueue:amqqueue(), rabbit_fifo_client:state()} |
203203
{'absent', amqqueue:amqqueue(), absent_reason()} |
204+
{'error', Type :: atom(), Reason :: string(), Args :: term()} |
204205
{protocol_error, Type :: atom(), Reason :: string(), Args :: term()}.
205206
declare(QueueName, Durable, AutoDelete, Args, Owner, ActingUser) ->
206207
declare(QueueName, Durable, AutoDelete, Args, Owner, ActingUser, node()).
@@ -219,6 +220,7 @@ declare(QueueName, Durable, AutoDelete, Args, Owner, ActingUser) ->
219220
node() | {'ignore_location', node()}) ->
220221
{'new' | 'existing' | 'owner_died', amqqueue:amqqueue()} |
221222
{'absent', amqqueue:amqqueue(), absent_reason()} |
223+
{'error', Type :: atom(), Reason :: string(), Args :: term()} |
222224
{protocol_error, Type :: atom(), Reason :: string(), Args :: term()}.
223225
declare(QueueName = #resource{virtual_host = VHost}, Durable, AutoDelete, Args,
224226
Owner, ActingUser, Node) ->

deps/rabbit/src/rabbit_channel.erl

+2
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,8 @@ handle_method(#'queue.declare'{queue = QueueNameBin,
25032503
%% connection has died. Pretend the queue exists though,
25042504
%% just so nothing fails.
25052505
{ok, QueueName, 0, 0};
2506+
{error, queue_limit_exceeded, Reason, ReasonArgs} ->
2507+
rabbit_misc:precondition_failed(Reason, ReasonArgs);
25062508
{protocol_error, ErrorType, Reason, ReasonArgs} ->
25072509
rabbit_misc:protocol_error(ErrorType, Reason, ReasonArgs)
25082510
end;

deps/rabbit/src/rabbit_queue_type.erl

+11-9
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ is_compatible(Type, Durable, Exclusive, AutoDelete) ->
304304
{'new' | 'existing' | 'owner_died', amqqueue:amqqueue()} |
305305
{'absent', amqqueue:amqqueue(), rabbit_amqqueue:absent_reason()} |
306306
{protocol_error, Type :: atom(), Reason :: string(), Args :: term()} |
307+
{'error', Type :: atom(), Reason :: string(), Args :: term()} |
307308
{'error', Err :: term() }.
308309
declare(Q0, Node) ->
309310
Q = rabbit_queue_decorator:set(rabbit_policy:set(Q0)),
@@ -774,7 +775,7 @@ known_queue_type_names() ->
774775

775776
-spec check_queue_limits(amqqueue:amqqueue()) ->
776777
ok |
777-
{protocol_error, Type :: atom(), Reason :: string(), Args :: term()}.
778+
{error, queue_limit_exceeded, Reason :: string(), Args :: term()}.
778779
check_queue_limits(Q) ->
779780
maybe
780781
ok ?= check_vhost_queue_limit(Q),
@@ -788,10 +789,9 @@ check_vhost_queue_limit(Q) ->
788789
false ->
789790
ok;
790791
{true, Limit} ->
791-
{protocol_error, precondition_failed,
792-
"cannot declare queue '~ts': "
793-
"queue limit in vhost '~ts' (~tp) is reached",
794-
[QueueName, VHost, Limit]}
792+
queue_limit_error("cannot declare queue '~ts': "
793+
"queue limit in vhost '~ts' (~tp) is reached",
794+
[QueueName, VHost, Limit])
795795
end.
796796

797797
check_cluster_queue_limit(Q) ->
@@ -802,11 +802,13 @@ check_cluster_queue_limit(Q) ->
802802
Limit ->
803803
case rabbit_db_queue:count() >= Limit of
804804
true ->
805-
{protocol_error, precondition_failed,
806-
"cannot declare queue '~ts': "
807-
"queue limit in cluster (~tp) is reached",
808-
[QueueName, Limit]};
805+
queue_limit_error("cannot declare queue '~ts': "
806+
"queue limit in cluster (~tp) is reached",
807+
[QueueName, Limit]);
809808
false ->
810809
ok
811810
end
812811
end.
812+
813+
queue_limit_error(Reason, ReasonArgs) ->
814+
{error, queue_limit_exceeded, Reason, ReasonArgs}.

deps/rabbit/test/amqp_auth_SUITE.erl

+2-3
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ v1_vhost_queue_limit(Config) ->
727727
Session1, <<"test-sender-1">>, TargetAddress),
728728
ExpectedErr = amqp_error(
729729
?V_1_0_AMQP_ERROR_RESOURCE_LIMIT_EXCEEDED,
730-
<<"cannot declare queue 'q1' in vhost 'test vhost': vhost queue limit (0) is reached">>),
730+
<<"cannot declare queue 'q1': queue limit in vhost 'test vhost' (0) is reached">>),
731731
receive {amqp10_event, {session, Session1, {ended, ExpectedErr}}} -> ok
732732
after 5000 -> flush(missing_ended),
733733
ct:fail("did not receive expected error")
@@ -831,8 +831,7 @@ declare_queue_vhost_queue_limit(Config) ->
831831
?assertMatch(#{subject := <<"403">>}, amqp10_msg:properties(Resp)),
832832
?assertEqual(
833833
#'v1_0.amqp_value'{
834-
content = {utf8, <<"refused to declare queue '", QName/binary, "' in vhost 'test vhost' ",
835-
"because vhost queue limit 0 is reached">>}},
834+
content = {utf8, <<"cannot declare queue '", QName/binary, "': queue limit in vhost 'test vhost' (0) is reached">>}},
836835
amqp10_msg:body(Resp)),
837836

838837
ok = cleanup_pair(Init),

deps/rabbitmq_mqtt/src/rabbit_mqtt_processor.erl

+21-26
Original file line numberDiff line numberDiff line change
@@ -1354,32 +1354,27 @@ create_queue(QNamePart, QOwner, QArgs, QType,
13541354
Err0 -> Err0
13551355
end
13561356
end,
1357-
case rabbit_vhost_limit:is_over_queue_limit(VHost) of
1358-
false ->
1359-
rabbit_core_metrics:queue_declared(QName),
1360-
Q0 = amqqueue:new(QName,
1361-
none,
1362-
_Durable = true,
1363-
_AutoDelete = false,
1364-
QOwner,
1365-
QArgs,
1366-
VHost,
1367-
#{user => Username},
1368-
QType),
1369-
case rabbit_queue_type:declare(Q0, node()) of
1370-
{new, Q} when ?is_amqqueue(Q) ->
1371-
rabbit_core_metrics:queue_created(QName),
1372-
{ok, Q};
1373-
Other ->
1374-
?LOG_ERROR("Failed to declare ~s: ~p",
1375-
[rabbit_misc:rs(QName), Other]),
1376-
{error, queue_declare}
1377-
end;
1378-
{true, Limit} ->
1379-
?LOG_ERROR("cannot declare ~s because "
1380-
"queue limit ~p in vhost '~s' is reached",
1381-
[rabbit_misc:rs(QName), Limit, VHost]),
1382-
{error, queue_limit_exceeded}
1357+
rabbit_core_metrics:queue_declared(QName),
1358+
Q0 = amqqueue:new(QName,
1359+
none,
1360+
_Durable = true,
1361+
_AutoDelete = false,
1362+
QOwner,
1363+
QArgs,
1364+
VHost,
1365+
#{user => Username},
1366+
QType),
1367+
case rabbit_queue_type:declare(Q0, node()) of
1368+
{new, Q} when ?is_amqqueue(Q) ->
1369+
rabbit_core_metrics:queue_created(QName),
1370+
{ok, Q};
1371+
{error, queue_limit_exceeded, Reason, ReasonArgs} ->
1372+
?LOG_ERROR(Reason, ReasonArgs),
1373+
{error, queue_limit_exceeded};
1374+
Other ->
1375+
?LOG_ERROR("Failed to declare ~s: ~p",
1376+
[rabbit_misc:rs(QName), Other]),
1377+
{error, queue_declare}
13831378
end
13841379
else
13851380
{error, access_refused} = Err ->

deps/rabbitmq_stream/src/rabbit_stream_manager.erl

+6-9
Original file line numberDiff line numberDiff line change
@@ -524,15 +524,6 @@ handle_info(Info, State) ->
524524
create_stream(VirtualHost, Reference, Arguments, Username) ->
525525
StreamQueueArguments = stream_queue_arguments(Arguments),
526526
maybe
527-
ok ?= case rabbit_vhost_limit:is_over_queue_limit(VirtualHost) of
528-
false ->
529-
ok;
530-
{true, Limit} ->
531-
rabbit_log:warning("Cannot declare stream ~tp because "
532-
"queue limit ~tp in vhost '~tp' is reached",
533-
[Reference, Limit, VirtualHost]),
534-
{error, validation_failed}
535-
end,
536527
ok ?= validate_stream_queue_arguments(StreamQueueArguments),
537528
do_create_stream(VirtualHost, Reference, StreamQueueArguments, Username)
538529
else
@@ -581,6 +572,12 @@ do_create_stream(VirtualHost, Reference, StreamQueueArguments, Username) ->
581572
rabbit_log:warning("Error while creating ~tp stream, ~tp",
582573
[Reference, Err]),
583574
{error, internal_error};
575+
{error,
576+
queue_limit_exceeded, Reason, ReasonArg} ->
577+
rabbit_log:warning("Cannot declare stream ~tp because, "
578+
++ Reason,
579+
[Reference] ++ ReasonArg),
580+
{error, validation_failed};
584581
{protocol_error,
585582
precondition_failed,
586583
Msg,

0 commit comments

Comments
 (0)