From 14a467dc69630242e76da0c86c9320b07dda1710 Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 20 May 2026 19:47:15 -0400 Subject: [PATCH] Fix 1: Move SpectatorRegistryService from in-memory to Redis Sets Spectator registrations were held in a per-process ConcurrentDictionary, causing spectators to miss updates when game actions were handled by a different pod than the one that processed game.spectate. Changes: - SpectatorRegistryService: replace ConcurrentDictionary with Redis Set operations (SADD/SREM/SMEMBERS/DEL) via IConnectionMultiplexer. All four methods are now async (RegisterAsync, UnregisterAsync, GetSpectatorsAsync, RemoveSessionAsync). TTL of 24h on each SADD for self-expiry on abnormal session end. - RedisKeys: add SpectatorSet(Guid sessionId) -> "spectators:{id}" - SOR.Game.csproj: add StackExchange.Redis package reference - GameHandlerBase: all GetSpectators/RemoveSession call sites updated to await the new async methods (BroadcastToAllViewersAsync, PushStateToPlayersAsync x3, FinalizeGame x2, BroadcastPlayerEliminated) - GameSpectateHandler: await RegisterAsync - GameStopSpectatingHandler: make HandleAsync truly async, await UnregisterAsync - ServiceRegistration: update comment to reflect Redis backing Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../src/SOR.Game/Handlers/GameHandlerBase.cs | 18 ++--- .../SOR.Game/Handlers/GameSpectateHandler.cs | 2 +- .../Handlers/GameStopSpectatingHandler.cs | 9 +-- Server/src/SOR.Game/SOR.Game.csproj | 4 + Server/src/SOR.Game/ServiceRegistration.cs | 3 +- .../src/SOR.Game/SpectatorRegistryService.cs | 76 ++++++++++++++----- Server/src/SOR.Shared/Redis/RedisKeys.cs | 12 +++ 7 files changed, 90 insertions(+), 34 deletions(-) diff --git a/Server/src/SOR.Game/Handlers/GameHandlerBase.cs b/Server/src/SOR.Game/Handlers/GameHandlerBase.cs index 5b40568..ad6096a 100644 --- a/Server/src/SOR.Game/Handlers/GameHandlerBase.cs +++ b/Server/src/SOR.Game/Handlers/GameHandlerBase.cs @@ -244,7 +244,7 @@ internal abstract class GameHandlerBase var registry = SpectatorRegistryService.Instance; if (registry != null) { - foreach (var specId in registry.GetSpectators(session.Id)) + foreach (var specId in await registry.GetSpectatorsAsync(session.Id)) tasks.Add(clientPush.PushAsync(specId.ToString(), push, ct)); } await Task.WhenAll(tasks); @@ -328,7 +328,7 @@ internal abstract class GameHandlerBase var regFull = SpectatorRegistryService.Instance; if (regFull != null) { - var spectatorsFull = regFull.GetSpectators(session.Id); + var spectatorsFull = await regFull.GetSpectatorsAsync(session.Id); if (spectatorsFull.Count > 0) { var spectatorDto = GameEngine.ToDtoSpectator(state); @@ -348,7 +348,7 @@ internal abstract class GameHandlerBase var registry = SpectatorRegistryService.Instance; if (registry != null) { - var spectators = registry.GetSpectators(session.Id); + var spectators = await registry.GetSpectatorsAsync(session.Id); if (spectators.Count > 0) { var spectatorDto = GameEngine.ToDtoSpectator(state); @@ -375,7 +375,7 @@ internal abstract class GameHandlerBase var registry = SpectatorRegistryService.Instance; if (registry != null) { - var spectators = registry.GetSpectators(session.Id); + var spectators = await registry.GetSpectatorsAsync(session.Id); if (spectators.Count > 0) { var spectatorDto = GameEngine.ToDtoSpectator(state); @@ -626,10 +626,10 @@ internal abstract class GameHandlerBase var spectatorRegistry = SpectatorRegistryService.Instance; if (spectatorRegistry != null) { - foreach (var specId in spectatorRegistry.GetSpectators(session.Id)) + foreach (var specId in await spectatorRegistry.GetSpectatorsAsync(session.Id)) gameOverTasks.Add(clientPush.PushAsync(specId.ToString(), ServerMessage.Push("game.over", push), ct)); - spectatorRegistry.RemoveSession(session.Id); + await spectatorRegistry.RemoveSessionAsync(session.Id); } await Task.WhenAll(gameOverTasks); @@ -760,10 +760,10 @@ internal abstract class GameHandlerBase var spectatorRegistry = SpectatorRegistryService.Instance; if (spectatorRegistry != null) { - foreach (var specId in spectatorRegistry.GetSpectators(session.Id)) + foreach (var specId in await spectatorRegistry.GetSpectatorsAsync(session.Id)) gameOverTasks.Add(clientPush.PushAsync(specId.ToString(), ServerMessage.Push("game.over", push), ct)); - spectatorRegistry.RemoveSession(session.Id); + await spectatorRegistry.RemoveSessionAsync(session.Id); } await Task.WhenAll(gameOverTasks); @@ -808,7 +808,7 @@ internal abstract class GameHandlerBase var spectatorRegistry = SpectatorRegistryService.Instance; if (spectatorRegistry != null) { - foreach (var specId in spectatorRegistry.GetSpectators(session.Id)) + foreach (var specId in await spectatorRegistry.GetSpectatorsAsync(session.Id)) tasks.Add(clientPush.PushAsync(specId.ToString(), push, ct)); } diff --git a/Server/src/SOR.Game/Handlers/GameSpectateHandler.cs b/Server/src/SOR.Game/Handlers/GameSpectateHandler.cs index dba3554..dd4c009 100644 --- a/Server/src/SOR.Game/Handlers/GameSpectateHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameSpectateHandler.cs @@ -49,7 +49,7 @@ internal sealed class GameSpectateHandler : GameHandlerBase, IMessageHandler if (state.Phase == GamePhase.Placement) throw new GameException("PLACEMENT_IN_PROGRESS"); - _registry.Register(req.SessionId, accountId); + await _registry.RegisterAsync(req.SessionId, accountId); var dto = GameEngine.ToDtoSpectator(state); diff --git a/Server/src/SOR.Game/Handlers/GameStopSpectatingHandler.cs b/Server/src/SOR.Game/Handlers/GameStopSpectatingHandler.cs index 54ab5f4..4e02a19 100644 --- a/Server/src/SOR.Game/Handlers/GameStopSpectatingHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameStopSpectatingHandler.cs @@ -22,7 +22,7 @@ internal sealed class GameStopSpectatingHandler : GameHandlerBase, IMessageHandl public string MessageType => "game.spectate.stop"; - public Task HandleAsync( + public async Task HandleAsync( IClientMessage message, AuthenticatedConnection connection, CancellationToken ct) @@ -30,10 +30,9 @@ internal sealed class GameStopSpectatingHandler : GameHandlerBase, IMessageHandl var req = _serializer.DeserializePayload(message.Payload); var accountId = Guid.Parse(connection.UserId); - _registry.Unregister(req.SessionId, accountId); + await _registry.UnregisterAsync(req.SessionId, accountId); - return Task.FromResult( - ServerMessage.Reply("game.spectate.stop.ack", message.Seq, - new StopSpectatingResponse(req.SessionId))); + return ServerMessage.Reply("game.spectate.stop.ack", message.Seq, + new StopSpectatingResponse(req.SessionId)); } } diff --git a/Server/src/SOR.Game/SOR.Game.csproj b/Server/src/SOR.Game/SOR.Game.csproj index 67e9da6..c40c117 100644 --- a/Server/src/SOR.Game/SOR.Game.csproj +++ b/Server/src/SOR.Game/SOR.Game.csproj @@ -7,6 +7,10 @@ + + + + diff --git a/Server/src/SOR.Game/ServiceRegistration.cs b/Server/src/SOR.Game/ServiceRegistration.cs index e54bf9e..b3195d2 100644 --- a/Server/src/SOR.Game/ServiceRegistration.cs +++ b/Server/src/SOR.Game/ServiceRegistration.cs @@ -32,7 +32,8 @@ public static class ServiceRegistration services.AddSingleton(); services.AddHostedService(sp => sp.GetRequiredService()); - // Tracks spectators per session (in-memory; spectators only, players are not registered here). + // Tracks spectators per session via Redis Set; shared across all pods. + // Spectators only — players are not registered here. services.AddSingleton(); // Tracks clients subscribed to game activity events for the live-star skybox. diff --git a/Server/src/SOR.Game/SpectatorRegistryService.cs b/Server/src/SOR.Game/SpectatorRegistryService.cs index 437f000..265bb86 100644 --- a/Server/src/SOR.Game/SpectatorRegistryService.cs +++ b/Server/src/SOR.Game/SpectatorRegistryService.cs @@ -1,41 +1,81 @@ namespace SOR.Game; -using System.Collections.Concurrent; using System.Collections.Immutable; +using StackExchange.Redis; +using SOR.Shared.Redis; /// -/// In-memory registry that tracks which accounts are currently spectating each session. +/// Tracks which accounts are currently spectating each active game session. +/// +/// Storage: Redis Set keyed by . +/// Using Redis (instead of in-process memory) means registrations are shared +/// across all server pods — a spectator who joined on pod A will receive pushes +/// from pod B when the next game action is handled there. +/// +/// Each SADD refreshes the set's TTL to 24 hours so that stale entries +/// self-expire if a session ends abnormally without a RemoveSession call. +/// /// Registered as a singleton. Handlers call Register/Unregister; FinalizeGameAsync /// calls RemoveSession via the static Instance reference. /// internal sealed class SpectatorRegistryService { /// - /// Static reference for use in static helpers (FinalizeGameAsync, PushStateToPlayersAsync) - /// without requiring DI injection into every call site. + /// Static reference used by static helpers (FinalizeGameAsync, PushStateToPlayersAsync) + /// that cannot use constructor injection because they are static methods. + /// Set once when the singleton is first constructed by the DI container. /// internal static SpectatorRegistryService? Instance { get; private set; } - private readonly ConcurrentDictionary> _map = new(); + // Maximum time a spectator set can live without being refreshed. + // Covers the longest plausible game session with headroom. + private static readonly TimeSpan SetTtl = TimeSpan.FromHours(24); - public SpectatorRegistryService() + private readonly IConnectionMultiplexer _redis; + + public SpectatorRegistryService(IConnectionMultiplexer redis) { + _redis = redis; Instance = this; } - public void Register(Guid sessionId, Guid accountId) - => _map.AddOrUpdate(sessionId, - ImmutableHashSet.Create(accountId), - (_, existing) => existing.Add(accountId)); + /// Add as a spectator for . + public async Task RegisterAsync(Guid sessionId, Guid accountId) + { + var db = _redis.GetDatabase(); + var key = RedisKeys.SpectatorSet(sessionId); + await db.SetAddAsync(key, accountId.ToString()); + await db.KeyExpireAsync(key, SetTtl); + } - public void Unregister(Guid sessionId, Guid accountId) - => _map.AddOrUpdate(sessionId, - ImmutableHashSet.Empty, - (_, existing) => existing.Remove(accountId)); + /// Remove from the spectator set for . + public Task UnregisterAsync(Guid sessionId, Guid accountId) + => _redis.GetDatabase().SetRemoveAsync( + RedisKeys.SpectatorSet(sessionId), + accountId.ToString()); - public void RemoveSession(Guid sessionId) - => _map.TryRemove(sessionId, out _); + /// Delete the entire spectator set when a session ends. + public Task RemoveSessionAsync(Guid sessionId) + => _redis.GetDatabase().KeyDeleteAsync(RedisKeys.SpectatorSet(sessionId)); - public IReadOnlySet GetSpectators(Guid sessionId) - => _map.TryGetValue(sessionId, out var set) ? set : ImmutableHashSet.Empty; + /// + /// Returns the current set of spectator account IDs for . + /// Returns an empty set when no spectators are registered or the key has expired. + /// + public async Task> GetSpectatorsAsync(Guid sessionId) + { + var members = await _redis.GetDatabase() + .SetMembersAsync(RedisKeys.SpectatorSet(sessionId)); + + if (members.Length == 0) + return ImmutableHashSet.Empty; + + var result = new HashSet(members.Length); + foreach (var m in members) + { + if (Guid.TryParse(m, out var id)) + result.Add(id); + } + return result; + } } diff --git a/Server/src/SOR.Shared/Redis/RedisKeys.cs b/Server/src/SOR.Shared/Redis/RedisKeys.cs index dc0a2e1..67f67f7 100644 --- a/Server/src/SOR.Shared/Redis/RedisKeys.cs +++ b/Server/src/SOR.Shared/Redis/RedisKeys.cs @@ -60,6 +60,18 @@ public static class RedisKeys /// public static string LeaderboardRanked() => "leaderboard:ranked"; + // ── Spectators ──────────────────────────────────────────────────────────── + + /// + /// Redis Set of spectator account IDs (as strings) currently watching a game session. + /// Shared across all pods so any pod can push to spectators regardless of which pod + /// handled the game.spectate request. + /// SADD on join, SREM on leave, DEL when the session ends. + /// TTL is set on SADD to self-expire after the maximum session lifetime. + /// Example: "spectators:7c9e6679-7425-40de-944b-e07fc1f90ae7" + /// + public static string SpectatorSet(Guid sessionId) => $"spectators:{sessionId}"; + // ── Feature Flags ───────────────────────────────────────────────────────── ///