Przeglądaj źródła

fix: limit gateway connections with license

firest 1 rok temu
rodzic
commit
b5d507bad8

+ 19 - 1
apps/emqx_gateway/src/emqx_gateway_cm_registry.erl

@@ -28,7 +28,7 @@
     unregister_channel/2
 ]).
 
--export([lookup_channels/2]).
+-export([lookup_channels/2, get_connected_client_count/0]).
 
 -export([tabname/1]).
 
@@ -95,6 +95,24 @@ lookup_channels(Name, ClientId) ->
 record(ClientId, ChanPid) ->
     #channel{chid = ClientId, pid = ChanPid}.
 
+get_connected_client_count() ->
+    Gatewyas = emqx_gateway_utils:find_gateway_definitions(),
+    Fun = fun(#{name := Name}, Acc) ->
+        Tab = tabname(Name),
+        case ets:whereis(Tab) of
+            undefined ->
+                Acc;
+            _ ->
+                case ets:info(Tab, size) of
+                    undefined ->
+                        Acc;
+                    Size ->
+                        Acc + Size
+                end
+        end
+    end,
+    lists:foldl(Fun, 0, Gatewyas).
+
 %%--------------------------------------------------------------------
 %% gen_server callbacks
 %%--------------------------------------------------------------------

+ 1 - 1
apps/emqx_license/src/emqx_license.app.src

@@ -3,6 +3,6 @@
     {vsn, "5.1.0"},
     {modules, []},
     {registered, [emqx_license_sup]},
-    {applications, [kernel, stdlib, emqx_ctl]},
+    {applications, [kernel, stdlib, emqx_ctl, emqx_gateway]},
     {mod, {emqx_license_app, []}}
 ]}.

+ 2 - 1
apps/emqx_license/src/emqx_license_resources.erl

@@ -56,7 +56,8 @@ local_connection_count() ->
 
 -spec connection_count() -> non_neg_integer().
 connection_count() ->
-    local_connection_count() + cached_remote_connection_count().
+    local_connection_count() + cached_remote_connection_count() +
+        emqx_gateway_cm_registry:get_connected_client_count().
 
 %%------------------------------------------------------------------------------
 %% gen_server callbacks