Просмотр исходного кода

fix: mgmt listener cli (#5632)

* fix: mgmt cli linteners
DDDHuang 4 лет назад
Родитель
Сommit
3bc92e5845
2 измененных файлов с 45 добавлено и 19 удалено
  1. 24 0
      apps/emqx/src/emqx_listeners.erl
  2. 21 19
      apps/emqx_management/src/emqx_mgmt_cli.erl

+ 24 - 0
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() ->

+ 21 - 19
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