mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-17 21:15:32 +09:00
fix(gateway): always trim the connect snapshot for guild connects (#2943)
This commit is contained in:
1 parent
b8e3807262
commit
43741cdad8
1 file changed
+16
-62
@@ -26,7 +26,6 @@
|
|||||||
<<"members">>, members_normalized, <<"member_role_index">>, members_sorted_ids
|
<<"members">>, members_normalized, <<"member_role_index">>, members_sorted_ids
|
||||||
]).
|
]).
|
||||||
-define(CONNECT_SNAPSHOT_HEAVY_SESSION_KEYS, [active_guilds, user_roles, viewable_channels]).
|
-define(CONNECT_SNAPSHOT_HEAVY_SESSION_KEYS, [active_guilds, user_roles, viewable_channels]).
|
||||||
-define(DEFAULT_CONNECT_SNAPSHOT_TRIM_MEMBERS, 5000).
|
|
||||||
|
|
||||||
-export_type([guild_state/0, guild_reply/1, user_id/0]).
|
-export_type([guild_state/0, guild_reply/1, user_id/0]).
|
||||||
|
|
||||||
@@ -196,32 +195,7 @@ maybe_trim_connect_snapshot(Item, Base, State) ->
|
|||||||
|
|
||||||
-spec should_trim_connect_snapshot(guild_state()) -> boolean().
|
-spec should_trim_connect_snapshot(guild_state()) -> boolean().
|
||||||
should_trim_connect_snapshot(State) ->
|
should_trim_connect_snapshot(State) ->
|
||||||
case connect_snapshot_trim_member_threshold() of
|
has_members_ets(State).
|
||||||
undefined ->
|
|
||||||
false;
|
|
||||||
Threshold ->
|
|
||||||
member_count_at_least(Threshold, State) andalso has_members_ets(State)
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec member_count_at_least(pos_integer(), guild_state()) -> boolean().
|
|
||||||
member_count_at_least(Threshold, State) ->
|
|
||||||
case maps:get(member_count, State, undefined) of
|
|
||||||
Count when is_integer(Count) -> Count >= Threshold;
|
|
||||||
_ -> false
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec connect_snapshot_trim_member_threshold() -> pos_integer() | undefined.
|
|
||||||
connect_snapshot_trim_member_threshold() ->
|
|
||||||
case
|
|
||||||
application:get_env(
|
|
||||||
fluxer_gateway,
|
|
||||||
connect_snapshot_trim_member_threshold,
|
|
||||||
?DEFAULT_CONNECT_SNAPSHOT_TRIM_MEMBERS
|
|
||||||
)
|
|
||||||
of
|
|
||||||
N when is_integer(N), N > 0 -> N;
|
|
||||||
_ -> undefined
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec has_members_ets(guild_state()) -> boolean().
|
-spec has_members_ets(guild_state()) -> boolean().
|
||||||
has_members_ets(#{data := #{members_ets := Tab}}) -> is_reference(Tab);
|
has_members_ets(#{data := #{members_ets := Tab}}) -> is_reference(Tab);
|
||||||
@@ -549,47 +523,27 @@ projection_keeps_snapshot_without_sessions_test() ->
|
|||||||
trim_state(MemberCount) ->
|
trim_state(MemberCount) ->
|
||||||
#{id => 42, member_count => MemberCount, data => #{members_ets => make_ref()}}.
|
#{id => 42, member_count => MemberCount, data => #{members_ets => make_ref()}}.
|
||||||
|
|
||||||
with_trim_threshold(Threshold, Fun) ->
|
trim_applies_at_every_member_count_test() ->
|
||||||
application:set_env(fluxer_gateway, connect_snapshot_trim_member_threshold, Threshold),
|
?assertEqual(true, should_trim_connect_snapshot(trim_state(49435))),
|
||||||
|
?assertEqual(true, should_trim_connect_snapshot(trim_state(5000))),
|
||||||
|
?assertEqual(true, should_trim_connect_snapshot(trim_state(1))),
|
||||||
|
?assertEqual(true, should_trim_connect_snapshot(maps:remove(member_count, trim_state(1)))).
|
||||||
|
|
||||||
|
trim_ignores_threshold_env_test() ->
|
||||||
|
application:set_env(fluxer_gateway, connect_snapshot_trim_member_threshold, 20000),
|
||||||
try
|
try
|
||||||
Fun()
|
?assertEqual(true, should_trim_connect_snapshot(trim_state(1)))
|
||||||
after
|
after
|
||||||
application:unset_env(fluxer_gateway, connect_snapshot_trim_member_threshold)
|
application:unset_env(fluxer_gateway, connect_snapshot_trim_member_threshold)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
trim_defaults_to_large_guilds_test() ->
|
|
||||||
application:unset_env(fluxer_gateway, connect_snapshot_trim_member_threshold),
|
|
||||||
Default = ?DEFAULT_CONNECT_SNAPSHOT_TRIM_MEMBERS,
|
|
||||||
?assertEqual(true, should_trim_connect_snapshot(trim_state(49435))),
|
|
||||||
?assertEqual(true, should_trim_connect_snapshot(trim_state(Default))),
|
|
||||||
?assertEqual(false, should_trim_connect_snapshot(trim_state(Default - 1))).
|
|
||||||
|
|
||||||
trim_needs_member_count_at_threshold_test() ->
|
|
||||||
with_trim_threshold(20000, fun() ->
|
|
||||||
?assertEqual(true, should_trim_connect_snapshot(trim_state(49435))),
|
|
||||||
?assertEqual(true, should_trim_connect_snapshot(trim_state(20000))),
|
|
||||||
?assertEqual(false, should_trim_connect_snapshot(trim_state(19999))),
|
|
||||||
NoCount = maps:remove(member_count, trim_state(1)),
|
|
||||||
?assertEqual(false, should_trim_connect_snapshot(NoCount))
|
|
||||||
end).
|
|
||||||
|
|
||||||
trim_needs_members_ets_test() ->
|
trim_needs_members_ets_test() ->
|
||||||
with_trim_threshold(1, fun() ->
|
?assertEqual(false, should_trim_connect_snapshot(#{member_count => 10, data => #{}})),
|
||||||
?assertEqual(false, should_trim_connect_snapshot(#{member_count => 10, data => #{}})),
|
?assertEqual(
|
||||||
?assertEqual(
|
false,
|
||||||
false,
|
should_trim_connect_snapshot(#{member_count => 10, data => #{members_ets => 7}})
|
||||||
should_trim_connect_snapshot(#{member_count => 10, data => #{members_ets => 7}})
|
),
|
||||||
),
|
?assertEqual(false, should_trim_connect_snapshot(#{member_count => 10})).
|
||||||
?assertEqual(false, should_trim_connect_snapshot(#{member_count => 10}))
|
|
||||||
end).
|
|
||||||
|
|
||||||
trim_ignores_invalid_threshold_test() ->
|
|
||||||
with_trim_threshold(0, fun() ->
|
|
||||||
?assertEqual(false, should_trim_connect_snapshot(trim_state(49435)))
|
|
||||||
end),
|
|
||||||
with_trim_threshold(not_an_integer, fun() ->
|
|
||||||
?assertEqual(false, should_trim_connect_snapshot(trim_state(49435)))
|
|
||||||
end).
|
|
||||||
|
|
||||||
trim_and_projection_target_disjoint_keys_test() ->
|
trim_and_projection_target_disjoint_keys_test() ->
|
||||||
?assertEqual(
|
?assertEqual(
|
||||||
|
|||||||
Reference in new issue
Block a user