Spaces:
Running
Running
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 files23 unmodified lines24252627282930313212 unmodified lines4546474849505152535455565758596061626364656667686910 unmodified lines80818261628384858628 unmodified lines115116117979811811912012128 unmodified lines15015115213313415315415515625 unmodified lines18218318416616718518618718823 unmodified lines // In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info) private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();
// In-memory cache for fast-path project access check: (ProjectId + "_" + UserId) -> Expiration time private static readonly ConcurrentDictionary<string, DateTime> AccessCache = new();
public SyncController(ISyncService syncService, IGraphService graphService) { _syncService = syncService;12 unmodified lines private string CurrentUserEmail => User.FindFirst(ClaimTypes.Email)?.Value ?? "unknown@user.com"; private string CurrentUserName => User.FindFirst("name")?.Value ?? User.FindFirst(ClaimTypes.Name)?.Value ?? "Collaborator";
private async Task<bool> HasProjectAccessAsync(Guid graphId, Guid userId) { var cacheKey = $"{graphId}_{userId}"; if (AccessCache.TryGetValue(cacheKey, out var expiry) && expiry > DateTime.UtcNow) { return true; }
var graph = await _graphService.GetByIdAsync(graphId, userId); if (graph is null) { return false; }
// Cache authorization for 30 seconds AccessCache[cacheKey] = DateTime.UtcNow.AddSeconds(30); return true; }
[HttpPost("{userId:guid}")] public async Task<ActionResult<SyncResponseDto>> Sync(Guid userId, [FromBody] SyncRequestDto request) {10 unmodified lines if (userId == Guid.Empty) return Unauthorized();
if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID"); var graph = await _graphService.GetByIdAsync(graphId, userId); if (graph is null) return Forbid(); if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();
var projectPresence = Presence.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, WebRTCPresence>()); 28 unmodified lines if (userId == Guid.Empty) return Unauthorized();
if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID"); var graph = await _graphService.GetByIdAsync(graphId, userId); if (graph is null) return Forbid(); if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();
if (!Presence.TryGetValue(projectId, out var projectPresence)) {28 unmodified lines if (userId == Guid.Empty) return Unauthorized();
if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID"); var graph = await _graphService.GetByIdAsync(graphId, userId); if (graph is null) return Forbid(); if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();
var message = new WebRTCSignal {25 unmodified lines if (userId == Guid.Empty) return Unauthorized();
if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID"); var graph = await _graphService.GetByIdAsync(graphId, userId); if (graph is null) return Forbid(); if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();
var projectQueues = Signals.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>()); var userQueue = projectQueues.GetOrAdd(userId.ToString(), _ => new ConcurrentQueue<WebRTCSignal>());FlowAPI/Controllers/SyncController.cs+26-8