Skip to content

Commit

Permalink
Switch rabbitmqctl set_vhost_limits to use JSON payload values
Browse files Browse the repository at this point in the history
Just like policies do.
  • Loading branch information
michaelklishin committed Jan 19, 2016
1 parent 7fc5f1a commit f3a1101
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
4 changes: 0 additions & 4 deletions include/rabbit_cli.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
-define(OFFLINE_OPT, "--offline").
-define(ONLINE_OPT, "--online").

-define(MAX_CONNECTIONS_OPT, "--max-connections").

-define(NODE_DEF(Node), {?NODE_OPT, {option, Node}}).
-define(QUIET_DEF, {?QUIET_OPT, flag}).
-define(VHOST_DEF, {?VHOST_OPT, {option, "/"}}).
Expand All @@ -48,8 +46,6 @@
-define(OFFLINE_DEF, {?OFFLINE_OPT, flag}).
-define(ONLINE_DEF, {?ONLINE_OPT, flag}).

-define(MAX_CONNECTIONS_DEF, {?MAX_CONNECTIONS_OPT, {option, "0"}}).

-define(RPC_TIMEOUT, infinity).

%% Subset of standartized exit codes from sysexits.h, see
Expand Down
5 changes: 3 additions & 2 deletions src/rabbit_control_main.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
{clear_policy, [?VHOST_DEF]},
{list_policies, [?VHOST_DEF]},

{set_vhost_limits, [?VHOST_DEF, ?MAX_CONNECTIONS_DEF]},
{set_vhost_limits, [?VHOST_DEF]},

{list_queues, [?VHOST_DEF]},
{list_exchanges, [?VHOST_DEF]},
Expand Down Expand Up @@ -514,10 +514,11 @@ action(clear_policy, Node, [Key], Opts, Inform) ->
Inform("Clearing policy ~p", [Key]),
rpc_call(Node, rabbit_policy, delete, [VHostArg, list_to_binary(Key)]);

action(set_vhost_limits, _Node, [], Opts, Inform) ->
action(set_vhost_limits, Node, [Defn], Opts, Inform) ->
Msg = "Setting vhost limits for vhost ~p",
VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)),
Inform(Msg, [VHostArg]),
rpc_call(Node, rabbit_vhost_limit, parse_set, [VHostArg, Defn]),
ok;

action(report, Node, _Args, _Opts, Inform) ->
Expand Down
30 changes: 29 additions & 1 deletion src/rabbit_vhost.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

%%----------------------------------------------------------------------------

-export([add/1, delete/1, exists/1, list/0, with/2, assert/1]).
-export([add/1, delete/1, exists/1, list/0, with/2, assert/1, update/2,
set_limits/2]).
-export([info/1, info/2, info_all/0, info_all/1, info_all/2, info_all/3]).

-ifdef(use_specs).
Expand All @@ -31,6 +32,7 @@
-spec(list/0 :: () -> [rabbit_types:vhost()]).
-spec(with/2 :: (rabbit_types:vhost(), rabbit_misc:thunk(A)) -> A).
-spec(assert/1 :: (rabbit_types:vhost()) -> 'ok').
-spec(update/2 :: (rabbit_types:vhost(), rabbit_misc:thunk(A)) -> A).

-spec(info/1 :: (rabbit_types:vhost()) -> rabbit_types:infos()).
-spec(info/2 :: (rabbit_types:vhost(), rabbit_types:info_keys())
Expand Down Expand Up @@ -142,6 +144,32 @@ assert(VHostPath) -> case exists(VHostPath) of
false -> throw({error, {no_such_vhost, VHostPath}})
end.

update(VHostPath, Fun) ->
case mnesia:read({rabbit_vhost, VHostPath}) of
[] ->
mnesia:abort({no_such_vhost, VHostPath});
[V] ->
V1 = Fun(V),
ok = mnesia:write(rabbit_vhost, V1, write),
V1
end.

limits_of(VHostPath) when is_binary(VHostPath) ->
assert(VHostPath),
case mnesia:read({rabbit_vhost, VHostPath}) of
[] ->
mnesia:abort({no_such_vhost, VHostPath});
[V = #vhost{limits = Limits}] ->
Limits
end;
limits_of(VHost = #vhost{virtual_host = Name}) ->
limits_of(Name).

set_limits(VHost = #vhost{}, undefined) ->
VHost#vhost{limits = undefined};
set_limits(VHost = #vhost{}, Limits) ->
VHost#vhost{limits = Limits}.

%%----------------------------------------------------------------------------

infos(Items, X) -> [{Item, i(Item, X)} || Item <- Items].
Expand Down
75 changes: 75 additions & 0 deletions src/rabbit_vhost_limit.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License
%% at http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
%%

-module(rabbit_vhost_limit).

-behaviour(rabbit_runtime_parameter).

-include("rabbit.hrl").

-export([register/0]).
-export([parse_set/2]).
-export([validate/5, notify/4, notify_clear/3]).

-rabbit_boot_step({?MODULE,
[{description, "vhost limit parameters"},
{mfa, {rabbit_vhost_limit, register, []}},
{requires, rabbit_registry},
{enables, recovery}]}).

%%----------------------------------------------------------------------------

register() ->
rabbit_registry:register(runtime_parameter, <<"vhost-limits">>, ?MODULE).

validate(_VHost, <<"vhost-limits">>, Name, Term, _User) ->
rabbit_parameter_validation:proplist(
Name, vhost_limit_validation(), Term).

notify(VHost, <<"vhost-limits">>, <<"limits">>, Limits) ->
rabbit_event:notify(vhost_limits_set, [{name, <<"limits">>} | Limits]),
update_vhost(VHost, Limits).

notify_clear(VHost, <<"vhost-limits">>, <<"limits">>) ->
rabbit_event:notify(vhost_limits_cleared, [{name, <<"limits">>}]),
update_vhost(VHost, undefined).

%%----------------------------------------------------------------------------

parse_set(VHost, Defn) ->
case rabbit_misc:json_decode(Defn) of
{ok, JSON} ->
set(VHost, rabbit_misc:json_to_term(JSON));
error ->
{error_string, "JSON decoding error"}
end.

set(VHost, Defn) ->
rabbit_runtime_parameters:set_any(VHost, <<"vhost-limits">>,
<<"limits">>, Defn, none).

vhost_limit_validation() ->
[{<<"max-connections">>, fun rabbit_parameter_validation:number/2, mandatory}].

update_vhost(VHostName, Limits) ->
rabbit_misc:execute_mnesia_transaction(
fun() ->
rabbit_vhost:update(VHostName,
fun(VHost) ->
rabbit_vhost:set_limits(VHost, Limits)
end)
end),
ok.

0 comments on commit f3a1101

Please sign in to comment.