danylokhodus commited on
Commit
cee8b77
·
1 Parent(s): e63bd5d

Optimize: add thread-safe in-memory AccessCache for project authorization checks to completely avoid database hits on every high-frequency poll request

Browse files
FlowAPI/Controllers/SyncController.cs CHANGED
@@ -24,6 +24,9 @@ namespace FlowAPI.Controllers
24
  // In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info)
25
  private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();
26
 
 
 
 
27
  public SyncController(ISyncService syncService, IGraphService graphService)
28
  {
29
  _syncService = syncService;
@@ -42,6 +45,25 @@ namespace FlowAPI.Controllers
42
  private string CurrentUserEmail => User.FindFirst(ClaimTypes.Email)?.Value ?? "unknown@user.com";
43
  private string CurrentUserName => User.FindFirst("name")?.Value ?? User.FindFirst(ClaimTypes.Name)?.Value ?? "Collaborator";
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  [HttpPost("{userId:guid}")]
46
  public async Task<ActionResult<SyncResponseDto>> Sync(Guid userId, [FromBody] SyncRequestDto request)
47
  {
@@ -58,8 +80,7 @@ namespace FlowAPI.Controllers
58
  if (userId == Guid.Empty) return Unauthorized();
59
 
60
  if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
61
- var graph = await _graphService.GetByIdAsync(graphId, userId);
62
- if (graph is null) return Forbid();
63
 
64
  var projectPresence = Presence.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, WebRTCPresence>());
65
 
@@ -94,8 +115,7 @@ namespace FlowAPI.Controllers
94
  if (userId == Guid.Empty) return Unauthorized();
95
 
96
  if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
97
- var graph = await _graphService.GetByIdAsync(graphId, userId);
98
- if (graph is null) return Forbid();
99
 
100
  if (!Presence.TryGetValue(projectId, out var projectPresence))
101
  {
@@ -130,8 +150,7 @@ namespace FlowAPI.Controllers
130
  if (userId == Guid.Empty) return Unauthorized();
131
 
132
  if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
133
- var graph = await _graphService.GetByIdAsync(graphId, userId);
134
- if (graph is null) return Forbid();
135
 
136
  var message = new WebRTCSignal
137
  {
@@ -163,8 +182,7 @@ namespace FlowAPI.Controllers
163
  if (userId == Guid.Empty) return Unauthorized();
164
 
165
  if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
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>());
 
24
  // In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info)
25
  private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();
26
 
27
+ // In-memory cache for fast-path project access check: (ProjectId + "_" + UserId) -> Expiration time
28
+ private static readonly ConcurrentDictionary<string, DateTime> AccessCache = new();
29
+
30
  public SyncController(ISyncService syncService, IGraphService graphService)
31
  {
32
  _syncService = syncService;
 
45
  private string CurrentUserEmail => User.FindFirst(ClaimTypes.Email)?.Value ?? "unknown@user.com";
46
  private string CurrentUserName => User.FindFirst("name")?.Value ?? User.FindFirst(ClaimTypes.Name)?.Value ?? "Collaborator";
47
 
48
+ private async Task<bool> HasProjectAccessAsync(Guid graphId, Guid userId)
49
+ {
50
+ var cacheKey = $"{graphId}_{userId}";
51
+ if (AccessCache.TryGetValue(cacheKey, out var expiry) && expiry > DateTime.UtcNow)
52
+ {
53
+ return true;
54
+ }
55
+
56
+ var graph = await _graphService.GetByIdAsync(graphId, userId);
57
+ if (graph is null)
58
+ {
59
+ return false;
60
+ }
61
+
62
+ // Cache authorization for 30 seconds
63
+ AccessCache[cacheKey] = DateTime.UtcNow.AddSeconds(30);
64
+ return true;
65
+ }
66
+
67
  [HttpPost("{userId:guid}")]
68
  public async Task<ActionResult<SyncResponseDto>> Sync(Guid userId, [FromBody] SyncRequestDto request)
69
  {
 
80
  if (userId == Guid.Empty) return Unauthorized();
81
 
82
  if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
83
+ if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();
 
84
 
85
  var projectPresence = Presence.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, WebRTCPresence>());
86
 
 
115
  if (userId == Guid.Empty) return Unauthorized();
116
 
117
  if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
118
+ if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();
 
119
 
120
  if (!Presence.TryGetValue(projectId, out var projectPresence))
121
  {
 
150
  if (userId == Guid.Empty) return Unauthorized();
151
 
152
  if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
153
+ if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();
 
154
 
155
  var message = new WebRTCSignal
156
  {
 
182
  if (userId == Guid.Empty) return Unauthorized();
183
 
184
  if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");
185
+ if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();
 
186
 
187
  var projectQueues = Signals.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>());
188
  var userQueue = projectQueues.GetOrAdd(userId.ToString(), _ => new ConcurrentQueue<WebRTCSignal>());