From 38215f73fc0d06768bc908a1458b01687e08f982 Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Sun, 24 Apr 2022 11:17:02 +0800 Subject: [PATCH] fix: rate limiter schema check crash and return 500 --- .../emqx_limiter/src/emqx_limiter_schema.erl | 69 ++++++++++--------- .../src/emqx_dashboard_config.erl | 9 +-- .../src/emqx_dashboard_swagger.erl | 4 +- .../test/emqx_mgmt_api_trace_SUITE.erl | 2 +- 4 files changed, 44 insertions(+), 40 deletions(-) diff --git a/apps/emqx/src/emqx_limiter/src/emqx_limiter_schema.erl b/apps/emqx/src/emqx_limiter/src/emqx_limiter_schema.erl index e6d63a7bf..33db09d07 100644 --- a/apps/emqx/src/emqx_limiter/src/emqx_limiter_schema.erl +++ b/apps/emqx/src/emqx_limiter/src/emqx_limiter_schema.erl @@ -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) -> @@ -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; diff --git a/apps/emqx_dashboard/src/emqx_dashboard_config.erl b/apps/emqx_dashboard/src/emqx_dashboard_config.erl index 330fc3627..aea8304da 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_config.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_config.erl @@ -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. diff --git a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl index 7fb2613ee..da590f88d 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl @@ -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]); diff --git a/apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl index 1011427f2..b4f34eaf3 100644 --- a/apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl +++ b/apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl @@ -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 = [