From 09197d37752a586c48988d96c83ff87f65117cf4 Mon Sep 17 00:00:00 2001 From: zhouzb Date: Wed, 22 Apr 2020 15:08:14 +0800 Subject: [PATCH] Delete expired client --- src/emqx_flapping.erl | 13 ++++++++++++- test/emqx_flapping_SUITE.erl | 14 +++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/emqx_flapping.erl b/src/emqx_flapping.erl index 2256ef015..36323d90f 100644 --- a/src/emqx_flapping.erl +++ b/src/emqx_flapping.erl @@ -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}. \ No newline at end of file diff --git a/test/emqx_flapping_SUITE.erl b/test/emqx_flapping_SUITE.erl index 223fd5d7e..dcd552385 100644 --- a/test/emqx_flapping_SUITE.erl +++ b/test/emqx_flapping_SUITE.erl @@ -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))). \ No newline at end of file