fix(battle-node): collision-safe battle-id registration + viewer eviction
RegisterPending → TryRegisterPending (TryAdd instead of indexer) so battle-id collisions return false instead of silently evicting a live battle. MatchingBridge retries with fresh IDs on collision (max 5). Before registering, EvictStaleForViewer removes any stale pending battle the viewer left behind, enforcing the one-pending-per-viewer invariant that was previously comment-asserted. Store tests switched to per-test local stores to fix a race under the assembly-wide ParallelScope.All. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,8 +2,10 @@ namespace SVSim.BattleNode.Sessions;
|
||||
|
||||
public interface IBattleSessionStore
|
||||
{
|
||||
/// <summary>Register a battle minted by the matching bridge, awaiting a WS connect.</summary>
|
||||
void RegisterPending(PendingBattle battle);
|
||||
/// <summary>Register a battle minted by the matching bridge, awaiting a WS connect.
|
||||
/// Returns false if a battle with the same id is already pending (caller should retry
|
||||
/// with a fresh id).</summary>
|
||||
bool TryRegisterPending(PendingBattle battle);
|
||||
|
||||
/// <summary>Look up the pending battle. Returns null if not present.</summary>
|
||||
PendingBattle? TryGetPending(string battleId);
|
||||
|
||||
@@ -6,8 +6,8 @@ public sealed class InMemoryBattleSessionStore : IBattleSessionStore
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, PendingBattle> _pending = new();
|
||||
|
||||
public void RegisterPending(PendingBattle battle) =>
|
||||
_pending[battle.BattleId] = battle;
|
||||
public bool TryRegisterPending(PendingBattle battle) =>
|
||||
_pending.TryAdd(battle.BattleId, battle);
|
||||
|
||||
public PendingBattle? TryGetPending(string battleId) =>
|
||||
_pending.TryGetValue(battleId, out var b) ? b : null;
|
||||
|
||||
Reference in New Issue
Block a user