mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-17 21:15:32 +09:00
refactor(gateway): share shard helpers and test fixtures (#3381)
This commit is contained in:
1 parent
f22261f218
commit
e7b13fd19f
25 files changed
+101
-2021
No files matched your search
@@ -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) ->
|
||||
|
||||
Reference in new issue
Block a user