Merge pull request #8234 from zhongwencool/listener-reinforcement

feat: Listener reinforcement
This commit is contained in:
zhongwencool 2022-06-16 14:05:32 +08:00 committed by GitHub
commit 5287dc322e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -377,11 +377,11 @@ post_config_update([listeners, Type, Name], {create, _Request}, NewConf, undefin
start_listener(Type, Name, NewConf);
post_config_update([listeners, Type, Name], {update, _Request}, NewConf, OldConf, _AppEnvs) ->
case NewConf of
#{<<"enabled">> := true} -> restart_listener(Type, Name, {OldConf, NewConf});
#{enabled := true} -> restart_listener(Type, Name, {OldConf, NewConf});
_ -> ok
end;
post_config_update([listeners, _Type, _Name], '$remove', undefined, undefined, _AppEnvs) ->
{error, not_found};
ok;
post_config_update([listeners, Type, Name], '$remove', undefined, OldConf, _AppEnvs) ->
case stop_listener(Type, Name, OldConf) of
ok ->

View File

@ -355,9 +355,8 @@ crud_listeners_by_id(post, #{bindings := #{id := Id}, body := Body0}) ->
end;
crud_listeners_by_id(delete, #{bindings := #{id := Id}}) ->
{ok, #{type := Type, name := Name}} = emqx_listeners:parse_listener_id(Id),
case remove([listeners, Type, Name]) of
case ensure_remove([listeners, Type, Name]) of
{ok, _} -> {204};
{error, not_found} -> {204};
{error, Reason} -> {400, #{code => 'BAD_REQUEST', message => err_msg(Reason)}}
end.
@ -552,7 +551,7 @@ action(Path, Action, Conf) ->
create(Path, Conf) ->
wrap(emqx_conf:update(Path, {create, Conf}, ?OPTS(cluster))).
remove(Path) ->
ensure_remove(Path) ->
wrap(emqx_conf:remove(Path, ?OPTS(cluster))).
wrap({error, {post_config_update, emqx_listeners, Reason}}) -> {error, Reason};

View File

@ -142,6 +142,21 @@ crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type) ->
?assertMatch(#{<<"acceptors">> := Acceptors1}, Update),
Get2 = request(get, NewPath, [], []),
?assertMatch(#{<<"acceptors">> := Acceptors1}, Get2),
?assert(is_running(NewListenerId)),
%% update an stopped listener
action_listener(NewListenerId, "stop", false),
?assertNot(is_running(NewListenerId)),
%% update
Get3 = request(get, NewPath, [], []),
#{<<"acceptors">> := Acceptors3} = Get3,
Acceptors4 = Acceptors3 + 1,
Update1 =
request(put, NewPath, [], Get3#{<<"acceptors">> => Acceptors4}),
?assertMatch(#{<<"acceptors">> := Acceptors4}, Update1),
Get4 = request(get, NewPath, [], []),
?assertMatch(#{<<"acceptors">> := Acceptors4}, Get4),
?assertNot(is_running(NewListenerId)),
%% delete
?assertEqual([], delete(NewPath)),
@ -151,6 +166,11 @@ crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type) ->
?assertEqual([], delete(NewPath)),
ok.
t_delete_nonexistent_listener(_) ->
NonExist = emqx_mgmt_api_test_util:api_path(["listeners", "tcp:nonexistent"]),
?assertEqual([], delete(NonExist)),
ok.
t_action_listeners(_) ->
ID = "tcp:default",
action_listener(ID, "stop", false),