From 3bc92e5845b9fb550b39c6714a94b17f49adbcc8 Mon Sep 17 00:00:00 2001 From: DDDHuang <44492639+DDDHuang@users.noreply.github.com> Date: Thu, 2 Sep 2021 17:16:13 +0800 Subject: [PATCH] fix: mgmt listener cli (#5632) * fix: mgmt cli linteners --- apps/emqx/src/emqx_listeners.erl | 24 +++++++++++++ apps/emqx_management/src/emqx_mgmt_cli.erl | 40 ++++++++++++---------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/apps/emqx/src/emqx_listeners.erl b/apps/emqx/src/emqx_listeners.erl index 399bd3d08..8f0141b3a 100644 --- a/apps/emqx/src/emqx_listeners.erl +++ b/apps/emqx/src/emqx_listeners.erl @@ -26,6 +26,8 @@ , restart/0 , stop/0 , is_running/1 + , current_conns/2 + , max_conns/2 ]). -export([ start_listener/1 @@ -89,6 +91,28 @@ is_running(quic, _ListenerId, _Conf)-> %% TODO: quic support {error, no_found}. +current_conns(ID, ListenOn) -> + {Type, Name} = parse_listener_id(ID), + current_conns(Type, Name, ListenOn). + +current_conns(Type, Name, ListenOn) when Type == tcl; Type == ssl -> + esockd:get_current_connections({listener_id(Type, Name), ListenOn}); +current_conns(Type, Name, _ListenOn) when Type =:= ws; Type =:= wss -> + proplists:get_value(all_connections, ranch:info(listener_id(Type, Name))); +current_conns(_, _, _) -> + {error, not_support}. + +max_conns(ID, ListenOn) -> + {Type, Name} = parse_listener_id(ID), + max_conns(Type, Name, ListenOn). + +max_conns(Type, Name, ListenOn) when Type == tcl; Type == ssl -> + esockd:get_max_connections({listener_id(Type, Name), ListenOn}); +max_conns(Type, Name, _ListenOn) when Type =:= ws; Type =:= wss -> + proplists:get_value(max_connections, ranch:info(listener_id(Type, Name))); +max_conns(_, _, _) -> + {error, not_support}. + %% @doc Start all listeners. -spec(start() -> ok). start() -> diff --git a/apps/emqx_management/src/emqx_mgmt_cli.erl b/apps/emqx_management/src/emqx_mgmt_cli.erl index 3d4dea31e..c7fb8a7b7 100644 --- a/apps/emqx_management/src/emqx_mgmt_cli.erl +++ b/apps/emqx_management/src/emqx_mgmt_cli.erl @@ -412,26 +412,28 @@ trace_off(Who, Name) -> %% @doc Listeners Command listeners([]) -> - lists:foreach(fun({{Protocol, ListenOn}, _Pid}) -> - Info = [{listen_on, {string, format_listen_on(ListenOn)}}, - {acceptors, esockd:get_acceptors({Protocol, ListenOn})}, - {max_conns, esockd:get_max_connections({Protocol, ListenOn})}, - {current_conn, esockd:get_current_connections({Protocol, ListenOn})}, - {shutdown_count, esockd:get_shutdown_count({Protocol, ListenOn})} - ], - emqx_ctl:print("~s~n", [Protocol]), + lists:foreach(fun({ID, Conf}) -> + {Host, Port} = maps:get(bind, Conf), + Acceptors = maps:get(acceptors, Conf), + ProxyProtocol = maps:get(proxy_protocol, Conf, undefined), + Running = maps:get(running, Conf), + CurrentConns = case emqx_listeners:current_conns(ID, {Host, Port}) of + {error, _} -> []; + CC -> [{current_conn, CC}] + end, + MaxConn = case emqx_listeners:max_conns(ID, {Host, Port}) of + {error, _} -> []; + MC -> [{max_conns, MC}] + end, + Info = [ + {listen_on, {string, format_listen_on(Port)}}, + {acceptors, Acceptors}, + {proxy_protocol, ProxyProtocol}, + {running, Running} + ] ++ CurrentConns ++ MaxConn, + emqx_ctl:print("~s~n", [ID]), lists:foreach(fun indent_print/1, Info) - end, esockd:listeners()), - lists:foreach(fun({Protocol, Opts}) -> - Port = proplists:get_value(port, Opts), - Info = [{listen_on, {string, format_listen_on(Port)}}, - {acceptors, maps:get(num_acceptors, proplists:get_value(transport_options, Opts, #{}), 0)}, - {max_conns, proplists:get_value(max_connections, Opts)}, - {current_conn, proplists:get_value(all_connections, Opts)}, - {shutdown_count, []}], - emqx_ctl:print("~s~n", [Protocol]), - lists:foreach(fun indent_print/1, Info) - end, ranch:info()); + end, emqx_listeners:list()); listeners(["stop", ListenerId]) -> case emqx_listeners:stop_listener(list_to_atom(ListenerId)) of