Merge pull request #10711 from lafirest/fix/fix_infinity_bucket

fix(limiter): a bucket with an infinity rate shouldn't be added to limiter server
This commit is contained in:
JianBo He 2023-05-17 10:40:59 +08:00 committed by GitHub
commit 0dd3325d70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 3 deletions

View File

@ -3,7 +3,7 @@
{id, "emqx"}, {id, "emqx"},
{description, "EMQX Core"}, {description, "EMQX Core"},
% strict semver, bump manually! % strict semver, bump manually!
{vsn, "5.0.25"}, {vsn, "5.0.26"},
{modules, []}, {modules, []},
{registered, []}, {registered, []},
{applications, [ {applications, [

View File

@ -36,6 +36,7 @@
calc_capacity/1, calc_capacity/1,
extract_with_type/2, extract_with_type/2,
default_client_config/0, default_client_config/0,
default_bucket_config/0,
short_paths_fields/1, short_paths_fields/1,
get_listener_opts/1, get_listener_opts/1,
get_node_opts/1, get_node_opts/1,

View File

@ -131,6 +131,9 @@ connect(Id, Type, Cfg) ->
-spec add_bucket(limiter_id(), limiter_type(), hocons:config() | undefined) -> ok. -spec add_bucket(limiter_id(), limiter_type(), hocons:config() | undefined) -> ok.
add_bucket(_Id, _Type, undefined) -> add_bucket(_Id, _Type, undefined) ->
ok; ok;
%% a bucket with an infinity rate shouldn't be added to this server, because it is always full
add_bucket(_Id, _Type, #{rate := infinity}) ->
ok;
add_bucket(Id, Type, Cfg) -> add_bucket(Id, Type, Cfg) ->
?CALL(Type, {add_bucket, Id, Cfg}). ?CALL(Type, {add_bucket, Id, Cfg}).
@ -507,8 +510,6 @@ make_root(#{rate := Rate, burst := Burst}) ->
correction => 0 correction => 0
}. }.
do_add_bucket(_Id, #{rate := infinity}, #{root := #{rate := infinity}} = State) ->
State;
do_add_bucket(Id, #{rate := Rate} = Cfg, #{buckets := Buckets} = State) -> do_add_bucket(Id, #{rate := Rate} = Cfg, #{buckets := Buckets} = State) ->
case maps:get(Id, Buckets, undefined) of case maps:get(Id, Buckets, undefined) of
undefined -> undefined ->

View File

@ -617,6 +617,24 @@ t_extract_with_type(_) ->
) )
). ).
t_add_bucket(_) ->
Checker = fun(Size) ->
#{buckets := Buckets} = sys:get_state(emqx_limiter_server:whereis(bytes)),
?assertEqual(Size, maps:size(Buckets), Buckets)
end,
DefBucket = emqx_limiter_schema:default_bucket_config(),
?assertEqual(ok, emqx_limiter_server:add_bucket(?FUNCTION_NAME, bytes, undefined)),
Checker(0),
?assertEqual(ok, emqx_limiter_server:add_bucket(?FUNCTION_NAME, bytes, DefBucket)),
Checker(0),
?assertEqual(
ok, emqx_limiter_server:add_bucket(?FUNCTION_NAME, bytes, DefBucket#{rate := 100})
),
Checker(1),
?assertEqual(ok, emqx_limiter_server:del_bucket(?FUNCTION_NAME, bytes)),
Checker(0),
ok.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Test Cases Create Instance %% Test Cases Create Instance
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------