refactor: replace Dictionary with ConcurrentDictionary for sessions#663
refactor: replace Dictionary with ConcurrentDictionary for sessions#663AngeloTadeucci merged 1 commit intomasterfrom
Conversation
Replace the Dictionary<long, GameSession> with ConcurrentDictionary<long, GameSession> to provide thread-safe access without explicit locking. This eliminates the need for mutex locks around session dictionary operations. Remove mutex locks from OnConnected, OnDisconnected, GetSession, GetSessionByAccountId, GetSessions, and event broadcasting methods since ConcurrentDictionary handles synchronization internally. Update OnDisconnected to use TryRemove with KeyValuePair for atomic reference equality checks, ensuring the correct session is removed during reconnection scenarios (same-channel migration). Remove unused Maple2.Graphics.Interface project. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe PR replaces manual mutex-based locking on session management with a lock-free ConcurrentDictionary approach, eliminating explicit synchronization code. Additionally, the Graphics.Interface project configuration file is completely cleared of its build settings. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
Maple2.Server.Game/GameServer.cs (1)
91-93: Consider performance for high session counts.This performs an O(n) scan over all sessions. While acceptable for typical server populations, if session counts grow large, consider maintaining a secondary
ConcurrentDictionary<long, GameSession>keyed byAccountIdfor O(1) lookups.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@Maple2.Server.Game/GameServer.cs` around lines 91 - 93, GetSessionByAccountId currently scans sessions.Values with FirstOrDefault which is O(n); add and maintain a secondary concurrent map keyed by AccountId (e.g., ConcurrentDictionary<long, GameSession> accountSessions) and update it whenever sessions are added/removed/changed (the same places that modify the primary sessions collection) so GetSessionByAccountId can perform an O(1) lookup from accountSessions.TryGetValue(accountId, out var session) and return the session or null; ensure thread-safety and keep accountSessions in sync with the primary sessions map and reference the symbols GetSessionByAccountId, sessions, AccountId, and GameSession when making changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@Maple2.Server.Game/GameServer.cs`:
- Around line 91-93: GetSessionByAccountId currently scans sessions.Values with
FirstOrDefault which is O(n); add and maintain a secondary concurrent map keyed
by AccountId (e.g., ConcurrentDictionary<long, GameSession> accountSessions) and
update it whenever sessions are added/removed/changed (the same places that
modify the primary sessions collection) so GetSessionByAccountId can perform an
O(1) lookup from accountSessions.TryGetValue(accountId, out var session) and
return the session or null; ensure thread-safety and keep accountSessions in
sync with the primary sessions map and reference the symbols
GetSessionByAccountId, sessions, AccountId, and GameSession when making changes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 42232260-aeab-42e4-8637-47126c83352c
📒 Files selected for processing (2)
Maple2.Graphics.Interface/Maple2.Graphics.Interface.csprojMaple2.Server.Game/GameServer.cs
💤 Files with no reviewable changes (1)
- Maple2.Graphics.Interface/Maple2.Graphics.Interface.csproj
Replace the Dictionary<long, GameSession> with
ConcurrentDictionary<long, GameSession> to provide thread-safe access without explicit locking. This eliminates the need for mutex locks around session dictionary operations.
Remove mutex locks from OnConnected, OnDisconnected, GetSession, GetSessionByAccountId, GetSessions, and event broadcasting methods since ConcurrentDictionary handles synchronization internally.
Update OnDisconnected to use TryRemove with KeyValuePair for atomic reference equality checks, ensuring the correct session is removed during reconnection scenarios (same-channel migration).
Remove unused Maple2.Graphics.Interface project.
Summary by CodeRabbit