Merge remote-tracking branch 'origin/develop'
This commit is contained in:
commit
af6ad8a90f
|
@ -694,7 +694,7 @@ zone.external.flapping_threshold = 10, 1m
|
||||||
## -s: second
|
## -s: second
|
||||||
##
|
##
|
||||||
## Default: 1h, 1 hour
|
## Default: 1h, 1 hour
|
||||||
zone.external.flapping_expiry_interval = 1h
|
zone.external.flapping_banned_expiry_interval = 1h
|
||||||
|
|
||||||
## All the topics will be prefixed with the mountpoint path if this option is enabled.
|
## All the topics will be prefixed with the mountpoint path if this option is enabled.
|
||||||
##
|
##
|
||||||
|
@ -789,7 +789,7 @@ zone.internal.flapping_threshold = 10, 1m
|
||||||
## -s: second
|
## -s: second
|
||||||
##
|
##
|
||||||
## Default: 1h, 1 hour
|
## Default: 1h, 1 hour
|
||||||
zone.internal.flapping_expiry_interval = 1h
|
zone.internal.flapping_banned_expiry_interval = 1h
|
||||||
|
|
||||||
## All the topics will be prefixed with the mountpoint path if this option is enabled.
|
## All the topics will be prefixed with the mountpoint path if this option is enabled.
|
||||||
##
|
##
|
||||||
|
|
|
@ -836,7 +836,7 @@ end}.
|
||||||
{datatype, string}
|
{datatype, string}
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
{mapping, "zone.$name.flapping_expiry_interval", "emqx.zones", [
|
{mapping, "zone.$name.flapping_banned_expiry_interval", "emqx.zones", [
|
||||||
{datatype, {duration, s}}
|
{datatype, {duration, s}}
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
|
|
|
@ -207,4 +207,3 @@ stats_fun() ->
|
||||||
undefined -> ok;
|
undefined -> ok;
|
||||||
Size -> emqx_stats:setstat('connections/count', 'connections/max', Size)
|
Size -> emqx_stats:setstat('connections/count', 'connections/max', Size)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
|
@ -50,27 +50,23 @@
|
||||||
%% the expiry time unit is minutes.
|
%% the expiry time unit is minutes.
|
||||||
-spec(init_flapping(ClientId :: binary(), Interval :: integer()) -> flapping_record()).
|
-spec(init_flapping(ClientId :: binary(), Interval :: integer()) -> flapping_record()).
|
||||||
init_flapping(ClientId, Interval) ->
|
init_flapping(ClientId, Interval) ->
|
||||||
#flapping{ client_id = ClientId
|
#flapping{client_id = ClientId,
|
||||||
, check_count = 1
|
check_count = 1,
|
||||||
, timestamp = emqx_time:now_secs() + Interval
|
timestamp = emqx_time:now_secs() + Interval}.
|
||||||
}.
|
|
||||||
|
|
||||||
%% @doc This function is used to initialize flapping records
|
%% @doc This function is used to initialize flapping records
|
||||||
%% the expiry time unit is minutes.
|
%% the expiry time unit is minutes.
|
||||||
-spec(check( Action :: atom()
|
-spec(check(Action :: atom(), ClientId :: binary(),
|
||||||
, ClientId :: binary()
|
Threshold :: {integer(), integer()}) -> flapping_state()).
|
||||||
, Threshold :: {integer(), integer()})
|
|
||||||
-> flapping_state()).
|
|
||||||
check(Action, ClientId, Threshold = {_TimesThreshold, TimeInterval}) ->
|
check(Action, ClientId, Threshold = {_TimesThreshold, TimeInterval}) ->
|
||||||
check(Action, ClientId, Threshold, init_flapping(ClientId, TimeInterval)).
|
check(Action, ClientId, Threshold, init_flapping(ClientId, TimeInterval)).
|
||||||
|
|
||||||
-spec(check( Action :: atom()
|
-spec(check(Action :: atom(), ClientId :: binary(),
|
||||||
, ClientId :: binary()
|
Threshold :: {integer(), integer()},
|
||||||
, Threshold :: {integer(), integer()}
|
InitFlapping :: flapping_record()) -> flapping_state()).
|
||||||
, InitFlapping :: flapping_record())
|
|
||||||
-> flapping_state()).
|
|
||||||
check(Action, ClientId, Threshold, InitFlapping) ->
|
check(Action, ClientId, Threshold, InitFlapping) ->
|
||||||
try ets:update_counter(?FLAPPING_TAB, ClientId, {_Pos = #flapping.check_count, 1}) of
|
case ets:update_counter(?FLAPPING_TAB, ClientId, {#flapping.check_count, 1}, InitFlapping) of
|
||||||
|
1 -> ok;
|
||||||
CheckCount ->
|
CheckCount ->
|
||||||
case ets:lookup(?FLAPPING_TAB, ClientId) of
|
case ets:lookup(?FLAPPING_TAB, ClientId) of
|
||||||
[Flapping] ->
|
[Flapping] ->
|
||||||
|
@ -78,23 +74,14 @@ check(Action, ClientId, Threshold, InitFlapping) ->
|
||||||
_Flapping ->
|
_Flapping ->
|
||||||
ok
|
ok
|
||||||
end
|
end
|
||||||
catch
|
|
||||||
error:badarg ->
|
|
||||||
ets:insert_new(?FLAPPING_TAB, InitFlapping),
|
|
||||||
ok
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec(check_flapping( Action :: atom()
|
check_flapping(Action, CheckCount, _Threshold = {TimesThreshold, TimeInterval},
|
||||||
, CheckTimes :: integer()
|
|
||||||
, Threshold :: {integer(), integer()}
|
|
||||||
, InitFlapping :: flapping_record())
|
|
||||||
-> flapping_state()).
|
|
||||||
check_flapping(Action, CheckTimes, _Threshold = {TimesThreshold, TimeInterval},
|
|
||||||
Flapping = #flapping{ client_id = ClientId
|
Flapping = #flapping{ client_id = ClientId
|
||||||
, timestamp = Timestamp }) ->
|
, timestamp = Timestamp }) ->
|
||||||
case emqx_time:now_secs() of
|
case emqx_time:now_secs() of
|
||||||
NowTimestamp when NowTimestamp =< Timestamp,
|
NowTimestamp when NowTimestamp =< Timestamp,
|
||||||
CheckTimes > TimesThreshold ->
|
CheckCount > TimesThreshold ->
|
||||||
ets:delete(?FLAPPING_TAB, ClientId),
|
ets:delete(?FLAPPING_TAB, ClientId),
|
||||||
flapping;
|
flapping;
|
||||||
NowTimestamp when NowTimestamp > Timestamp,
|
NowTimestamp when NowTimestamp > Timestamp,
|
||||||
|
@ -110,7 +97,7 @@ check_flapping(Action, CheckTimes, _Threshold = {TimesThreshold, TimeInterval},
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% gen_statem callbacks
|
%% gen_statem callbacks
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec(start_link(TimerInterval :: integer()) -> startlink_ret()).
|
-spec(start_link(TimerInterval :: [integer()]) -> startlink_ret()).
|
||||||
start_link(TimerInterval) ->
|
start_link(TimerInterval) ->
|
||||||
gen_statem:start_link({local, ?MODULE}, ?MODULE, [TimerInterval], []).
|
gen_statem:start_link({local, ?MODULE}, ?MODULE, [TimerInterval], []).
|
||||||
|
|
||||||
|
@ -145,17 +132,6 @@ terminate(_Reason, _StateName, _State) ->
|
||||||
|
|
||||||
%% @doc clean expired records in ets
|
%% @doc clean expired records in ets
|
||||||
clean_expired_records() ->
|
clean_expired_records() ->
|
||||||
Records = ets:tab2list(?FLAPPING_TAB),
|
NowTime = emqx_time:now_secs(),
|
||||||
traverse_records(Records).
|
MatchSpec = [{{'$1', '$2', '$3'},[{'<', '$3', NowTime}], [true]}],
|
||||||
|
ets:select_delete(?FLAPPING_TAB, MatchSpec).
|
||||||
traverse_records([]) ->
|
|
||||||
ok;
|
|
||||||
traverse_records([#flapping{client_id = ClientId,
|
|
||||||
timestamp = Timestamp} | LeftRecords]) ->
|
|
||||||
case emqx_time:now_secs() > Timestamp of
|
|
||||||
true ->
|
|
||||||
ets:delete(?FLAPPING_TAB, ClientId);
|
|
||||||
false ->
|
|
||||||
true
|
|
||||||
end,
|
|
||||||
traverse_records(LeftRecords).
|
|
||||||
|
|
|
@ -943,16 +943,15 @@ flag(true) -> 1.
|
||||||
do_flapping_detect(Action, #pstate{zone = Zone,
|
do_flapping_detect(Action, #pstate{zone = Zone,
|
||||||
client_id = ClientId,
|
client_id = ClientId,
|
||||||
enable_flapping_detect = true}) ->
|
enable_flapping_detect = true}) ->
|
||||||
ExpiryInterval = emqx_zone:get_env(Zone, flapping_expiry_interval, 3600000),
|
BanExpiryInterval = emqx_zone:get_env(Zone, flapping_ban_expiry_interval, 3600000),
|
||||||
Threshold = emqx_zone:get_env(Zone, flapping_threshold, 20),
|
Threshold = emqx_zone:get_env(Zone, flapping_threshold, 20),
|
||||||
Until = erlang:system_time(second) + ExpiryInterval,
|
Until = erlang:system_time(second) + BanExpiryInterval,
|
||||||
case emqx_flapping:check(Action, ClientId, Threshold) of
|
case emqx_flapping:check(Action, ClientId, Threshold) of
|
||||||
flapping ->
|
flapping ->
|
||||||
emqx_banned:add(#banned{who = {client_id, ClientId},
|
emqx_banned:add(#banned{who = {client_id, ClientId},
|
||||||
reason = <<"flapping">>,
|
reason = <<"flapping">>,
|
||||||
by = <<"flapping_checker">>,
|
by = <<"flapping_checker">>,
|
||||||
until = Until
|
until = Until}),
|
||||||
}),
|
|
||||||
ok;
|
ok;
|
||||||
_Other ->
|
_Other ->
|
||||||
ok
|
ok
|
||||||
|
|
Loading…
Reference in New Issue