Commit 55bb1ee
committed
Scalability hardening: secure API keys, fix O(N²) realtime, cache auth token, reduce polling
CRITICAL-1 — Move PAGEINDEX and GROQ keys server-side
- Create supabase/functions/api-page-index edge function to proxy all
PageIndex and Groq calls, reading keys from Deno env (never in bundle)
- Rewrite pageIndexService.ts to call the edge function via Supabase auth;
remove VITE_PAGEINDEX_API_KEY and VITE_GROQ_API_KEY from frontend
HIGH-1 — Scope active_sessions Realtime channel to current user
- Add filter `user_id=eq.${user.id}` to postgres_changes subscription
- Prevents O(N²) fan-out where 1M users receive every other user's events
- Also add fetchActiveSessions to the cleanup interval so global count
stays fresh via periodic polling when other users' sessions change
HIGH-2 — Paginate initial call_history fetch
- Add .limit(50) to the Supabase query; prevents loading 10–50 MB of JSON
into browser memory for users with large call histories
HIGH-3 — Cache Supabase auth token in api.ts interceptor
- Module-level token + expiry cache; skips getSession() when token is fresh
- Invalidated immediately via onAuthStateChange; re-fetched only when
<60 s from expiry — eliminates 1M auth reads/s at peak load
MEDIUM-1 — Remove 30 s KnowledgeBase polling
- setInterval(testConnection, 30000) fired 33 K req/s at 1M users
- Replaced with single on-mount check; button remains for manual re-test
MEDIUM-2 — Throttle mousemove handlers via requestAnimationFrame
- MousePosition.tsx and useMouseGradient.ts both fired setState on every
raw mousemove (60+ Hz); wrapped in rAF to cap at display refresh rate
MEDIUM-3 — Add jitter to token refresh interval
- Fixed 60 s interval caused thundering herd (16 K Supabase auth hits/min)
- Added Math.random() * 30_000 ms jitter to spread load across time
https://claude.ai/code/session_01SGdxNUC1TVMDtW73TZbxjW1 parent 97b8c67 commit 55bb1ee
File tree
7 files changed
+272
-123
lines changed- src
- components
- DemoCall
- Landing/Particles
- config
- contexts
- hooks
- services
- supabase/functions/api-page-index
7 files changed
+272
-123
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
202 | | - | |
203 | | - | |
204 | 202 | | |
205 | 203 | | |
206 | 204 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
17 | 21 | | |
18 | 22 | | |
19 | 23 | | |
20 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
21 | 28 | | |
22 | 29 | | |
23 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
20 | 44 | | |
21 | 45 | | |
22 | 46 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 47 | + | |
| 48 | + | |
27 | 49 | | |
28 | 50 | | |
29 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
379 | 379 | | |
380 | 380 | | |
381 | 381 | | |
382 | | - | |
| 382 | + | |
| 383 | + | |
383 | 384 | | |
384 | 385 | | |
385 | 386 | | |
| |||
582 | 583 | | |
583 | 584 | | |
584 | 585 | | |
585 | | - | |
| 586 | + | |
586 | 587 | | |
587 | | - | |
| 588 | + | |
588 | 589 | | |
589 | 590 | | |
590 | 591 | | |
| |||
597 | 598 | | |
598 | 599 | | |
599 | 600 | | |
600 | | - | |
| 601 | + | |
601 | 602 | | |
602 | 603 | | |
603 | | - | |
| 604 | + | |
604 | 605 | | |
605 | 606 | | |
606 | 607 | | |
| |||
817 | 818 | | |
818 | 819 | | |
819 | 820 | | |
820 | | - | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
821 | 824 | | |
822 | 825 | | |
823 | 826 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | | - | |
14 | | - | |
15 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
19 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
20 | 27 | | |
21 | 28 | | |
22 | 29 | | |
| |||
0 commit comments