feat(olp): add config and backoff gc

This commit is contained in:
William Yang 2021-10-04 12:00:32 +02:00
parent 166f02edc4
commit 9304e3c122
4 changed files with 51 additions and 8 deletions

View File

@ -1144,8 +1144,7 @@ run_terminate_hook(Reason, #channel{clientinfo = ClientInfo, session = Session})
%% Internal functions %% Internal functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
overload_protection(_, #channel{clientinfo = #{zone := Zone}}) -> overload_protection(_, #channel{clientinfo = #{zone := Zone}}) ->
T = get_mqtt_conf(Zone, overload_drawback_delay, 1), emqx_olp:backoff(Zone),
emqx_olp:backoff(T),
ok. ok.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------

View File

@ -821,8 +821,10 @@ ensure_rate_limit(Stats, State = #state{limiter = Limiter}) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Run GC and Check OOM %% Run GC and Check OOM
run_gc(Stats, State = #state{gc_state = GcSt}) -> run_gc(Stats, State = #state{gc_state = GcSt, zone = Zone}) ->
case ?ENABLED(GcSt) andalso emqx_gc:run(Stats, GcSt) of case ?ENABLED(GcSt) andalso not emqx_olp:backoff_gc(Zone)
andalso emqx_gc:run(Stats, GcSt)
of
false -> State; false -> State;
{_IsGC, GcSt1} -> {_IsGC, GcSt1} ->
State#state{gc_state = GcSt1} State#state{gc_state = GcSt1}

View File

@ -16,16 +16,35 @@
-module(emqx_olp). -module(emqx_olp).
-export([ is_overloaded/0 -export([ is_overloaded/0
, backoff/1 , backoff/1
, backoff_gc/1
, backoff_hibernation/1
]). ]).
-spec is_overloaded() -> boolean(). -spec is_overloaded() -> boolean().
is_overloaded() -> is_overloaded() ->
load_ctl:is_overloaded(). load_ctl:is_overloaded().
-spec backoff(timer:timeout()) -> ok | timeout. -spec backoff(Zone :: atom()) -> ok | timeout.
backoff(Delay) -> backoff(Zone) ->
load_ctl:maydelay(Delay). case emqx_config:get_zone_conf(Zone, [overload_protection, enable], false) of
true ->
Delay = emqx_config:get_zone_conf(Zone, [overload_protection, backoff_delay], 1),
load_ctl:maydelay(Delay);
false ->
ok
end.
-spec backoff_gc(Zone :: atom()) -> ok | timeout.
backoff_gc(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_gc], false).
-spec backoff_hibernation(Zone :: atom()) -> ok | timeout.
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).
%%%_* Emacs ==================================================================== %%%_* Emacs ====================================================================
%%% Local Variables: %%% Local Variables:
%%% allout-layout: t %%% allout-layout: t

View File

@ -122,6 +122,9 @@ roots(medium) ->
, {"force_shutdown", , {"force_shutdown",
sc(ref("force_shutdown"), sc(ref("force_shutdown"),
#{})} #{})}
, {"overload_protection",
sc(ref("overload_protection"),
#{})}
]; ];
roots(low) -> roots(low) ->
[ {"force_gc", [ {"force_gc",
@ -323,7 +326,9 @@ fields("mqtt") ->
fields("zone") -> fields("zone") ->
Fields = ["mqtt", "stats", "flapping_detect", "force_shutdown", Fields = ["mqtt", "stats", "flapping_detect", "force_shutdown",
"conn_congestion", "rate_limit", "quota", "force_gc"], "conn_congestion", "rate_limit", "quota", "force_gc",
"overload_protection"
],
[{F, ref(emqx_zone_schema, F)} || F <- Fields]; [{F, ref(emqx_zone_schema, F)} || F <- Fields];
fields("rate_limit") -> fields("rate_limit") ->
@ -391,6 +396,24 @@ fields("force_shutdown") ->
})} })}
]; ];
fields("overload_protection") ->
[ {"enable",
sc(boolean(),
#{ default => false})}
, {"backoff_delay",
sc(range(0, inf),
#{ default => 1
})}
, {"backoff_gc",
sc(boolean(),
#{ default => true
})}
, {"backoff_hibernation",
sc(boolean(),
#{ default => true
})}
];
fields("conn_congestion") -> fields("conn_congestion") ->
[ {"enable_alarm", [ {"enable_alarm",
sc(boolean(), sc(boolean(),