Skip to content

Commit

Permalink
fix: rate limiter schema check crash and return 500
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongwencool committed Apr 24, 2022
1 parent 26f82fe commit 38215f7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
69 changes: 35 additions & 34 deletions apps/emqx/src/emqx_limiter/src/emqx_limiter_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -202,34 +202,30 @@ to_rate(Str, CanInfinity, CanZero) ->
{ok, infinity};
%% if time unit is 1s, it can be omitted
[QuotaStr] ->
{ok, Val} = to_capacity(QuotaStr),
check_capacity(
Str,
Val,
CanZero,
fun(Quota) ->
{ok, Quota * minimum_period() / ?UNIT_TIME_IN_MS}
end
);
Fun = fun(Quota) ->
{ok, Quota * minimum_period() / ?UNIT_TIME_IN_MS}
end,
to_capacity(QuotaStr, Str, CanZero, Fun);
[QuotaStr, Interval] ->
{ok, Val} = to_capacity(QuotaStr),
check_capacity(
Str,
Val,
CanZero,
fun(Quota) ->
case emqx_schema:to_duration_ms(Interval) of
{ok, Ms} when Ms > 0 ->
{ok, Quota * minimum_period() / Ms};
_ ->
{error, Str}
end
Fun = fun(Quota) ->
case emqx_schema:to_duration_ms(Interval) of
{ok, Ms} when Ms > 0 ->
{ok, Quota * minimum_period() / Ms};
_ ->
{error, Str}
end
);
end,
to_capacity(QuotaStr, Str, CanZero, Fun);
_ ->
{error, Str}
end.

to_capacity(QuotaStr, Str, CanZero, Fun) ->
case to_capacity(QuotaStr) of
{ok, Val} -> check_capacity(Str, Val, CanZero, Fun);
{error, _Error} -> {error, Str}
end.

check_capacity(_Str, 0, true, _Cont) ->
{ok, 0};
check_capacity(Str, 0, false, _Cont) ->
Expand All @@ -247,18 +243,23 @@ to_initial(Str) ->

to_quota(Str, Regex) ->
{ok, MP} = re:compile(Regex),
Result = re:run(Str, MP, [{capture, all_but_first, list}]),
case Result of
{match, [Quota, Unit]} ->
Val = erlang:list_to_integer(Quota),
Unit2 = string:to_lower(Unit),
{ok, apply_unit(Unit2, Val)};
{match, [Quota, ""]} ->
{ok, erlang:list_to_integer(Quota)};
{match, ""} ->
{ok, infinity};
_ ->
{error, Str}
try
Result = re:run(Str, MP, [{capture, all_but_first, list}]),
case Result of
{match, [Quota, Unit]} ->
Val = erlang:list_to_integer(Quota),
Unit2 = string:to_lower(Unit),
{ok, apply_unit(Unit2, Val)};
{match, [Quota, ""]} ->
{ok, erlang:list_to_integer(Quota)};
{match, ""} ->
{ok, infinity};
_ ->
{error, Str}
end
catch
_:Error ->
{error, Error}
end.

apply_unit("", Val) -> Val;
Expand Down
9 changes: 5 additions & 4 deletions apps/emqx_dashboard/src/emqx_dashboard_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ pre_config_update(_Path, UpdateConf0, RawConf) ->
post_config_update(_, _Req, NewConf, OldConf, _AppEnvs) ->
#{listeners := NewListeners} = NewConf,
#{listeners := OldListeners} = OldConf,
case NewListeners =:= OldListeners of
true -> ok;
false -> erlang:send_after(500, ?MODULE, {update_listeners, OldListeners, NewListeners})
end,
_ =
case NewListeners =:= OldListeners of
true -> ok;
false -> erlang:send_after(500, ?MODULE, {update_listeners, OldListeners, NewListeners})
end,
ok.
4 changes: 3 additions & 1 deletion apps/emqx_dashboard/src/emqx_dashboard_swagger.erl
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,9 @@ schema_converter(Options) ->

serialize_hocon_error_msg({_Schema, Errors}) ->
Msg = lists:map(fun hocon_error/1, Errors),
iolist_to_binary(string:join(Msg, ",")).
iolist_to_binary(string:join(Msg, ","));
serialize_hocon_error_msg(Error) ->
iolist_to_binary(io_lib:format("~p", [Error])).

hocon_error({validation_error, #{reason := #{exception := Exception}, path := Path}}) ->
io_lib:format("~ts: ~p", [sub_path(Path), Exception]);
Expand Down
2 changes: 1 addition & 1 deletion apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ t_http_test(_Config) ->
ErrorTrace = #{},
{error, {"HTTP/1.1", 400, "Bad Request"}, Body} =
request_api(post, api_path("trace"), Header, ErrorTrace),
?assertMatch(#{<<"code">> := <<"BAD_REQUEST">>} = json(Body)),
?assertMatch(#{<<"code">> := <<"BAD_REQUEST">>}, json(Body)),

Name = <<"test-name">>,
Trace = [
Expand Down

0 comments on commit 38215f7

Please sign in to comment.