mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-18 05:25:40 +09:00
perf(push): batch badge count invalidation per mention chunk (#2178)
This commit is contained in:
1 parent
78056e0041
commit
43d6c85f7e
7 files changed
+148
-8
No files matched your search
@@ -12,6 +12,8 @@ execute_method(<<"push.sync_user_blocked_ids">>, P) ->
|
||||
do_sync_user_blocked_ids(P);
|
||||
execute_method(<<"push.invalidate_badge_count">>, P) ->
|
||||
do_invalidate_badge_count(P);
|
||||
execute_method(<<"push.invalidate_badge_counts">>, P) ->
|
||||
do_invalidate_badge_counts(P);
|
||||
execute_method(<<"push.clear_channel_notifications">>, P) ->
|
||||
do_clear_channel_notifications(P);
|
||||
execute_method(<<"push.invalidate_subscriptions">>, P) ->
|
||||
@@ -43,6 +45,19 @@ do_invalidate_badge_count(#{<<"user_id">> := UBin}) ->
|
||||
fanout_local_cache_mutation(push, invalidate_user_badge_count_local, [UserId]),
|
||||
true.
|
||||
|
||||
-spec do_invalidate_badge_counts(map()) -> true.
|
||||
do_invalidate_badge_counts(#{<<"user_ids">> := UserIdsRaw}) ->
|
||||
UserIds = lists:usort(validation:snowflake_list_or_throw(<<"user_ids">>, UserIdsRaw)),
|
||||
gateway_rpc_guild_routing:validate_batch_size(length(UserIds)),
|
||||
fanout_badge_count_invalidation(UserIds).
|
||||
|
||||
-spec fanout_badge_count_invalidation([pos_integer()]) -> true.
|
||||
fanout_badge_count_invalidation([]) ->
|
||||
true;
|
||||
fanout_badge_count_invalidation(UserIds) ->
|
||||
fanout_local_cache_mutation(push, invalidate_user_badge_counts_local, [UserIds]),
|
||||
true.
|
||||
|
||||
-spec do_clear_channel_notifications(map()) -> true.
|
||||
do_clear_channel_notifications(#{
|
||||
<<"user_id">> := UBin,
|
||||
@@ -79,3 +94,24 @@ fanout_local_cache_mutation(Module, Function, Args) ->
|
||||
),
|
||||
ok
|
||||
end.
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
invalidate_badge_counts_rejects_oversized_batch_test() ->
|
||||
UserIds = [integer_to_binary(UserId) || UserId <- lists:seq(1000, 1200)],
|
||||
?assertError(
|
||||
{gateway_rpc_error, _},
|
||||
execute_method(<<"push.invalidate_badge_counts">>, #{<<"user_ids">> => UserIds})
|
||||
).
|
||||
|
||||
invalidate_badge_counts_rejects_invalid_snowflakes_test() ->
|
||||
?assertError(
|
||||
{validation, _},
|
||||
execute_method(<<"push.invalidate_badge_counts">>, #{<<"user_ids">> => [<<"nope">>]})
|
||||
).
|
||||
|
||||
invalidate_badge_counts_accepts_an_empty_batch_test() ->
|
||||
?assert(execute_method(<<"push.invalidate_badge_counts">>, #{<<"user_ids">> => []})).
|
||||
|
||||
-endif.
|
||||
@@ -16,6 +16,7 @@
|
||||
invalidate_user_subscriptions_local/1,
|
||||
invalidate_user_badge_count/1,
|
||||
invalidate_user_badge_count_local/1,
|
||||
invalidate_user_badge_counts_local/1,
|
||||
clear_channel_notifications/3
|
||||
]).
|
||||
-export([get_cache_stats/0]).
|
||||
@@ -172,6 +173,15 @@ invalidate_user_badge_count_local(UserId) ->
|
||||
push_ets_cache:delete_badge_count(UserId)
|
||||
end).
|
||||
|
||||
-spec invalidate_user_badge_counts_local(term()) -> ok.
|
||||
invalidate_user_badge_counts_local(UserIds) ->
|
||||
case integer_list(UserIds) of
|
||||
{ok, TypedUserIds} ->
|
||||
lists:foreach(fun invalidate_user_badge_count_local/1, TypedUserIds);
|
||||
error ->
|
||||
ok
|
||||
end.
|
||||
|
||||
-spec maybe_cast(term(), term()) -> ok.
|
||||
maybe_cast(Key, Msg) ->
|
||||
case is_push_noop() of
|
||||
@@ -551,6 +561,25 @@ sync_user_blocked_ids_local_updates_local_cache_test() ->
|
||||
?assertEqual([20, 30], push_ets_cache:get_blocked_ids(10))
|
||||
end).
|
||||
|
||||
invalidate_user_badge_counts_local_deletes_every_cached_entry_test() ->
|
||||
push_ets_cache:init(),
|
||||
push_ets_cache:put_badge_count(10, 5, 1000),
|
||||
push_ets_cache:put_badge_count(11, 7, 1000),
|
||||
with_registered_push(fun() ->
|
||||
ok = invalidate_user_badge_counts_local([10, 11])
|
||||
end),
|
||||
?assertEqual(undefined, push_ets_cache:get_badge_count(10)),
|
||||
?assertEqual(undefined, push_ets_cache:get_badge_count(11)).
|
||||
|
||||
invalidate_user_badge_counts_local_ignores_untyped_ids_test() ->
|
||||
push_ets_cache:init(),
|
||||
push_ets_cache:put_badge_count(12, 5, 1000),
|
||||
with_registered_push(fun() ->
|
||||
ok = invalidate_user_badge_counts_local([<<"12">>])
|
||||
end),
|
||||
?assertEqual({5, 1000}, push_ets_cache:get_badge_count(12)),
|
||||
push_ets_cache:delete_badge_count(12).
|
||||
|
||||
invalidate_user_subscriptions_local_deletes_local_cache_test() ->
|
||||
push_ets_cache:init(),
|
||||
push_ets_cache:put_subscriptions(10, [#{<<"endpoint">> => <<"test">>}]),
|
||||
|
||||
Reference in new issue
Block a user