Correct timestamp for banned (#3188)

This commit is contained in:
turtleDeng 2020-01-16 23:11:19 +08:00 committed by JianBo He
parent 9a76164e65
commit f6b2c9a69f
5 changed files with 19 additions and 14 deletions

View File

@ -640,15 +640,15 @@ end}.
{translation, "emqx.flapping_detect_policy", fun(Conf) ->
Policy = cuttlefish:conf_get("flapping_detect_policy", Conf),
[Threshold, Duration, Interval] = string:tokens(Policy, ", "),
ParseDuration = fun(S) ->
case cuttlefish_duration:parse(S, ms) of
ParseDuration = fun(S, Dur) ->
case cuttlefish_duration:parse(S, Dur) of
I when is_integer(I) -> I;
{error, Reason} -> error(Reason)
end
end,
#{threshold => list_to_integer(Threshold),
duration => ParseDuration(Duration),
banned_interval => ParseDuration(Interval)
duration => ParseDuration(Duration, ms),
banned_interval => ParseDuration(Interval, s)
}
end}.

View File

@ -85,7 +85,7 @@ do_check(Who) when is_tuple(Who) ->
case mnesia:dirty_read(?BANNED_TAB, Who) of
[] -> false;
[#banned{until = Until}] ->
Until > erlang:system_time(millisecond)
Until > erlang:system_time(second)
end.
-spec(create(emqx_types:banned()) -> ok).

View File

@ -124,7 +124,7 @@ handle_cast({detected, #flapping{clientid = ClientId,
true -> %% Flapping happened:(
?LOG(error, "Flapping detected: ~s(~s) disconnected ~w times in ~wms",
[ClientId, inet:ntoa(PeerHost), DetectCnt, Duration]),
Now = erlang:system_time(millisecond),
Now = erlang:system_time(second),
Banned = #banned{who = {clientid, ClientId},
by = <<"flapping detector">>,
reason = <<"flapping is detected">>,

View File

@ -31,7 +31,7 @@ set_special_configs(emqx) ->
application:set_env(emqx, flapping_detect_policy,
#{threshold => 3,
duration => 100,
banned_interval => 200
banned_interval => 2
});
set_special_configs(_App) -> ok.
@ -52,7 +52,7 @@ t_detect_check(_) ->
true = emqx_flapping:detect(ClientInfo),
timer:sleep(100),
true = emqx_banned:check(ClientInfo),
timer:sleep(200),
timer:sleep(3000),
false = emqx_banned:check(ClientInfo),
Childrens = supervisor:which_children(emqx_cm_sup),
{flapping, Pid, _, _} = lists:keyfind(flapping, 1, Childrens),

View File

@ -147,8 +147,8 @@ t_connect_keepalive_timeout(_) ->
Msg ->
ReasonCode = 141,
?assertMatch({disconnected, ReasonCode, _Channel}, Msg)
after
round(timer:seconds(Keepalive) * 2 * 1.5 ) -> error("keepalive timeout")
after round(timer:seconds(Keepalive) * 2 * 1.5 ) ->
error("keepalive timeout")
end.
%%--------------------------------------------------------------------
@ -160,7 +160,7 @@ t_shared_subscriptions_client_terminates_when_qos_eq_2(_) ->
application:set_env(emqx, shared_dispatch_ack_enabled, true),
Topic = nth(1, ?TOPICS),
Shared_topic = list_to_binary("$share/sharename/" ++ binary_to_list(<<"TopicA">>)),
SharedTopic = list_to_binary("$share/sharename/" ++ binary_to_list(<<"TopicA">>)),
CRef = counters:new(1, [atomics]),
meck:expect(emqtt, connected,
@ -174,18 +174,23 @@ t_shared_subscriptions_client_terminates_when_qos_eq_2(_) ->
{clientid, <<"sub_client_1">>},
{keepalive, 5}]),
{ok, _} = emqtt:connect(Sub1),
{ok, _, [2]} = emqtt:subscribe(Sub1, Shared_topic, qos2),
{ok, _, [2]} = emqtt:subscribe(Sub1, SharedTopic, qos2),
{ok, Sub2} = emqtt:start_link([{proto_ver, v5},
{clientid, <<"sub_client_2">>},
{keepalive, 5}]),
{ok, _} = emqtt:connect(Sub2),
{ok, _, [2]} = emqtt:subscribe(Sub2, Shared_topic, qos2),
{ok, _, [2]} = emqtt:subscribe(Sub2, SharedTopic, qos2),
{ok, Pub} = emqtt:start_link([{proto_ver, v5}, {clientid, <<"pub_client">>}]),
{ok, _} = emqtt:connect(Pub),
{ok, _} = emqtt:publish(Pub, Topic, <<"t_shared_subscriptions_client_terminates_when_qos_eq_2">>, 2),
receive
{disconnected,shutdown,for_testiong} -> ok
{'EXIT', _,{shutdown, for_testiong}} ->
ok
after 1000 ->
error("disconnected timeout")
end,
?assertEqual(1, counters:get(CRef, 1)).