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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-05-20 19:47:15 -04:00
co-authored by Claude Happy
parent e5cfbae7bd
commit 14a467dc69
7 changed files with 90 additions and 34 deletions
@@ -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));
}
@@ -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);
@@ -22,7 +22,7 @@ internal sealed class GameStopSpectatingHandler : GameHandlerBase, IMessageHandl
public string MessageType => "game.spectate.stop";
public Task<IServerMessage?> HandleAsync(
public async Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken ct)
@@ -30,10 +30,9 @@ internal sealed class GameStopSpectatingHandler : GameHandlerBase, IMessageHandl
var req = _serializer.DeserializePayload<StopSpectatingRequest>(message.Payload);
var accountId = Guid.Parse(connection.UserId);
_registry.Unregister(req.SessionId, accountId);
await _registry.UnregisterAsync(req.SessionId, accountId);
return Task.FromResult<IServerMessage?>(
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));
}
}
+4
View File
@@ -7,6 +7,10 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="StackExchange.Redis" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SOR.Database\SOR.Database.csproj" />
<ProjectReference Include="..\SOR.Networking.Abstractions\SOR.Networking.Abstractions.csproj" />
+2 -1
View File
@@ -32,7 +32,8 @@ public static class ServiceRegistration
services.AddSingleton<GameTurnTimerService>();
services.AddHostedService(sp => sp.GetRequiredService<GameTurnTimerService>());
// 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<SpectatorRegistryService>();
// Tracks clients subscribed to game activity events for the live-star skybox.
+58 -18
View File
@@ -1,41 +1,81 @@
namespace SOR.Game;
using System.Collections.Concurrent;
using System.Collections.Immutable;
using StackExchange.Redis;
using SOR.Shared.Redis;
/// <summary>
/// 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 <see cref="RedisKeys.SpectatorSet(Guid)"/>.
/// 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.
/// </summary>
internal sealed class SpectatorRegistryService
{
/// <summary>
/// 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.
/// </summary>
internal static SpectatorRegistryService? Instance { get; private set; }
private readonly ConcurrentDictionary<Guid, ImmutableHashSet<Guid>> _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));
/// <summary>Add <paramref name="accountId"/> as a spectator for <paramref name="sessionId"/>.</summary>
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<Guid>.Empty,
(_, existing) => existing.Remove(accountId));
/// <summary>Remove <paramref name="accountId"/> from the spectator set for <paramref name="sessionId"/>.</summary>
public Task UnregisterAsync(Guid sessionId, Guid accountId)
=> _redis.GetDatabase().SetRemoveAsync(
RedisKeys.SpectatorSet(sessionId),
accountId.ToString());
public void RemoveSession(Guid sessionId)
=> _map.TryRemove(sessionId, out _);
/// <summary>Delete the entire spectator set when a session ends.</summary>
public Task RemoveSessionAsync(Guid sessionId)
=> _redis.GetDatabase().KeyDeleteAsync(RedisKeys.SpectatorSet(sessionId));
public IReadOnlySet<Guid> GetSpectators(Guid sessionId)
=> _map.TryGetValue(sessionId, out var set) ? set : ImmutableHashSet<Guid>.Empty;
/// <summary>
/// Returns the current set of spectator account IDs for <paramref name="sessionId"/>.
/// Returns an empty set when no spectators are registered or the key has expired.
/// </summary>
public async Task<IReadOnlySet<Guid>> GetSpectatorsAsync(Guid sessionId)
{
var members = await _redis.GetDatabase()
.SetMembersAsync(RedisKeys.SpectatorSet(sessionId));
if (members.Length == 0)
return ImmutableHashSet<Guid>.Empty;
var result = new HashSet<Guid>(members.Length);
foreach (var m in members)
{
if (Guid.TryParse(m, out var id))
result.Add(id);
}
return result;
}
}
+12
View File
@@ -60,6 +60,18 @@ public static class RedisKeys
/// </summary>
public static string LeaderboardRanked() => "leaderboard:ranked";
// ── Spectators ────────────────────────────────────────────────────────────
/// <summary>
/// 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"
/// </summary>
public static string SpectatorSet(Guid sessionId) => $"spectators:{sessionId}";
// ── Feature Flags ─────────────────────────────────────────────────────────
/// <summary>