feat: always update default zones

This commit is contained in:
zhongwencool 2023-06-06 22:25:54 +08:00
parent 48381d4c86
commit d58506a7c3
3 changed files with 28 additions and 11 deletions

View File

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

View File

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

View File

@ -26,16 +26,16 @@ all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), emqx_common_test_helpers:boot_modules(all),
emqx_common_test_helpers:start_apps([]), emqx_common_test_helpers:start_apps([]),
emqx_config:put_zone_conf( %% update global default config
default, {ok, _} = emqx:update_config(
[flapping_detect], [flapping_detect],
#{ #{
enable => true, <<"enable">> => true,
max_count => 3, <<"max_count">> => 3,
% 0.1s % 0.1s
window_time => 100, <<"window_time">> => 100,
%% 2s %% 2s
ban_time => 2000 <<"ban_time">> => "2s"
} }
), ),
Config. Config.
@ -53,6 +53,7 @@ t_detect_check(_) ->
clientid => <<"client007">>, clientid => <<"client007">>,
peerhost => {127, 0, 0, 1} peerhost => {127, 0, 0, 1}
}, },
ct:pal("www:~p~n", [emqx_flapping:get_policy(default)]),
false = emqx_flapping:detect(ClientInfo), false = emqx_flapping:detect(ClientInfo),
false = emqx_banned:check(ClientInfo), false = emqx_banned:check(ClientInfo),
false = emqx_flapping:detect(ClientInfo), false = emqx_flapping:detect(ClientInfo),
@ -115,8 +116,23 @@ t_conf_update(_) ->
emqx_config:put_zone_conf(new_zone, [flapping_detect], #{}), emqx_config:put_zone_conf(new_zone, [flapping_detect], #{}),
?assertEqual(Global, get_policy(new_zone)), ?assertEqual(Global, get_policy(new_zone)),
emqx_config:put_zone_conf(new_zone_1, [flapping_detect], #{window_time => 100}), emqx_config:put_zone_conf(zone_1, [flapping_detect], #{window_time => 100}),
?assertEqual(Global#{window_time := 100}, emqx_flapping:get_policy(new_zone_1)), ?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. ok.
get_policy(Zone) -> get_policy(Zone) ->