From f6b2c9a69fced22851d5d39de546dd9fd2da430c Mon Sep 17 00:00:00 2001 From: turtleDeng Date: Thu, 16 Jan 2020 23:11:19 +0800 Subject: [PATCH] Correct timestamp for banned (#3188) --- priv/emqx.schema | 8 ++++---- src/emqx_banned.erl | 2 +- src/emqx_flapping.erl | 2 +- test/emqx_flapping_SUITE.erl | 4 ++-- test/mqtt_protocol_v5_SUITE.erl | 17 +++++++++++------ 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/priv/emqx.schema b/priv/emqx.schema index 527e36a7a..2763078f7 100644 --- a/priv/emqx.schema +++ b/priv/emqx.schema @@ -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}. diff --git a/src/emqx_banned.erl b/src/emqx_banned.erl index 1ca0ecf8f..80f93be70 100644 --- a/src/emqx_banned.erl +++ b/src/emqx_banned.erl @@ -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). diff --git a/src/emqx_flapping.erl b/src/emqx_flapping.erl index c1faa7e95..2256ef015 100644 --- a/src/emqx_flapping.erl +++ b/src/emqx_flapping.erl @@ -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">>, diff --git a/test/emqx_flapping_SUITE.erl b/test/emqx_flapping_SUITE.erl index 4d9fd5907..223fd5d7e 100644 --- a/test/emqx_flapping_SUITE.erl +++ b/test/emqx_flapping_SUITE.erl @@ -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), diff --git a/test/mqtt_protocol_v5_SUITE.erl b/test/mqtt_protocol_v5_SUITE.erl index 0f5fe3f07..01d4391f0 100644 --- a/test/mqtt_protocol_v5_SUITE.erl +++ b/test/mqtt_protocol_v5_SUITE.erl @@ -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)).