Przeglądaj źródła

feat(olp): management API

William Yang 4 lat temu
rodzic
commit
67267acb70

+ 21 - 0
apps/emqx/src/emqx_olp.erl

@@ -14,12 +14,20 @@
 %% limitations under the License.
 %%--------------------------------------------------------------------
 -module(emqx_olp).
+
 -export([ is_overloaded/0
         , backoff/1
         , backoff_gc/1
         , backoff_hibernation/1
         ]).
 
+
+%% exports for O&M
+-export([ status/0
+        , on/0
+        , off/0
+        ]).
+
 -spec is_overloaded() -> boolean().
 is_overloaded() ->
   load_ctl:is_overloaded().
@@ -45,6 +53,19 @@ backoff_hibernation(Zone) ->
   load_ctl:is_overloaded()
     andalso emqx_config:get_zone_conf(Zone, [overload_protection, enable], false)
     andalso emqx_config:get_zone_conf(Zone, [overload_protection, backoff_hibernation], false).
+
+-spec status() -> any().
+status() ->
+  is_overloaded().
+
+-spec off() -> ok | {error, timeout}.
+off() ->
+  load_ctl:stop_runq_flagman(5000).
+
+-spec on() -> any().
+on() ->
+ load_ctl:restart_runq_flagman().
+
 %%%_* Emacs ====================================================================
 %%% Local Variables:
 %%% allout-layout: t

+ 22 - 0
apps/emqx_management/src/emqx_mgmt_cli.erl

@@ -38,6 +38,7 @@
         , trace/1
         , log/1
         , authz/1
+        , olp/1
         ]).
 
 -define(PROC_INFOKEYS, [status,
@@ -495,6 +496,27 @@ authz(_) ->
                     {"authz cache-clean <ClientId>",      "Clears authorization cache for given client"}
                    ]).
 
+
+%%--------------------------------------------------------------------
+%% @doc OLP (Overload Protection related)
+olp(["status"]) ->
+    S = case emqx_olp:is_overloaded() of
+            true -> "overloaded";
+            false -> "not overloaded"
+        end,
+    emqx_ctl:print("~p is ~s ~n", [node(), S]);
+olp(["off"]) ->
+    Res = emqx_olp:off(),
+    emqx_ctl:print("Turn off overload protetion ~p : ~p ~n", [node(), Res]);
+olp(["on"]) ->
+    Res = emqx_olp:on(),
+    emqx_ctl:print("Turn on overload protection ~p : ~p ~n", [node(), Res]);
+olp(_) ->
+    emqx_ctl:usage([{"olp status", "Return OLP status if system is overloaded"},
+                    {"olp on",     "Turn on overload protection"},
+                    {"olp off",    "Turn off overload protection"}
+                   ]).
+
 %%--------------------------------------------------------------------
 %% Dump ETS
 %%--------------------------------------------------------------------