Delete expired client

This commit is contained in:
zhouzb 2020-04-22 15:08:14 +08:00
parent 545ba905dc
commit 09197d3775
2 changed files with 25 additions and 2 deletions

View File

@ -109,7 +109,7 @@ init([]) ->
{read_concurrency, true},
{write_concurrency, true}
]),
{ok, #{}, hibernate}.
{ok, ensure_timer(#{}), hibernate}.
handle_call(Req, _From, State) ->
?LOG(error, "Unexpected call: ~p", [Req]),
@ -142,6 +142,12 @@ handle_cast(Msg, State) ->
?LOG(error, "Unexpected cast: ~p", [Msg]),
{noreply, State}.
handle_info({timeout, TRef, expired_detecting}, State = #{expired_timer := TRef}) ->
Timestamp = erlang:system_time(millisecond) - maps:get(duration, get_policy()),
MatchSpec = [{{'_', '_', '_', '$1', '_'},[{'<', '$1', Timestamp}], [true]}],
ets:select_delete(?FLAPPING_TAB, MatchSpec),
{noreply, ensure_timer(State), hibernate};
handle_info(Info, State) ->
?LOG(error, "Unexpected info: ~p", [Info]),
{noreply, State}.
@ -151,3 +157,8 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
ensure_timer(State) ->
Timeout = maps:get(duration, get_policy()),
TRef = emqx_misc:start_timer(Timeout, expired_detecting),
State#{expired_timer => TRef}.

View File

@ -19,6 +19,8 @@
-compile(export_all).
-compile(nowarn_export_all).
-include_lib("eunit/include/eunit.hrl").
all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) ->
@ -50,7 +52,7 @@ t_detect_check(_) ->
false = emqx_flapping:detect(ClientInfo),
false = emqx_banned:check(ClientInfo),
true = emqx_flapping:detect(ClientInfo),
timer:sleep(100),
timer:sleep(50),
true = emqx_banned:check(ClientInfo),
timer:sleep(3000),
false = emqx_banned:check(ClientInfo),
@ -61,3 +63,13 @@ t_detect_check(_) ->
Pid ! test,
ok = emqx_flapping:stop().
t_expired_detecting(_) ->
ClientInfo = #{zone => external,
clientid => <<"clientid">>,
peerhost => {127,0,0,1}},
false = emqx_flapping:detect(ClientInfo),
?assertEqual(true, lists:any(fun({flapping, <<"clientid">>, _, _, _}) -> true;
(_) -> false end, ets:tab2list(emqx_flapping))),
timer:sleep(200),
?assertEqual(true, lists:all(fun({flapping, <<"clientid">>, _, _, _}) -> false;
(_) -> true end, ets:tab2list(emqx_flapping))).