Merge pull request #605 from AtalayaLabs/claude/performance-optimization-analysis-sisndc

ROUND4: Row-path allocs, drive cache, CalDAV parse, PROPFIND emit, N+1 hydration, Azure streaming, faces bound
This commit is contained in:
Dionisio Pozo
2026-07-17 15:56:34 +02:00
committed by GitHub
44 changed files with 5092 additions and 402 deletions
@@ -147,8 +147,12 @@ pub struct AuthApplicationService {
/// request. The short TTL keeps the "role changes apply without token
/// rotation" property within seconds while removing one DB round-trip
/// per request; the known mutation paths (`change_user_role`,
/// `set_user_active`) also invalidate eagerly.
user_flags_cache: Cache<Uuid, UserFlags>,
/// `set_user_active`) also invalidate eagerly. `moka::future` so
/// concurrent misses for one user coalesce into a single DB lookup
/// (`try_get_with` single-flight) — every authenticated request
/// calls this, so each 30 s TTL expiry used to fan out one SELECT
/// per in-flight request of that user.
user_flags_cache: moka::future::Cache<Uuid, UserFlags>,
/// Self-service auth-method allowlist (mirrors
/// `AuthConfig::allowed_auth_methods`). Empty = both methods
/// allowed. Consulted by login / register / magic-link handlers via
@@ -198,7 +202,7 @@ impl AuthApplicationService {
.time_to_live(Duration::from_secs(120))
.build(),
magic_link_repo: None,
user_flags_cache: Cache::builder()
user_flags_cache: moka::future::Cache::builder()
.max_capacity(10_000)
.time_to_live(USER_FLAGS_CACHE_TTL)
.build(),
@@ -1363,7 +1367,7 @@ impl AuthApplicationService {
// Invalidate the flags cache so subsequent per-request guards
// observe the new `is_external=false` without waiting for the
// 30-second TTL. Same pattern as `change_user_role`.
self.user_flags_cache.invalidate(&caller_id);
self.user_flags_cache.invalidate(&caller_id).await;
// Dispatch — home-drive provisioning happens here. Log-and-
// continue: a provisioning failure leaves the row updated and
@@ -1508,12 +1512,20 @@ impl AuthApplicationService {
/// Staleness is bounded by [`USER_FLAGS_CACHE_TTL`]; role and active
/// changes made through this service invalidate the entry eagerly.
pub async fn get_user_flags(&self, user_id: Uuid) -> Result<UserFlags, DomainError> {
if let Some(flags) = self.user_flags_cache.get(&user_id) {
return Ok(flags);
}
let flags = self.user_storage.get_user_flags(user_id).await?;
self.user_flags_cache.insert(user_id, flags);
Ok(flags)
// Single-flight: concurrent misses for the same user coalesce
// into ONE storage lookup; errors are never cached (same herd
// shape ROUND3 fixed for basic-auth, minus the Argon2 cost).
self.user_flags_cache
.try_get_with(user_id, async {
Ok::<_, DomainError>(self.user_storage.get_user_flags(user_id).await?)
})
.await
// try_get_with hands back `Arc<DomainError>` shared by all
// waiters; DomainError isn't Clone, so rebuild a fresh one
// preserving the kind / entity / message.
.map_err(|shared: std::sync::Arc<DomainError>| {
DomainError::new(shared.kind, shared.entity_type, shared.message.clone())
})
}
/// Apply a profile update on behalf of the calling user (PR 24).
@@ -2226,7 +2238,7 @@ impl AuthApplicationService {
self.user_storage
.set_user_active_status(user_id, active)
.await?;
self.user_flags_cache.invalidate(&user_id);
self.user_flags_cache.invalidate(&user_id).await;
Ok(())
}
@@ -2240,7 +2252,7 @@ impl AuthApplicationService {
));
}
self.user_storage.change_role(user_id, role).await?;
self.user_flags_cache.invalidate(&user_id);
self.user_flags_cache.invalidate(&user_id).await;
Ok(())
}
+7 -11
View File
@@ -189,17 +189,13 @@ impl CalendarUseCase for CalendarService {
})
.collect();
// Hydrate DTOs. `get_calendar` misses on trashed / deleted
// calendars — those are dropped from the listing rather than
// erroring, so a lifecycle-race doesn't turn a PROPFIND into
// a 5xx.
let mut out = Vec::with_capacity(calendar_ids.len());
for id in calendar_ids {
if let Ok(dto) = self.calendar_storage.get_calendar(&id.to_string()).await {
out.push(dto);
}
}
Ok(out)
// Hydrate DTOs in ONE `= ANY` round-trip (was one point SELECT
// per accessible calendar — K serial round-trips on every
// CalDAV discovery poll). Missing rows (deleted/trashed race)
// drop out of the result set instead of erroring, so a
// lifecycle-race still doesn't turn a PROPFIND into a 5xx.
let ids: Vec<Uuid> = calendar_ids.into_iter().collect();
self.calendar_storage.get_calendars_by_ids(&ids).await
}
async fn list_public_calendars(
+8 -6
View File
@@ -494,12 +494,14 @@ impl AddressBookUseCase for ContactService {
let mut address_book_map = std::collections::HashMap::new();
for id in book_ids {
// Missing rows (deleted / trashed race) drop out silently
// — matches the calendar-listing carve-out.
if let Ok(Some(book)) = self.contact_storage.get_address_book_by_id(&id).await {
address_book_map.insert(*book.id(), book);
}
// Hydrate in ONE `= ANY` round-trip (was one point SELECT per
// accessible book — K serial round-trips on every CardDAV
// discovery poll). Missing rows (deleted / trashed race) drop
// out of the result set — matches the calendar-listing
// carve-out.
let ids: Vec<Uuid> = book_ids.into_iter().collect();
for book in self.contact_storage.get_address_books_by_ids(&ids).await? {
address_book_map.insert(*book.id(), book);
}
// Public address books surface for every authenticated caller
@@ -263,6 +263,12 @@ impl DriveManagementService {
self.authz
.invalidate_drive_role_cache_for_drive(drive_id)
.await;
// Same freshness contract for the repo's readable-drives cache:
// the subject's drive list changed with this grant.
match subject {
Subject::User(uid) => self.drive_repo.invalidate_readable_for_user(uid).await,
_ => self.drive_repo.invalidate_readable_all(),
}
// D6 §11: canonical `drive.member_added` audit event covers
// every successful membership write (add + role-refresh, since
@@ -335,6 +341,12 @@ impl DriveManagementService {
self.authz
.invalidate_drive_role_cache_for_drive(drive_id)
.await;
// And the repo's readable-drives cache: the drive must vanish
// from the removed subject's list immediately.
match subject {
Subject::User(uid) => self.drive_repo.invalidate_readable_for_user(uid).await,
_ => self.drive_repo.invalidate_readable_all(),
}
// D6 §11: canonical `drive.member_removed` audit event covers
// every successful removal (owner-driven or admin bypass).
+11 -8
View File
@@ -196,15 +196,18 @@ impl MusicUseCase for MusicService {
// only. Owner is a grant like any other in `role_grants`, so we
// filter the aggregated set against the owner_id stamped on
// each row after hydration — cheaper than a second SQL round-trip.
let mut playlists: Vec<PlaylistDto> = Vec::with_capacity(playlist_ids.len());
// Hydrate in ONE `= ANY` round-trip (was one point SELECT per
// accessible playlist). Missing rows (deleted race) drop out of
// the result set silently, as before.
let user_str = user_id.to_string();
for id in playlist_ids.drain() {
if let Ok(Some(p)) = self.storage.get_playlist(&id.to_string()).await
&& (include_shared || p.owner_id == user_str)
{
playlists.push(p);
}
}
let ids: Vec<Uuid> = playlist_ids.drain().collect();
let mut playlists: Vec<PlaylistDto> = self
.storage
.get_playlists_by_ids(&ids)
.await?
.into_iter()
.filter(|p| include_shared || p.owner_id == user_str)
.collect();
if include_public {
let public = self.storage.list_public_playlists(limit, offset).await?;
@@ -44,6 +44,11 @@ pub struct SubjectGroupService {
/// 30 s TTL. Without this, fresh group-mediated drive grants
/// don't appear in `/api/drives` for up to 30 s after `add_member`.
engine: Arc<crate::infrastructure::services::pg_acl_engine::PgAclEngine>,
/// Same freshness contract for the drive repository's per-user
/// readable-drives cache: a membership change on a group that holds
/// drive grants changes every affected user's visible drive list,
/// so the cached lists drop alongside `user_groups_cache`.
drive_repo: Arc<crate::infrastructure::repositories::pg::DrivePgRepository>,
}
impl SubjectGroupService {
@@ -52,12 +57,14 @@ impl SubjectGroupService {
pool: Arc<PgPool>,
user_storage: Arc<UserPgRepository>,
engine: Arc<crate::infrastructure::services::pg_acl_engine::PgAclEngine>,
drive_repo: Arc<crate::infrastructure::repositories::pg::DrivePgRepository>,
) -> Self {
Self {
repo,
pool,
user_storage,
engine,
drive_repo,
}
}
@@ -426,6 +433,7 @@ impl SubjectGroupService {
// call for up to 30 s.
for uid in self.invalidation_targets(member).await? {
self.engine.invalidate_user_groups_cache(uid).await;
self.drive_repo.invalidate_readable_for_user(uid).await;
}
tracing::info!(
@@ -525,6 +533,7 @@ impl SubjectGroupService {
// for up to 30 s, surfacing grants they no longer have.
for uid in self.invalidation_targets(member).await? {
self.engine.invalidate_user_groups_cache(uid).await;
self.drive_repo.invalidate_readable_for_user(uid).await;
}
tracing::info!(
@@ -634,7 +643,9 @@ mod integration_tests {
// future test starts exercising real authz lookups.
let engine =
Arc::new(crate::infrastructure::services::pg_acl_engine::PgAclEngine::new_stub());
SubjectGroupService::new(repo, pool, user_storage, engine)
let drive_repo =
Arc::new(crate::infrastructure::repositories::pg::DrivePgRepository::new(pool.clone()));
SubjectGroupService::new(repo, pool, user_storage, engine, drive_repo)
}
async fn first_admin(pool: &sqlx::PgPool) -> Uuid {