fix: rate limiter schema check crash and return 500
This commit is contained in:
parent
26f82fe2af
commit
38215f73fc
|
@ -202,34 +202,30 @@ to_rate(Str, CanInfinity, CanZero) ->
|
||||||
{ok, infinity};
|
{ok, infinity};
|
||||||
%% if time unit is 1s, it can be omitted
|
%% if time unit is 1s, it can be omitted
|
||||||
[QuotaStr] ->
|
[QuotaStr] ->
|
||||||
{ok, Val} = to_capacity(QuotaStr),
|
Fun = fun(Quota) ->
|
||||||
check_capacity(
|
{ok, Quota * minimum_period() / ?UNIT_TIME_IN_MS}
|
||||||
Str,
|
end,
|
||||||
Val,
|
to_capacity(QuotaStr, Str, CanZero, Fun);
|
||||||
CanZero,
|
|
||||||
fun(Quota) ->
|
|
||||||
{ok, Quota * minimum_period() / ?UNIT_TIME_IN_MS}
|
|
||||||
end
|
|
||||||
);
|
|
||||||
[QuotaStr, Interval] ->
|
[QuotaStr, Interval] ->
|
||||||
{ok, Val} = to_capacity(QuotaStr),
|
Fun = fun(Quota) ->
|
||||||
check_capacity(
|
case emqx_schema:to_duration_ms(Interval) of
|
||||||
Str,
|
{ok, Ms} when Ms > 0 ->
|
||||||
Val,
|
{ok, Quota * minimum_period() / Ms};
|
||||||
CanZero,
|
_ ->
|
||||||
fun(Quota) ->
|
{error, Str}
|
||||||
case emqx_schema:to_duration_ms(Interval) of
|
|
||||||
{ok, Ms} when Ms > 0 ->
|
|
||||||
{ok, Quota * minimum_period() / Ms};
|
|
||||||
_ ->
|
|
||||||
{error, Str}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
);
|
end,
|
||||||
|
to_capacity(QuotaStr, Str, CanZero, Fun);
|
||||||
_ ->
|
_ ->
|
||||||
{error, Str}
|
{error, Str}
|
||||||
end.
|
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) ->
|
check_capacity(_Str, 0, true, _Cont) ->
|
||||||
{ok, 0};
|
{ok, 0};
|
||||||
check_capacity(Str, 0, false, _Cont) ->
|
check_capacity(Str, 0, false, _Cont) ->
|
||||||
|
@ -247,18 +243,23 @@ to_initial(Str) ->
|
||||||
|
|
||||||
to_quota(Str, Regex) ->
|
to_quota(Str, Regex) ->
|
||||||
{ok, MP} = re:compile(Regex),
|
{ok, MP} = re:compile(Regex),
|
||||||
Result = re:run(Str, MP, [{capture, all_but_first, list}]),
|
try
|
||||||
case Result of
|
Result = re:run(Str, MP, [{capture, all_but_first, list}]),
|
||||||
{match, [Quota, Unit]} ->
|
case Result of
|
||||||
Val = erlang:list_to_integer(Quota),
|
{match, [Quota, Unit]} ->
|
||||||
Unit2 = string:to_lower(Unit),
|
Val = erlang:list_to_integer(Quota),
|
||||||
{ok, apply_unit(Unit2, Val)};
|
Unit2 = string:to_lower(Unit),
|
||||||
{match, [Quota, ""]} ->
|
{ok, apply_unit(Unit2, Val)};
|
||||||
{ok, erlang:list_to_integer(Quota)};
|
{match, [Quota, ""]} ->
|
||||||
{match, ""} ->
|
{ok, erlang:list_to_integer(Quota)};
|
||||||
{ok, infinity};
|
{match, ""} ->
|
||||||
_ ->
|
{ok, infinity};
|
||||||
{error, Str}
|
_ ->
|
||||||
|
{error, Str}
|
||||||
|
end
|
||||||
|
catch
|
||||||
|
_:Error ->
|
||||||
|
{error, Error}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
apply_unit("", Val) -> Val;
|
apply_unit("", Val) -> Val;
|
||||||
|
|
|
@ -76,8 +76,9 @@ pre_config_update(_Path, UpdateConf0, RawConf) ->
|
||||||
post_config_update(_, _Req, NewConf, OldConf, _AppEnvs) ->
|
post_config_update(_, _Req, NewConf, OldConf, _AppEnvs) ->
|
||||||
#{listeners := NewListeners} = NewConf,
|
#{listeners := NewListeners} = NewConf,
|
||||||
#{listeners := OldListeners} = OldConf,
|
#{listeners := OldListeners} = OldConf,
|
||||||
case NewListeners =:= OldListeners of
|
_ =
|
||||||
true -> ok;
|
case NewListeners =:= OldListeners of
|
||||||
false -> erlang:send_after(500, ?MODULE, {update_listeners, OldListeners, NewListeners})
|
true -> ok;
|
||||||
end,
|
false -> erlang:send_after(500, ?MODULE, {update_listeners, OldListeners, NewListeners})
|
||||||
|
end,
|
||||||
ok.
|
ok.
|
||||||
|
|
|
@ -813,7 +813,9 @@ schema_converter(Options) ->
|
||||||
|
|
||||||
serialize_hocon_error_msg({_Schema, Errors}) ->
|
serialize_hocon_error_msg({_Schema, Errors}) ->
|
||||||
Msg = lists:map(fun hocon_error/1, 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}}) ->
|
hocon_error({validation_error, #{reason := #{exception := Exception}, path := Path}}) ->
|
||||||
io_lib:format("~ts: ~p", [sub_path(Path), Exception]);
|
io_lib:format("~ts: ~p", [sub_path(Path), Exception]);
|
||||||
|
|
|
@ -55,7 +55,7 @@ t_http_test(_Config) ->
|
||||||
ErrorTrace = #{},
|
ErrorTrace = #{},
|
||||||
{error, {"HTTP/1.1", 400, "Bad Request"}, Body} =
|
{error, {"HTTP/1.1", 400, "Bad Request"}, Body} =
|
||||||
request_api(post, api_path("trace"), Header, ErrorTrace),
|
request_api(post, api_path("trace"), Header, ErrorTrace),
|
||||||
?assertMatch(#{<<"code">> := <<"BAD_REQUEST">>} = json(Body)),
|
?assertMatch(#{<<"code">> := <<"BAD_REQUEST">>}, json(Body)),
|
||||||
|
|
||||||
Name = <<"test-name">>,
|
Name = <<"test-name">>,
|
||||||
Trace = [
|
Trace = [
|
||||||
|
|
Loading…
Reference in New Issue