Under 10-12 concurrent users the app queued behind itself: every SSE game-event/profile-update made each client run router.refresh(), i.e. a full layout + page render at ~15-25 queries, and a mission completion emitted one XP event per attendee row. Measured against production, no single query is slow and the DB is small, so this targets render count and per-render query count instead of indexes. Fewer renders per event: - GameTickRealtime: 250ms coalescing window -> 1s, hidden tabs skip the render and refresh once when shown again. - Delete OperationsRealtimeRefresher (5 routes) and the duplicate useGameTick refreshers in ProductionPanel/SlotsPanel; the global subscriber already refreshes those routes. - Presence heartbeats no longer broadcast (was O(N^2) per channel every 15s for a lastSeen no surface renders). - NavCore links opt out of prefetch: the layout is force-dynamic with no loading.tsx, so ~35 prefetched links per tab were 35 server renders. Fewer queries per render: - Qualification flags go through one cached, single-flight profile read instead of one query per auth check; invalidated on profile writes. - Dedupe DIVISION_ADMIN_QUALIFICATIONS (5 identical lookups -> 1). - Anchor the permission cache on globalThis so route bundles share it. - Minigame registry: real TTL cache plus invalidation on write. - New cachedConfig TTL cache for the shims/game-rules globals, with invalidation hooks; bank-account existence check stops hydrating. - pg pool max 10 -> 20 (node-postgres default throttled concurrent renders). Hot-path per-item work: - Contribution grant sweep: one status write per batch, one bucket write and one name lookup per user, one existence query per source collection instead of ~5 queries per entry. - Notifications bell: coalesced refetch, hidden-tab skip; mark-all-read filters by the actor instead of scanning 100 foreign unread rows. - Polling loops (shipments, ledger, mission reminders, chip bank) pause while the tab is hidden.
61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { broadcast } from "./bus";
|
|
|
|
export interface PresenceParticipant {
|
|
id: number;
|
|
name: string;
|
|
lastSeen: string;
|
|
}
|
|
|
|
interface PresenceRecord extends PresenceParticipant {
|
|
expiresAt: number;
|
|
}
|
|
|
|
export const PRESENCE_TTL_MS = 45_000;
|
|
const channels = new Map<string, Map<number, PresenceRecord>>();
|
|
|
|
function prune(channel: string, now = Date.now()): void {
|
|
const entries = channels.get(channel);
|
|
if (!entries) return;
|
|
for (const [id, entry] of entries) {
|
|
if (entry.expiresAt <= now) {
|
|
entries.delete(id);
|
|
broadcast("presence", { channel, action: "leave", participantId: id });
|
|
}
|
|
}
|
|
if (entries.size === 0) channels.delete(channel);
|
|
}
|
|
|
|
export function getPresence(channel: string): PresenceParticipant[] {
|
|
prune(channel);
|
|
return [...(channels.get(channel)?.values() ?? [])].map(({ id, name, lastSeen }) => ({ id, name, lastSeen }));
|
|
}
|
|
|
|
export function touchPresence(channel: string, participant: Omit<PresenceParticipant, "lastSeen">): void {
|
|
const now = Date.now();
|
|
prune(channel, now);
|
|
let entries = channels.get(channel);
|
|
if (!entries) {
|
|
entries = new Map();
|
|
channels.set(channel, entries);
|
|
}
|
|
const wasPresent = entries.has(participant.id);
|
|
const record = { ...participant, lastSeen: new Date(now).toISOString(), expiresAt: now + PRESENCE_TTL_MS };
|
|
entries.set(participant.id, record);
|
|
// A heartbeat only extends the TTL. Broadcasting an "update" to every
|
|
// subscriber on every 15s heartbeat made presence O(N^2) per channel (12
|
|
// players = 144 SSE messages per 15s) for a `lastSeen` no surface renders.
|
|
// Joins and leaves still broadcast, which is all the participant list needs.
|
|
if (wasPresent) return;
|
|
broadcast("presence", {
|
|
channel,
|
|
action: "join",
|
|
participant: { id: record.id, name: record.name, lastSeen: record.lastSeen },
|
|
});
|
|
}
|
|
|
|
export function removePresence(channel: string, id: number): void {
|
|
const entries = channels.get(channel);
|
|
if (!entries?.delete(id)) return;
|
|
if (entries.size === 0) channels.delete(channel);
|
|
broadcast("presence", { channel, action: "leave", participantId: id });
|
|
}
|