fix(auto_subscribe): update config in cluster

This commit is contained in:
DDDHuang 2021-12-30 16:42:50 +08:00
parent 888e7fd727
commit f19ccdfcde
2 changed files with 20 additions and 4 deletions

View File

@ -16,6 +16,8 @@
-module(emqx_auto_subscribe). -module(emqx_auto_subscribe).
-include_lib("emqx/include/logger.hrl").
-define(HOOK_POINT, 'client.connected'). -define(HOOK_POINT, 'client.connected').
-define(MAX_AUTO_SUBSCRIBE, 20). -define(MAX_AUTO_SUBSCRIBE, 20).
@ -80,8 +82,18 @@ format(Rule = #{topic := Topic}) when is_map(Rule) ->
}. }.
update_(Topics) when length(Topics) =< ?MAX_AUTO_SUBSCRIBE -> update_(Topics) when length(Topics) =< ?MAX_AUTO_SUBSCRIBE ->
{ok, _} = emqx:update_config([auto_subscribe, topics], Topics), case emqx_conf:update([auto_subscribe, topics],
update_hook(); Topics,
#{rawconf_with_defaults => true, override_to => cluster}) of
{ok, #{config := NewTopics}} ->
ok = update_hook(),
{ok, NewTopics};
{error, Reason} ->
?LOG(error, "Auto Subscribe update config failed: ~0p", [Reason]),
{error, Reason}
end;
% {ok, _} = emqx:update_config([auto_subscribe, topics], Topics),
% update_hook();
update_(_Topics) -> update_(_Topics) ->
{error, quota_exceeded}. {error, quota_exceeded}.

View File

@ -22,6 +22,7 @@
-export([auto_subscribe/2]). -export([auto_subscribe/2]).
-define(INTERNAL_ERROR, 'INTERNAL_ERROR').
-define(EXCEED_LIMIT, 'EXCEED_LIMIT'). -define(EXCEED_LIMIT, 'EXCEED_LIMIT').
-define(BAD_REQUEST, 'BAD_REQUEST'). -define(BAD_REQUEST, 'BAD_REQUEST').
@ -90,6 +91,9 @@ auto_subscribe(put, #{body := Params}) ->
Message = list_to_binary(io_lib:format("Max auto subscribe topic count is ~p", Message = list_to_binary(io_lib:format("Max auto subscribe topic count is ~p",
[emqx_auto_subscribe:max_limit()])), [emqx_auto_subscribe:max_limit()])),
{409, #{code => ?EXCEED_LIMIT, message => Message}}; {409, #{code => ?EXCEED_LIMIT, message => Message}};
ok -> {error, Reason} ->
{200, emqx_auto_subscribe:list()} Message = list_to_binary(io_lib:format("Update config failed ~p", [Reason])),
{500, #{code => ?INTERNAL_ERROR, message => Message}};
{ok, NewTopics} ->
{200, NewTopics}
end. end.