Bläddra i källkod

feat: always update default zones

zhongwencool 2 år sedan
förälder
incheckning
d58506a7c3

+ 3 - 2
apps/emqx/src/emqx_config.erl

@@ -831,13 +831,14 @@ merge_with_global_defaults(GlobalDefaults, ZoneVal) ->
     NewZoneVal :: map().
 maybe_update_zone([zones | T], ZonesValue, Value) ->
     %% note, do not write to PT, return *New value* instead
-    NewZonesValue = emqx_utils_maps:deep_put(T, ZonesValue, Value),
     GLD = zone_global_defaults(),
+    NewZonesValue0 = emqx_utils_maps:deep_put(T, ZonesValue, Value),
+    NewZonesValue1 = emqx_utils_maps:deep_merge(#{default => GLD}, NewZonesValue0),
     maps:map(
         fun(_ZoneName, ZoneValue) ->
             merge_with_global_defaults(GLD, ZoneValue)
         end,
-        NewZonesValue
+        NewZonesValue1
     );
 maybe_update_zone([RootName | T], RootValue, Value) when is_atom(RootName) ->
     NewRootValue = emqx_utils_maps:deep_put(T, RootValue, Value),

+ 1 - 1
apps/emqx/test/emqx_config_SUITE.erl

@@ -344,7 +344,7 @@ zone_global_defaults() ->
         conn_congestion =>
             #{enable_alarm => true, min_alarm_sustain_duration => 60000},
         flapping_detect =>
-            #{ban_time => 300000, max_count => 15, window_time => disabled},
+            #{ban_time => 300000, max_count => 15, window_time => 60000, enable => false},
         force_gc =>
             #{bytes => 16777216, count => 16000, enable => true},
         force_shutdown =>

+ 24 - 8
apps/emqx/test/emqx_flapping_SUITE.erl

@@ -26,16 +26,16 @@ all() -> emqx_common_test_helpers:all(?MODULE).
 init_per_suite(Config) ->
     emqx_common_test_helpers:boot_modules(all),
     emqx_common_test_helpers:start_apps([]),
-    emqx_config:put_zone_conf(
-        default,
+    %% update global default config
+    {ok, _} = emqx:update_config(
         [flapping_detect],
         #{
-            enable => true,
-            max_count => 3,
+            <<"enable">> => true,
+            <<"max_count">> => 3,
             % 0.1s
-            window_time => 100,
+            <<"window_time">> => 100,
             %% 2s
-            ban_time => 2000
+            <<"ban_time">> => "2s"
         }
     ),
     Config.
@@ -53,6 +53,7 @@ t_detect_check(_) ->
         clientid => <<"client007">>,
         peerhost => {127, 0, 0, 1}
     },
+    ct:pal("www:~p~n", [emqx_flapping:get_policy(default)]),
     false = emqx_flapping:detect(ClientInfo),
     false = emqx_banned:check(ClientInfo),
     false = emqx_flapping:detect(ClientInfo),
@@ -115,8 +116,23 @@ t_conf_update(_) ->
     emqx_config:put_zone_conf(new_zone, [flapping_detect], #{}),
     ?assertEqual(Global, get_policy(new_zone)),
 
-    emqx_config:put_zone_conf(new_zone_1, [flapping_detect], #{window_time => 100}),
-    ?assertEqual(Global#{window_time := 100}, emqx_flapping:get_policy(new_zone_1)),
+    emqx_config:put_zone_conf(zone_1, [flapping_detect], #{window_time => 100}),
+    ?assertEqual(Global#{window_time := 100}, emqx_flapping:get_policy(zone_1)),
+
+    Zones = #{
+        <<"zone_1">> => #{<<"flapping_detect">> => #{<<"window_time">> => 123}},
+        <<"zone_2">> => #{<<"flapping_detect">> => #{<<"window_time">> => 456}}
+    },
+    ?assertMatch({ok, _}, emqx:update_config([zones], Zones)),
+    %% new_zone is already deleted
+    ?assertError({config_not_found, _}, get_policy(new_zone)),
+    %% update zone(zone_1) has default.
+    ?assertEqual(Global#{window_time := 123}, emqx_flapping:get_policy(zone_1)),
+    %% create zone(zone_2) has default
+    ?assertEqual(Global#{window_time := 456}, emqx_flapping:get_policy(zone_2)),
+    %% reset to default(empty) andalso get default from global
+    ?assertMatch({ok, _}, emqx:update_config([zones], #{})),
+    ?assertEqual(Global, emqx:get_config([zones, default, flapping_detect])),
     ok.
 
 get_policy(Zone) ->