Spaces:
Running
Running
Commit ·
e83ad7d
1
Parent(s): c65f7a2
Fix real-time sync: refactor signaling to use a thread-safe nested ConcurrentDictionary with recipient-specific queues
Browse files17 unmodified lines18192021222122232425116 unmodified lines14214314414514514614614714814814914915015115115215315415510 unmodified lines16616716816816917017117217117217317317417517617517617717817918018118218318418518618718818918018118217 unmodified lines private readonly ISyncService _syncService; private readonly IGraphService _graphService;
// In-memory store for WebRTC: ProjectId -> List of signals private static readonly ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>> Signals = new(); // In-memory store for WebRTC: ProjectId -> (ReceiverId -> Queue of signals) private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>> Signals = new(); // In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info) private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();116 unmodified lines Timestamp = DateTime.UtcNow };
var projectQueues = Signals.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>()); var queue = Signals.GetOrAdd(projectId, _ => new ConcurrentQueue<WebRTCSignal>()); queue.Enqueue(message); var userQueue = projectQueues.GetOrAdd(dto.ReceiverId, _ => new ConcurrentQueue<WebRTCSignal>()); userQueue.Enqueue(message);
// Cap the queue at 100 entries per project while (queue.Count > 100) // Cap the queue at 100 entries per recipient while (userQueue.Count > 100) { queue.TryDequeue(out _); userQueue.TryDequeue(out _); }
return Ok(new { success = true });10 unmodified lines var graph = await _graphService.GetByIdAsync(graphId, userId); if (graph is null) return Forbid();
var queue = Signals.GetOrAdd(projectId, _ => new ConcurrentQueue<WebRTCSignal>()); var projectQueues = Signals.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>()); var userQueue = projectQueues.GetOrAdd(userId.ToString(), _ => new ConcurrentQueue<WebRTCSignal>()); var mySignals = new List<WebRTCSignal>(); var tempQueue = new List<WebRTCSignal>();
while (queue.TryDequeue(out var msg)) while (userQueue.TryDequeue(out var msg)) { // If it is for me and is not older than 1 minute if (msg.ReceiverId == userId.ToString() && (DateTime.UtcNow - msg.Timestamp).TotalSeconds < 60) // If it is not older than 1 minute if ((DateTime.UtcNow - msg.Timestamp).TotalSeconds < 60) { mySignals.Add(msg); } else if ((DateTime.UtcNow - msg.Timestamp).TotalSeconds < 60) { // Requeue other people's fresh messages tempQueue.Add(msg); } }
foreach (var msg in tempQueue) { queue.Enqueue(msg); }
return Ok(mySignals);FlowAPI/Controllers/SyncController.cs+13-23