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 lines1819202122232425116 unmodified lines14214314414514614714814915015115215315410 unmodified lines16516616716816917017117217317417517617717817918018118218318418518618718818919019119217 unmodified linesprivate readonly ISyncService _syncService;private readonly IGraphService _graphService;// In-memory store for WebRTC: ProjectId -> List of signalsprivate static readonly 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 linesTimestamp = DateTime.UtcNow};var queue = Signals.GetOrAdd(projectId, _ => new ConcurrentQueue<WebRTCSignal>());queue.Enqueue(message);// Cap the queue at 100 entries per projectwhile (queue.Count > 100){queue.TryDequeue(out _);}return Ok(new { success = true });10 unmodified linesvar graph = await _graphService.GetByIdAsync(graphId, userId);if (graph is null) return Forbid();var queue = Signals.GetOrAdd(projectId, _ => new ConcurrentQueue<WebRTCSignal>());var mySignals = new List<WebRTCSignal>();var tempQueue = new List<WebRTCSignal>();while (queue.TryDequeue(out var msg)){// If it is for me and is not older than 1 minuteif (msg.ReceiverId == userId.ToString() && (DateTime.UtcNow - msg.Timestamp).TotalSeconds < 60){mySignals.Add(msg);}else if ((DateTime.UtcNow - msg.Timestamp).TotalSeconds < 60){// Requeue other people's fresh messagestempQueue.Add(msg);}}foreach (var msg in tempQueue){queue.Enqueue(msg);}return Ok(mySignals);17 unmodified lines1819202122232425116 unmodified lines14214314414514614714814915015115215315415510 unmodified lines16616716816917017117217317417517617717817918018118217 unmodified linesprivate readonly ISyncService _syncService;private readonly IGraphService _graphService;// 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 linesTimestamp = DateTime.UtcNow};var projectQueues = Signals.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>());var userQueue = projectQueues.GetOrAdd(dto.ReceiverId, _ => new ConcurrentQueue<WebRTCSignal>());userQueue.Enqueue(message);// Cap the queue at 100 entries per recipientwhile (userQueue.Count > 100){userQueue.TryDequeue(out _);}return Ok(new { success = true });10 unmodified linesvar graph = await _graphService.GetByIdAsync(graphId, userId);if (graph is null) return Forbid();var projectQueues = Signals.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>());var userQueue = projectQueues.GetOrAdd(userId.ToString(), _ => new ConcurrentQueue<WebRTCSignal>());var mySignals = new List<WebRTCSignal>();while (userQueue.TryDequeue(out var msg)){// If it is not older than 1 minuteif ((DateTime.UtcNow - msg.Timestamp).TotalSeconds < 60){mySignals.Add(msg);}}return Ok(mySignals);
FlowAPI/Controllers/SyncController.cs+13-23