Delete expired client
This commit is contained in:
parent
545ba905dc
commit
09197d3775
|
@ -109,7 +109,7 @@ init([]) ->
|
||||||
{read_concurrency, true},
|
{read_concurrency, true},
|
||||||
{write_concurrency, true}
|
{write_concurrency, true}
|
||||||
]),
|
]),
|
||||||
{ok, #{}, hibernate}.
|
{ok, ensure_timer(#{}), hibernate}.
|
||||||
|
|
||||||
handle_call(Req, _From, State) ->
|
handle_call(Req, _From, State) ->
|
||||||
?LOG(error, "Unexpected call: ~p", [Req]),
|
?LOG(error, "Unexpected call: ~p", [Req]),
|
||||||
|
@ -142,6 +142,12 @@ handle_cast(Msg, State) ->
|
||||||
?LOG(error, "Unexpected cast: ~p", [Msg]),
|
?LOG(error, "Unexpected cast: ~p", [Msg]),
|
||||||
{noreply, State}.
|
{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) ->
|
handle_info(Info, State) ->
|
||||||
?LOG(error, "Unexpected info: ~p", [Info]),
|
?LOG(error, "Unexpected info: ~p", [Info]),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
@ -151,3 +157,8 @@ terminate(_Reason, _State) ->
|
||||||
|
|
||||||
code_change(_OldVsn, State, _Extra) ->
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
|
ensure_timer(State) ->
|
||||||
|
Timeout = maps:get(duration, get_policy()),
|
||||||
|
TRef = emqx_misc:start_timer(Timeout, expired_detecting),
|
||||||
|
State#{expired_timer => TRef}.
|
|
@ -19,6 +19,8 @@
|
||||||
-compile(export_all).
|
-compile(export_all).
|
||||||
-compile(nowarn_export_all).
|
-compile(nowarn_export_all).
|
||||||
|
|
||||||
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
all() -> emqx_ct:all(?MODULE).
|
all() -> emqx_ct:all(?MODULE).
|
||||||
|
|
||||||
init_per_suite(Config) ->
|
init_per_suite(Config) ->
|
||||||
|
@ -50,7 +52,7 @@ t_detect_check(_) ->
|
||||||
false = emqx_flapping:detect(ClientInfo),
|
false = emqx_flapping:detect(ClientInfo),
|
||||||
false = emqx_banned:check(ClientInfo),
|
false = emqx_banned:check(ClientInfo),
|
||||||
true = emqx_flapping:detect(ClientInfo),
|
true = emqx_flapping:detect(ClientInfo),
|
||||||
timer:sleep(100),
|
timer:sleep(50),
|
||||||
true = emqx_banned:check(ClientInfo),
|
true = emqx_banned:check(ClientInfo),
|
||||||
timer:sleep(3000),
|
timer:sleep(3000),
|
||||||
false = emqx_banned:check(ClientInfo),
|
false = emqx_banned:check(ClientInfo),
|
||||||
|
@ -61,3 +63,13 @@ t_detect_check(_) ->
|
||||||
Pid ! test,
|
Pid ! test,
|
||||||
ok = emqx_flapping:stop().
|
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))).
|
Loading…
Reference in New Issue