fix: topic metrics deny wildcard

This commit is contained in:
DDDHuang 2021-10-11 15:07:13 +08:00 committed by DDDHuang
parent 40963221f8
commit c90342da4a
2 changed files with 21 additions and 7 deletions

View File

@ -304,8 +304,8 @@ do_register(Topic, Speeds) ->
true -> true ->
{error, already_existed}; {error, already_existed};
false -> false ->
case number_of_registered_topics() < ?MAX_TOPICS of case {number_of_registered_topics() < ?MAX_TOPICS, emqx_topic:wildcard(Topic)} of
true -> {true, false} ->
CreateTime = emqx_rule_funcs:now_rfc3339(), CreateTime = emqx_rule_funcs:now_rfc3339(),
CRef = counters:new(counters_size(), [write_concurrency]), CRef = counters:new(counters_size(), [write_concurrency]),
ok = reset_counter(CRef), ok = reset_counter(CRef),
@ -318,8 +318,12 @@ do_register(Topic, Speeds) ->
end, Speeds, ?TOPIC_METRICS), end, Speeds, ?TOPIC_METRICS),
add_topic_config(Topic), add_topic_config(Topic),
{ok, NSpeeds}; {ok, NSpeeds};
false -> {true, true} ->
{error, quota_exceeded} {error, bad_topic};
{false, false} ->
{error, quota_exceeded};
{false, true} ->
{error, {quota_exceeded, bad_topic}}
end end
end. end.

View File

@ -36,6 +36,8 @@
-define(EXCEED_LIMIT, 'EXCEED_LIMIT'). -define(EXCEED_LIMIT, 'EXCEED_LIMIT').
-define(BAD_TOPIC, 'BAD_TOPIC').
-define(BAD_REQUEST, 'BAD_REQUEST'). -define(BAD_REQUEST, 'BAD_REQUEST').
api_spec() -> api_spec() ->
@ -94,7 +96,7 @@ topic_metrics_api() ->
responses => #{ responses => #{
<<"200">> => schema(<<"Create topic metrics success">>), <<"200">> => schema(<<"Create topic metrics success">>),
<<"409">> => error_schema(<<"Topic metrics max limit">>, [?EXCEED_LIMIT]), <<"409">> => error_schema(<<"Topic metrics max limit">>, [?EXCEED_LIMIT]),
<<"400">> => error_schema(<<"Topic metrics already exist or bad topic">>, [?BAD_REQUEST]) <<"400">> => error_schema(<<"Topic metrics already exist or bad topic">>, [?BAD_REQUEST, ?BAD_TOPIC])
} }
} }
}, },
@ -164,12 +166,20 @@ list_metrics() ->
register(Topic) -> register(Topic) ->
case emqx_topic_metrics:register(Topic) of case emqx_topic_metrics:register(Topic) of
{error, quota_exceeded} -> {error, quota_exceeded} ->
Message = list_to_binary(io_lib:format("Max topic metrics count is ~p", Message = list_to_binary(io_lib:format("Max topic metrics count is ~p",
[emqx_topic_metrics:max_limit()])), [emqx_topic_metrics:max_limit()])),
{409, #{code => ?EXCEED_LIMIT, message => Message}}; {409, #{code => ?EXCEED_LIMIT, message => Message}};
{error, bad_topic} ->
Message = list_to_binary(io_lib:format("Bad Topic, topic cannot have wildcard ~p",
[Topic])),
{400, #{code => ?BAD_TOPIC, message => Message}};
{error, {quota_exceeded, bad_topic}} ->
Message = list_to_binary(io_lib:format("Max topic metrics count is ~p, and topic cannot have wildcard ~p",
[emqx_topic_metrics:max_limit(), Topic])),
{400, #{code => ?BAD_REQUEST, message => Message}};
{error, already_existed} -> {error, already_existed} ->
Message = list_to_binary(io_lib:format("Topic ~p already registered", [Topic])), Message = list_to_binary(io_lib:format("Topic ~p already registered", [Topic])),
{400, #{code => ?BAD_REQUEST, message => Message}}; {400, #{code => ?BAD_TOPIC, message => Message}};
ok -> ok ->
{200} {200}
end. end.