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 lines24252627282912 unmodified lines42434445464710 unmodified lines585960616263646528 unmodified lines94959697989910010128 unmodified lines13013113213313413513613725 unmodified lines16316416516616716816917023 unmodified lines// In-memory store for WebRTC: ProjectId -> (UserId -> Presence Info)private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, WebRTCPresence>> Presence = new();public SyncController(ISyncService syncService, IGraphService graphService){_syncService = syncService;12 unmodified linesprivate string CurrentUserEmail => User.FindFirst(ClaimTypes.Email)?.Value ?? "unknown@user.com";private string CurrentUserName => User.FindFirst("name")?.Value ?? User.FindFirst(ClaimTypes.Name)?.Value ?? "Collaborator";[HttpPost("{userId:guid}")]public async Task<ActionResult<SyncResponseDto>> Sync(Guid userId, [FromBody] SyncRequestDto request){10 unmodified linesif (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();var projectPresence = Presence.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, WebRTCPresence>());28 unmodified linesif (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 (!Presence.TryGetValue(projectId, out var projectPresence)){28 unmodified linesif (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();var message = new WebRTCSignal{25 unmodified linesif (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();var projectQueues = Signals.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, ConcurrentQueue<WebRTCSignal>>());var userQueue = projectQueues.GetOrAdd(userId.ToString(), _ => new ConcurrentQueue<WebRTCSignal>());23 unmodified lines24252627282930313212 unmodified lines4546474849505152535455565758596061626364656667686910 unmodified lines8081828384858628 unmodified lines11511611711811912012128 unmodified lines15015115215315415515625 unmodified lines18218318418518618718823 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 timeprivate static readonly ConcurrentDictionary<string, DateTime> AccessCache = new();public SyncController(ISyncService syncService, IGraphService graphService){_syncService = syncService;12 unmodified linesprivate 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 secondsAccessCache[cacheKey] = DateTime.UtcNow.AddSeconds(30);return true;}[HttpPost("{userId:guid}")]public async Task<ActionResult<SyncResponseDto>> Sync(Guid userId, [FromBody] SyncRequestDto request){10 unmodified linesif (userId == Guid.Empty) return Unauthorized();if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();var projectPresence = Presence.GetOrAdd(projectId, _ => new ConcurrentDictionary<string, WebRTCPresence>());28 unmodified linesif (userId == Guid.Empty) return Unauthorized();if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();if (!Presence.TryGetValue(projectId, out var projectPresence)){28 unmodified linesif (userId == Guid.Empty) return Unauthorized();if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");if (!await HasProjectAccessAsync(graphId, userId)) return Forbid();var message = new WebRTCSignal{25 unmodified linesif (userId == Guid.Empty) return Unauthorized();if (!Guid.TryParse(projectId, out var graphId)) return BadRequest("Invalid project ID");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