refactor(gateway): share shard helpers and test fixtures (#3381)

This commit is contained in:
Hampus authored and GitHub committed 2026-10-10 23:04:30 +02:00
1 parent f22261f218
commit e7b13fd19f
25 files changed
+101 -2021

No files matched your search

+3 -33
View File
@@ -44,7 +44,7 @@ diagnostic_info() ->
init([]) ->
process_flag(trap_exit, true),
erlang:process_flag(fullsweep_after, 10),
{ShardCount, _Source} = determine_shard_count(presence_bus_shards),
{ShardCount, _Source} = shard_utils:determine_shard_count(presence_bus_shards),
Shards = start_shards(ShardCount, #{}),
{ok, #{shards => Shards, shard_count => ShardCount}, hibernate}.
@@ -127,22 +127,6 @@ code_change(_OldVsn, State, _Extra) ->
erlang:garbage_collect(),
{ok, State}.
-spec determine_shard_count(atom()) -> {pos_integer(), configured | auto}.
determine_shard_count(ConfigKey) ->
case fluxer_gateway_env:get(ConfigKey) of
Value when is_integer(Value), Value > 0 ->
{Value, configured};
_ ->
{default_shard_count(), auto}
end.
-spec default_shard_count() -> pos_integer().
default_shard_count() ->
shard_utils:max_positive([
erlang:system_info(logical_processors_available),
erlang:system_info(schedulers_online)
]).
-spec start_shards(pos_integer(), #{}) -> #{non_neg_integer() => shard()}.
start_shards(Count, Acc) ->
lists:foldl(fun start_shard_acc/2, Acc, lists:seq(0, Count - 1)).
@@ -238,26 +222,12 @@ select_shard(Key, Count) when Count > 0 ->
-spec find_shard_by_ref(reference(), #{non_neg_integer() => shard()}) ->
{ok, non_neg_integer()} | not_found.
find_shard_by_ref(Ref, Shards) ->
maps:fold(
fun
(Index, #{ref := R}, _) when R =:= Ref -> {ok, Index};
(_, _, Acc) -> Acc
end,
not_found,
Shards
).
shard_utils:find_shard_by_ref(Ref, Shards).
-spec find_shard_by_pid(pid(), #{non_neg_integer() => shard()}) ->
{ok, non_neg_integer()} | not_found.
find_shard_by_pid(Pid, Shards) ->
maps:fold(
fun
(Index, #{pid := P}, _) when P =:= Pid -> {ok, Index};
(_, _, Acc) -> Acc
end,
not_found,
Shards
).
shard_utils:find_shard_by_pid(Pid, Shards).
-spec safe_gen_server_call(pid() | atom(), term(), timeout()) -> term().
safe_gen_server_call(Server, Request, Timeout) ->