various gw fixes

This commit is contained in:
Hampus Kraft committed 2026-02-18 18:33:38 +00:00
1 parent 133c0ad619
commit 2be274e762
7 files changed
+327 -54

No files matched your search

+1 -1
View File
@@ -169,7 +169,7 @@ handle_cast({dispatch, Request}, State) ->
PendingCount = maps:fold(fun(_, S, Acc) ->
case maps:get(pending_connect, S, false) of true -> Acc + 1; _ -> Acc end
end, 0, Sessions),
logger:info("guild dispatch: event=~p guild_id=~p sessions=~p pending=~p",
logger:debug("guild dispatch: event=~p guild_id=~p sessions=~p pending=~p",
[Event, maps:get(id, State, unknown), SessionCount, PendingCount]),
ParsedEventData = parse_event_data(EventData),
{noreply, NewState} = guild_dispatch:handle_dispatch(Event, ParsedEventData, State),
+3 -3
View File
@@ -74,7 +74,7 @@ process_dispatch(Event, EventData, State) ->
FilteredSessions = filter_sessions_for_event(
Event, FinalData, SessionIdOpt, Sessions, FilterState
),
logger:info("process_dispatch: event=~p guild_id=~p total_sessions=~p filtered_sessions=~p",
logger:debug("process_dispatch: event=~p guild_id=~p total_sessions=~p filtered_sessions=~p",
[Event, GuildId, map_size(Sessions), length(FilteredSessions)]),
DispatchSuccess = dispatch_to_sessions(FilteredSessions, Event, FinalData, UpdatedState),
track_dispatch_metrics(Event, DispatchSuccess),
@@ -262,7 +262,7 @@ dispatch_bulk_to_session(_, _, _, _, Acc) ->
-spec dispatch_standard([session_pair()], event(), event_data(), guild_id(), guild_state()) ->
non_neg_integer().
dispatch_standard(FilteredSessions, Event, FinalData, GuildId, State) ->
logger:info("dispatch_standard: event=~p guild_id=~p filtered_sessions=~p member_count=~p",
logger:debug("dispatch_standard: event=~p guild_id=~p filtered_sessions=~p member_count=~p",
[Event, GuildId, length(FilteredSessions), maps:get(member_count, State, undefined)]),
SuccessCount = lists:foldl(
fun({Sid, SessionData}, Acc) ->
@@ -281,7 +281,7 @@ dispatch_standard(FilteredSessions, Event, FinalData, GuildId, State) ->
_:_ -> Acc
end;
false ->
logger:info("dispatch_standard skip: sid=~p is_pid=~p passive=~p small=~p",
logger:debug("dispatch_standard skip: sid=~p is_pid=~p passive=~p small=~p",
[Sid,
is_pid(Pid),
session_passive:is_passive(GuildId, SessionData),
+30 -1
View File
@@ -20,7 +20,15 @@
-include_lib("fluxer_gateway/include/timeout_config.hrl").
-export([start_link/0, start_or_lookup/1, start_or_lookup/2, lookup/1, lookup/2]).
-export([
start_link/0,
start_or_lookup/1,
start_or_lookup/2,
lookup/1,
lookup/2,
ensure_started/1,
ensure_started/2
]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-define(GUILD_PID_CACHE, guild_pid_cache).
@@ -58,6 +66,24 @@ lookup(GuildId, Timeout) ->
call_shard(GuildId, {lookup, GuildId}, Timeout)
end.
-spec ensure_started(guild_id()) -> ok | {error, term()}.
ensure_started(GuildId) ->
ensure_started(GuildId, ?DEFAULT_GEN_SERVER_TIMEOUT).
-spec ensure_started(guild_id(), pos_integer()) -> ok | {error, term()}.
ensure_started(GuildId, Timeout) ->
case call_shard(GuildId, {ensure_started, GuildId}, Timeout) of
ok ->
ok;
{ok, GuildPid} when is_pid(GuildPid) ->
ets:insert(?GUILD_PID_CACHE, {GuildId, GuildPid}),
ok;
{error, _} = Error ->
Error;
_ ->
{error, unavailable}
end.
-spec init(list()) -> {ok, state()}.
init([]) ->
process_flag(trap_exit, true),
@@ -76,6 +102,9 @@ handle_call({start_or_lookup, GuildId}, _From, State) ->
handle_call({lookup, GuildId}, _From, State) ->
{Reply, NewState} = forward_call(GuildId, {lookup, GuildId}, State),
{reply, Reply, NewState};
handle_call({ensure_started, GuildId}, _From, State) ->
{Reply, NewState} = forward_call(GuildId, {ensure_started, GuildId}, State),
{reply, Reply, NewState};
handle_call({stop_guild, GuildId}, _From, State) ->
{Reply, NewState} = forward_call(GuildId, {stop_guild, GuildId}, State),
{reply, Reply, NewState};
@@ -57,6 +57,8 @@ handle_call({start_or_lookup, GuildId}, From, State) ->
do_start_or_lookup(GuildId, From, State);
handle_call({lookup, GuildId}, _From, State) ->
do_lookup(GuildId, State);
handle_call({ensure_started, GuildId}, _From, State) ->
do_ensure_started(GuildId, State);
handle_call({stop_guild, GuildId}, _From, State) ->
do_stop_guild(GuildId, State);
handle_call({reload_guild, GuildId}, From, State) ->
@@ -134,6 +136,30 @@ do_lookup(GuildId, State) ->
end
end.
-spec do_ensure_started(guild_id(), state()) ->
{reply, ok | {ok, pid()} | {error, term()}, state()}.
do_ensure_started(GuildId, State) ->
Guilds = maps:get(guilds, State),
case maps:get(GuildId, Guilds, undefined) of
{Pid, _Ref} ->
{reply, {ok, Pid}, State};
loading ->
{reply, ok, State};
undefined ->
GuildName = process_registry:build_process_name(guild, GuildId),
case whereis(GuildName) of
undefined ->
{reply, ok, start_fetch_without_pending(GuildId, State)};
_ExistingPid ->
case process_registry:lookup_or_monitor(GuildName, GuildId, Guilds) of
{ok, Pid, _Ref, NewGuilds} ->
{reply, {ok, Pid}, State#{guilds => NewGuilds}};
{error, not_found} ->
{reply, {error, process_died}, State}
end
end
end.
-spec lookup_or_fetch(guild_id(), gen_server:from(), state()) ->
{reply, {ok, pid()}, state()} | {noreply, state()}.
lookup_or_fetch(GuildId, From, State) ->
@@ -161,6 +187,14 @@ start_fetch(GuildId, From, State) ->
spawn_fetch(GuildId, State),
{noreply, NewState}.
-spec start_fetch_without_pending(guild_id(), state()) -> state().
start_fetch_without_pending(GuildId, State) ->
Guilds = maps:get(guilds, State),
NewGuilds = maps:put(GuildId, loading, Guilds),
NewState = State#{guilds => NewGuilds},
spawn_fetch(GuildId, State),
NewState.
-spec spawn_fetch(guild_id(), state()) -> pid().
spawn_fetch(GuildId, _State) ->
Manager = self(),