mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-04 07:43:36 +01:00
* perf(auth): six copies of "who is reading", each an uncached round-trip getCurrentUserId was defined six times across services - timeline queries, timeline processors, groups, loans, projects, and the auth layer. Every copy called supabase.auth.getUser(), which is a NETWORK call: it validates the token against /auth/v1/user. So a single page asked the server who the reader was over and over, and the cache I added yesterday covered only four of the six importers. On the timeline that lookup also sat on the critical path. Measured on a cold load: 2429 -> 3061 get_user_timeline_feed 632ms 3105 -> 3351 auth/v1/user 246ms <- depends on nothing 3367 -> 3595 reactions x3 228ms <- waits on the one above The identity lookup ran after the feed returned, and then the reaction queries waited on it in turn, though it depends on neither. Changes: - One definition, in the auth layer, caching the in-flight promise so concurrent callers collapse onto one request. Safe as module state only because this module talks exclusively to the browser client; a per-request server client would hand one request's user to the next, and the one caller that passes its own client (groups) keeps its uncached path. - The cache is dropped on any auth state change, registered lazily on first use rather than at import - this module is imported very widely and an import-time subscription is a side effect every importer pays for. - getUserFeed warms the id alongside the feed request instead of after it, and asks for the total count concurrently rather than after enrichment. - check:one-current-user fails the build if a seventh copy appears. Wired into verify. A test caught a real behaviour change while writing this: getUser() catches its own errors and reports them in `error` rather than throwing, so "could not ask" arrived looking like "nobody is signed in" and would have been cached as signed-out for the rest of the page. Fixed the code, not the test. Mutation-proved: a seventh definition -> red; the owner losing its definition -> red; both green after restore; and the gate is reached through npm, not just when run by hand. Full suite: 261 suites, 2524 passed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012dpTLxh5GJWeWTF1UEvcD5 * perf(timeline): warm the reader id at the door every feed read goes through My previous commit warmed the reader's id inside getUserFeed. The timeline page calls getEnrichedUserFeed. So the fix shipped to production and did nothing: /auth/v1/user still started at 3552ms, after the feed returned at 3520, with the three reaction queries queued behind it. Moved to the timeline service facade, which every read goes through - including getEventById, getReplies, searchPosts and getThreadPosts, which have the same ordering problem on thread pages. One rule in one place rather than a call repeated inside each query function, and no longer dependent on me picking the right one. The test asserts the ORDER, not that the warm happens: a test that only checked "it was called" would pass with the call left at the end, which is the bug. It runs over every read the facade exposes and fails if a new one is added without warming. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012dpTLxh5GJWeWTF1UEvcD5 --------- Co-authored-by: Georgy Butaev <41178744+g-but@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
73 lines
3.0 KiB
JavaScript
73 lines
3.0 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* "Who is reading?" gets exactly one answer, from one place.
|
|
*
|
|
* Six copies of `getCurrentUserId` had accumulated across services — timeline
|
|
* queries, timeline processors, groups, loans, projects, and the auth layer —
|
|
* each one an uncached network call to /auth/v1/user. A single timeline load
|
|
* asked the server who the reader was repeatedly, and on the critical path:
|
|
* the lookup ran after the feed returned and gated the reaction queries.
|
|
*
|
|
* They were collapsed onto src/services/supabase/auth/session.ts, which caches
|
|
* the in-flight promise. This gate exists so the seventh copy is a red build
|
|
* rather than another silent round-trip: a duplicate is easy to reintroduce,
|
|
* costs nothing visible, and nothing else would ever catch it.
|
|
*
|
|
* Definitions are what this counts. Re-exports (`export { getCurrentUserId }
|
|
* from ...`) are the intended way to expose it from a service's own module.
|
|
*/
|
|
|
|
import { readFileSync } from 'node:fs';
|
|
import { execSync } from 'node:child_process';
|
|
|
|
const OWNER = 'src/services/supabase/auth/session.ts';
|
|
|
|
/**
|
|
* A caller may legitimately pass its own Supabase client — a per-request server
|
|
* client, where a module-level cache would hand one request's user to the next.
|
|
* Those wrappers take a client parameter and delegate when there isn't one.
|
|
*/
|
|
const DELEGATING_WRAPPERS = new Set(['src/services/groups/utils/helpers.ts']);
|
|
|
|
const files = execSync('git ls-files "src/**/*.ts" "src/**/*.tsx"', { encoding: 'utf8' })
|
|
.split('\n')
|
|
.filter(Boolean);
|
|
|
|
const defines = [];
|
|
for (const file of files) {
|
|
const body = readFileSync(file, 'utf8');
|
|
// A definition, not a re-export or an import.
|
|
if (/export\s+(async\s+)?function\s+getCurrentUserId\b/.test(body)) {
|
|
defines.push(file);
|
|
}
|
|
}
|
|
|
|
const unexpected = defines.filter(f => f !== OWNER && !DELEGATING_WRAPPERS.has(f));
|
|
|
|
if (!defines.includes(OWNER)) {
|
|
console.error(`✗ ${OWNER} no longer defines getCurrentUserId.`);
|
|
console.error(' That file is the single definition. Move it back, or update this gate');
|
|
console.error(' deliberately — do not let the definition drift somewhere unnoticed.');
|
|
process.exit(1);
|
|
}
|
|
|
|
if (unexpected.length > 0) {
|
|
console.error('✗ getCurrentUserId is defined in more than one place:');
|
|
for (const f of unexpected) {
|
|
console.error(` - ${f}`);
|
|
}
|
|
console.error('');
|
|
console.error(`Import or re-export it from ${OWNER} instead:`);
|
|
console.error(" export { getCurrentUserId } from '@/services/supabase/auth/session';");
|
|
console.error('');
|
|
console.error('Every copy is an uncached round-trip to /auth/v1/user. Six of them once');
|
|
console.error('shared one page load. If your caller must pass its own Supabase client,');
|
|
console.error('add it to DELEGATING_WRAPPERS in this script and delegate when it has none.');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(
|
|
`check:one-current-user passed — getCurrentUserId defined once (${OWNER})` +
|
|
(DELEGATING_WRAPPERS.size ? `, ${DELEGATING_WRAPPERS.size} delegating wrapper(s)` : '')
|
|
);
|