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 files
FlowAPI/Controllers/SyncController.cs
CHANGED
|
@@ -18,8 +18,8 @@ namespace FlowAPI.Controllers
|
|
| 18 |
private readonly ISyncService _syncService;
|
| 19 |
private readonly IGraphService _graphService;
|
| 20 |
|
| 21 |
-
// In-memory store for WebRTC: ProjectId ->
|
| 22 |
-
private static readonly ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>> Signals = new();
|
| 23 |
|
| 24 |
// In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info)
|
| 25 |
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();
|
|
@@ -142,13 +142,14 @@ namespace FlowAPI.Controllers
|
|
| 142 |
Timestamp = DateTime.UtcNow
|
| 143 |
};
|
| 144 |
|
| 145 |
-
var
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
-
// Cap the queue at 100 entries per
|
| 149 |
-
while (
|
| 150 |
{
|
| 151 |
-
|
| 152 |
}
|
| 153 |
|
| 154 |
return Ok(new { success = true });
|
|
@@ -165,28 +166,17 @@ namespace FlowAPI.Controllers
|
|
| 165 |
var graph = await _graphService.GetByIdAsync(graphId, userId);
|
| 166 |
if (graph is null) return Forbid();
|
| 167 |
|
| 168 |
-
var
|
|
|
|
| 169 |
|
| 170 |
var mySignals = new List<WebRTCSignal>();
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
while (queue.TryDequeue(out var msg))
|
| 174 |
{
|
| 175 |
-
// If it is
|
| 176 |
-
if (
|
| 177 |
{
|
| 178 |
mySignals.Add(msg);
|
| 179 |
}
|
| 180 |
-
else if ((DateTime.UtcNow - msg.Timestamp).TotalSeconds < 60)
|
| 181 |
-
{
|
| 182 |
-
// Requeue other people's fresh messages
|
| 183 |
-
tempQueue.Add(msg);
|
| 184 |
-
}
|
| 185 |
-
}
|
| 186 |
-
|
| 187 |
-
foreach (var msg in tempQueue)
|
| 188 |
-
{
|
| 189 |
-
queue.Enqueue(msg);
|
| 190 |
}
|
| 191 |
|
| 192 |
return Ok(mySignals);
|
|
|
|
| 18 |
private readonly ISyncService _syncService;
|
| 19 |
private readonly IGraphService _graphService;
|
| 20 |
|
| 21 |
+
// In-memory store for WebRTC: ProjectId -> (ReceiverId -> Queue of signals)
|
| 22 |
+
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>> Signals = new();
|
| 23 |
|
| 24 |
// In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info)
|
| 25 |
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();
|
|
|
|
| 142 |
Timestamp = DateTime.UtcNow
|
| 143 |
};
|
| 144 |
|
| 145 |
+
var projectQueues = Signals.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>());
|
| 146 |
+
var userQueue = projectQueues.GetOrAdd(dto.ReceiverId, _ => new ConcurrentQueue<WebRTCSignal>());
|
| 147 |
+
userQueue.Enqueue(message);
|
| 148 |
|
| 149 |
+
// Cap the queue at 100 entries per recipient
|
| 150 |
+
while (userQueue.Count > 100)
|
| 151 |
{
|
| 152 |
+
userQueue.TryDequeue(out _);
|
| 153 |
}
|
| 154 |
|
| 155 |
return Ok(new { success = true });
|
|
|
|
| 166 |
var graph = await _graphService.GetByIdAsync(graphId, userId);
|
| 167 |
if (graph is null) return Forbid();
|
| 168 |
|
| 169 |
+
var projectQueues = Signals.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>());
|
| 170 |
+
var userQueue = projectQueues.GetOrAdd(userId.ToString(), _ => new ConcurrentQueue<WebRTCSignal>());
|
| 171 |
|
| 172 |
var mySignals = new List<WebRTCSignal>();
|
| 173 |
+
while (userQueue.TryDequeue(out var msg))
|
|
|
|
|
|
|
| 174 |
{
|
| 175 |
+
// If it is not older than 1 minute
|
| 176 |
+
if ((DateTime.UtcNow - msg.Timestamp).TotalSeconds < 60)
|
| 177 |
{
|
| 178 |
mySignals.Add(msg);
|
| 179 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
}
|
| 181 |
|
| 182 |
return Ok(mySignals);
|