Merge pull request #1797 from terry-xiaoyu/banned_api_v2
Update for banned API
This commit is contained in:
commit
ba176f2073
|
@ -148,13 +148,16 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Banned
|
%% Banned
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
-type(banned_who() :: {client_id, binary()}
|
||||||
|
| {username, binary()}
|
||||||
|
| {ip_address, inet:ip_address()}).
|
||||||
|
|
||||||
-record(banned, {
|
-record(banned, {
|
||||||
key,
|
who :: banned_who(),
|
||||||
reason,
|
reason :: binary(),
|
||||||
by,
|
by :: binary(),
|
||||||
desc,
|
desc :: binary(),
|
||||||
until}).
|
until :: integer()
|
||||||
|
}).
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,7 @@ handle_cast(Msg, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
handle_info({timeout, TRef, expire}, State = #{expiry_timer := TRef}) ->
|
handle_info({timeout, TRef, expire}, State = #{expiry_timer := TRef}) ->
|
||||||
mnesia:async_dirty(fun expire_banned_items/1, [erlang:timestamp()]),
|
mnesia:async_dirty(fun expire_banned_items/1, [erlang:system_time(second)]),
|
||||||
{noreply, ensure_expiry_timer(State), hibernate};
|
{noreply, ensure_expiry_timer(State), hibernate};
|
||||||
|
|
||||||
handle_info(Info, State) ->
|
handle_info(Info, State) ->
|
||||||
|
@ -106,17 +106,8 @@ ensure_expiry_timer(State) ->
|
||||||
State#{expiry_timer := emqx_misc:start_timer(timer:minutes(5), expire)}.
|
State#{expiry_timer := emqx_misc:start_timer(timer:minutes(5), expire)}.
|
||||||
|
|
||||||
expire_banned_items(Now) ->
|
expire_banned_items(Now) ->
|
||||||
expire_banned_item(mnesia:first(?TAB), Now).
|
mnesia:foldl(fun
|
||||||
|
(B = #banned{until = Until}, _Acc) when Until < Now ->
|
||||||
expire_banned_item('$end_of_table', _Now) ->
|
mnesia:delete_object(?TAB, B, sticky_write);
|
||||||
ok;
|
(_, _Acc) -> ok
|
||||||
expire_banned_item(Key, Now) ->
|
end, ok, ?TAB).
|
||||||
case mnesia:read(?TAB, Key) of
|
|
||||||
[#banned{until = undefined}] ->
|
|
||||||
ok;
|
|
||||||
[B = #banned{until = Until}] when Until < Now ->
|
|
||||||
mnesia:delete_object(?TAB, B, sticky_write);
|
|
||||||
_ -> ok
|
|
||||||
end,
|
|
||||||
expire_banned_item(mnesia:next(?TAB, Key), Now).
|
|
||||||
|
|
||||||
|
|
|
@ -28,12 +28,12 @@ all() -> [t_banned_all].
|
||||||
t_banned_all(_) ->
|
t_banned_all(_) ->
|
||||||
emqx_ct_broker_helpers:run_setup_steps(),
|
emqx_ct_broker_helpers:run_setup_steps(),
|
||||||
emqx_banned:start_link(),
|
emqx_banned:start_link(),
|
||||||
{MegaSecs, Secs, MicroSecs} = erlang:timestamp(),
|
TimeNow = erlang:system_time(second),
|
||||||
ok = emqx_banned:add(#banned{key = {client_id, <<"TestClient">>},
|
ok = emqx_banned:add(#banned{who = {client_id, <<"TestClient">>},
|
||||||
reason = <<"test">>,
|
reason = <<"test">>,
|
||||||
by = <<"banned suite">>,
|
by = <<"banned suite">>,
|
||||||
desc = <<"test">>,
|
desc = <<"test">>,
|
||||||
until = {MegaSecs, Secs + 10, MicroSecs}}),
|
until = TimeNow + 10}),
|
||||||
% here is not expire banned test because its check interval is greater than 5 mins, but its effect has been confirmed
|
% here is not expire banned test because its check interval is greater than 5 mins, but its effect has been confirmed
|
||||||
timer:sleep(100),
|
timer:sleep(100),
|
||||||
?assert(emqx_banned:check(#{client_id => <<"TestClient">>, username => undefined, peername => {undefined, undefined}})),
|
?assert(emqx_banned:check(#{client_id => <<"TestClient">>, username => undefined, peername => {undefined, undefined}})),
|
||||||
|
|
Loading…
Reference in New Issue