refactor(mgmt): avoid dealing with router tab directly

Instead, contain all this behind `emqx_router` module interface.
This commit is contained in:
Andrew Mayorov 2023-08-08 15:34:04 +04:00
parent 9eccfa0909
commit dcb63440bc
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
2 changed files with 18 additions and 19 deletions

View File

@ -56,6 +56,11 @@
-export([print_routes/1]).
-export([
foldl_routes/2,
foldr_routes/2
]).
-export([topics/0]).
%% gen_server callbacks
@ -212,6 +217,14 @@ cleanup_routes(Node) ->
Route <- mnesia:match_object(?ROUTE_TAB, Pat, write)
].
-spec foldl_routes(fun((emqx_types:route(), Acc) -> Acc), Acc) -> Acc.
foldl_routes(FoldFun, AccIn) ->
ets:foldl(FoldFun, AccIn, ?ROUTE_TAB).
-spec foldr_routes(fun((emqx_types:route(), Acc) -> Acc), Acc) -> Acc.
foldr_routes(FoldFun, AccIn) ->
ets:foldr(FoldFun, AccIn, ?ROUTE_TAB).
call(Router, Msg) ->
gen_server:call(Router, Msg, infinity).

View File

@ -22,9 +22,6 @@
-include_lib("emqx/include/emqx_mqtt.hrl").
-include_lib("emqx/include/logger.hrl").
-include("emqx_mgmt.hrl").
-define(PRINT_CMD(Cmd, Descr), io:format("~-48s# ~ts~n", [Cmd, Descr])).
-define(DATA_BACKUP_OPTS, #{print_fun => fun emqx_ctl:print/2}).
-export([load/0]).
@ -49,20 +46,6 @@
data/1
]).
-define(PROC_INFOKEYS, [
status,
memory,
message_queue_len,
total_heap_size,
heap_size,
stack_size,
reductions
]).
-define(MAX_LIMIT, 10000).
-define(APP, emqx).
-spec load() -> ok.
load() ->
Cmds = [Fun || {Fun, _} <- ?MODULE:module_info(exports), is_cmd(Fun)],
@ -197,9 +180,12 @@ if_client(ClientId, Fun) ->
%% @doc Topics Command
topics(["list"]) ->
dump(?ROUTE_TAB, emqx_topic);
emqx_router:foldr_routes(
fun(Route, Acc) -> [print({emqx_topic, Route}) | Acc] end,
[]
);
topics(["show", Topic]) ->
Routes = ets:lookup(?ROUTE_TAB, bin(Topic)),
Routes = emqx_router:lookup_routes(Topic),
[print({emqx_topic, Route}) || Route <- Routes];
topics(_) ->
emqx_ctl:usage([