Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

[db-perf] perf(web): coalesce repeated Home unread presence requests (#9581)

teoe0a7caab1963
diff --git a/apps/web/src/lib/queries/auth/user-info.ts b/apps/web/src/lib/queries/auth/user-info.ts
index 6cf49b916b..7e6a08048b 100644
--- a/apps/web/src/lib/queries/auth/user-info.ts
+++ b/apps/web/src/lib/queries/auth/user-info.ts
@@ -7,6 +7,7 @@ import { hasLoginCookie } from '@core/util/cookies';
import { fireAndForget } from '@core/util/fireAndForget';
import { catchToResult, type ResultType, throwOnErr } from '@core/util/result';
import { authServiceClient } from '@service-auth/client';
+import { resetGraphqlPresenceRequests } from '@service-storage/graphql-soup';
import { queryOptions, useQuery } from '@tanstack/solid-query';
import { resetTeamCalendarSession } from '../calendar/team-cache';
import { queryClient, queryPersistence } from '../client';
@@ -74,6 +75,7 @@ export function invalidateAllAfterLogin() {
// Login may replace a session without visiting logout (including native auth).
// Invalidate old display-intent handles before refetching the new identity.
resetGraphqlSoupDoneSession();
+ resetGraphqlPresenceRequests();
resetTeamCalendarSession();
enableUserInfoQuery();
const invalidated = queryClient.invalidateQueries();
diff --git a/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts b/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts
index 821fe5031b..0221e2b578 100644
--- a/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts
+++ b/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts
@@ -124,6 +124,7 @@ import {
waitToRetryGraphqlSoupWebSocket,
} from './graphql-soup-websocket';
import { mailCacheProfile } from './mail-cache-profile';
+import { createPresenceRequestCoalescer } from './presence-request-coalescer';
export { emailThreadSortTs } from './email-thread-sort';
@@ -293,7 +294,22 @@ async function isLegacyProjectionValidationError(
}
}
-export async function dssGraphqlFetch(
+const presenceRequests = createPresenceRequestCoalescer();
+
+/** Drops coalesced presence requests that belong to the previous session. */
+export const resetGraphqlPresenceRequests = presenceRequests.reset;
+
+export function dssGraphqlFetch(
+ input: RequestInfo | URL,
+ init?: RequestInit,
+ queueOwner?: CacheHost
+): Promise<Response> {
+ return presenceRequests.coalesce(init, (coalescedInit) =>
+ sendDssGraphqlRequest(input, coalescedInit, queueOwner)
+ );
+}
+
+async function sendDssGraphqlRequest(
input: RequestInfo | URL,
init?: RequestInit,
queueOwner?: CacheHost
@@ -598,6 +614,7 @@ export function setGraphqlSoupSessionActive(active: boolean): void {
function disposeSessionClients(): void {
cacheSessionGeneration += 1;
+ presenceRequests.reset();
cacheRecovery.reset();
cacheInitializationFailed = false;
const cleanup = cachedCacheCleanup;
diff --git a/apps/web/src/lib/service-clients/service-storage/presence-request-coalescer.test.ts b/apps/web/src/lib/service-clients/service-storage/presence-request-coalescer.test.ts
new file mode 100644
index 0000000000..fc5ef95212
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/presence-request-coalescer.test.ts
@@ -0,0 +1,164 @@
+import { describe, expect, it, vi } from 'vitest';
+import { createPresenceRequestCoalescer } from './presence-request-coalescer';
+
+const presence = (variables = '{}'): RequestInit => ({
+ method: 'POST',
+ body: `{"operationName":"HomeUnreadPresence","query":"query HomeUnreadPresence { x }","variables":${variables}}`,
+});
+
+function harness() {
+ let time = 0;
+ const waits: { at: number; resolve: () => void }[] = [];
+ const { coalesce, reset } = createPresenceRequestCoalescer(
+ 2_000,
+ () => time,
+ (ms) =>
+ new Promise<void>((resolve) => {
+ waits.push({ at: time + ms, resolve });
+ })
+ );
+ const pending: { init: RequestInit | undefined; resolve: () => void }[] = [];
+ const send = vi.fn(
+ (init: RequestInit | undefined) =>
+ new Promise<Response>((resolve) => {
+ pending.push({
+ init,
+ resolve: () => resolve(new Response(`r${pending.length}`)),
+ });
+ })
+ );
+ const advance = async (ms: number) => {
+ time += ms;
+ for (const wait of waits.splice(0)) {
+ if (wait.at <= time) wait.resolve();
+ else waits.push(wait);
+ }
+ await flush();
+ };
+ return { coalesce, reset, send, pending, advance };
+}
+
+const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
+
+describe('createPresenceRequestCoalescer', () => {
+ it('runs one request at a time and shares the next among waiting callers', async () => {
+ const { coalesce, send, pending, advance } = harness();
+
+ const first = coalesce(presence(), send);
+ const joined = [
+ coalesce(presence(), send),
+ coalesce(presence(), send),
+ coalesce(presence(), send),
+ ];
+ expect(send).toHaveBeenCalledTimes(1);
+
+ pending[0].resolve();
+ await expect((await first).text()).resolves.toBe('r1');
+ await flush();
+ // The next request waits out the interval since the first one started.
+ expect(send).toHaveBeenCalledTimes(1);
+
+ await advance(2_000);
+ expect(send).toHaveBeenCalledTimes(2);
+ pending[1].resolve();
+ const bodies = await Promise.all(
+ joined.map(async (response) => (await response).text())
+ );
+ expect(bodies).toEqual(['r2', 'r2', 'r2']);
+ });
+
+ it('sends one request per interval while a tab refetches in a loop', async () => {
+ const { coalesce, send, pending, advance } = harness();
+ const responses: Promise<Response>[] = [];
+ // A tab asking 12 times a second for a minute.
+ for (let tick = 0; tick < 720; tick++) {
+ responses.push(coalesce(presence(), send));
+ await advance(83);
+ for (const request of pending) request.resolve();
+ }
+ await advance(2_000);
+ for (const request of pending) request.resolve();
+ await Promise.all(responses);
+ expect(send.mock.calls.length).toBe(30);
+ });
+
+ it('starts at once after a quiet interval', async () => {
+ const { coalesce, send, pending, advance } = harness();
+ const first = coalesce(presence(), send);
+ pending[0].resolve();
+ await first;
+ await advance(2_000);
+
+ const second = coalesce(presence(), send);
+ expect(send).toHaveBeenCalledTimes(2);
+ pending[1].resolve();
+ await second;
+ });
+
+ it('passes other operations and other variables through', async () => {
+ const { coalesce, send, pending } = harness();
+ const other: RequestInit = {
+ body: '{"operationName":"ChannelListSoup","query":"q"}',
+ };
+ const responses = [
+ coalesce(presence('{"a":1}'), send),
+ coalesce(presence('{"a":2}'), send),
+ coalesce(other, send),
+ coalesce(other, send),
+ ];
+ expect(send).toHaveBeenCalledTimes(4);
+ for (const request of pending) request.resolve();
+ await Promise.all(responses);
+ });
+
+ it('lets a waiting caller abort without cancelling the shared request', async () => {
+ const { coalesce, send, pending, advance } = harness();
+ const first = coalesce(presence(), send);
+ const controller = new AbortController();
+ const aborted = coalesce(
+ { ...presence(), signal: controller.signal },
+ send
+ );
+ const kept = coalesce(presence(), send);
+ controller.abort(new DOMException('aborted', 'AbortError'));
+ await expect(aborted).rejects.toThrow('aborted');
+
+ pending[0].resolve();
+ await first;
+ await advance(2_000);
+ expect(pending[1].init?.signal).toBeUndefined();
+ pending[1].resolve();
+ await expect((await kept).text()).resolves.toBe('r2');
+ });
+
+ it('rejects an already-aborted caller without sending', async () => {
+ const { coalesce, send, pending } = harness();
+ const first = coalesce(presence(), send);
+ const controller = new AbortController();
+ controller.abort(new DOMException('aborted', 'AbortError'));
+ await expect(
+ coalesce({ ...presence(), signal: controller.signal }, send)
+ ).rejects.toThrow('aborted');
+ expect(send).toHaveBeenCalledTimes(1);
+ pending[0].resolve();
+ await first;
+ });
+
+ it('drops a request queued by the previous session', async () => {
+ const { coalesce, reset, send, pending, advance } = harness();
+ const first = coalesce(presence(), send);
+ const queued = coalesce(presence(), send);
+ reset();
+ const next = coalesce(presence(), send);
+ expect(send).toHaveBeenCalledTimes(2);
+
+ pending[0].resolve();
+ pending[1].resolve();
+ const dropped = expect(queued).rejects.toThrow('outlived its session');
+ await first;
+ await next;
+ await advance(2_000);
+ await dropped;
+ expect(send).toHaveBeenCalledTimes(2);
+ });
+});
diff --git a/apps/web/src/lib/service-clients/service-storage/presence-request-coalescer.ts b/apps/web/src/lib/service-clients/service-storage/presence-request-coalescer.ts
new file mode 100644
index 0000000000..0799ede4be
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/presence-request-coalescer.ts
@@ -0,0 +1,126 @@
+/**
+ * Background unread presence is refetched by notification patches, activity
+ * pushes, reconnects and cache recovery. Each trigger is right to ask, but
+ * together they can repeat one request many times a second from a single tab,
+ * and every request runs several replica queries. Identical requests share one
+ * fetch, and a new one starts at most once per interval.
+ */
+const COALESCED_OPERATIONS = ['HomeUnreadPresence'];
+
+export const PRESENCE_REQUEST_INTERVAL_MS = 2_000;
+
+type Slot = {
+ session: number;
+ running?: Promise<Response>;
+ queued?: Promise<Response>;
+ startedAt: number;
+};
+
+type Send = (init: RequestInit | undefined) => Promise<Response>;
+
+function coalescingKey(init: RequestInit | undefined): string | undefined {
+ const body = init?.body;
+ if (typeof body !== 'string') return undefined;
+ return COALESCED_OPERATIONS.some((name) =>
+ body.includes(`"operationName":"${name}"`)
+ )
+ ? body
+ : undefined;
+}
+
+async function abortable<T>(
+ promise: Promise<T>,
+ signal: AbortSignal | null | undefined
+): Promise<T> {
+ if (!signal) return promise;
+ let abort = () => {};
+ const aborted = new Promise<never>((_, reject) => {
+ abort = () => reject(signal.reason);
+ signal.addEventListener('abort', abort, { once: true });
+ });
+ try {
+ return await Promise.race([promise, aborted]);
+ } finally {
+ signal.removeEventListener('abort', abort);
+ }
+}
+
+export function createPresenceRequestCoalescer(
+ intervalMs = PRESENCE_REQUEST_INTERVAL_MS,
+ now: () => number = Date.now,
+ wait: (ms: number) => Promise<void> = (ms) =>
+ new Promise((resolve) => setTimeout(resolve, ms))
+) {
+ let slots = new Map<string, Slot>();
+ let session = 0;
+
+ const start = (slot: Slot, send: Send, init: RequestInit | undefined) => {
+ const startedAt = now();
+ slot.startedAt = startedAt;
+ slot.running = (async () => {
+ try {
+ return await send(init);
+ } finally {
+ if (slot.startedAt === startedAt) slot.running = undefined;
+ }
+ })();
+ return slot.running;
+ };
+
+ const startQueued = async (slot: Slot, send: Send, init: RequestInit) => {
+ try {
+ await slot.running;
+ } catch {
+ // The queued request runs whether or not the previous one failed.
+ }
+ const delay = slot.startedAt + intervalMs - now();
+ if (delay > 0) await wait(delay);
+ slot.queued = undefined;
+ // Credentials are read when the request is sent, so a request queued by
+ // the previous session must not go out under the next one.
+ if (slot.session !== session)
+ throw new Error('presence request outlived its session');
+ return start(slot, send, init);
+ };
+
+ const shared = async (slot: Slot) => {
+ if (!slot.queued) throw new Error('presence request was not queued');
+ return (await slot.queued).clone();
+ };
+
+ const coalesce = (
+ init: RequestInit | undefined,
+ send: Send
+ ): Promise<Response> => {
+ const key = coalescingKey(init);
+ if (key === undefined) return send(init);
+ if (init?.signal?.aborted) return Promise.reject(init.signal.reason);
+ for (const [other, slot] of slots) {
+ if (!slot.running && !slot.queued && now() - slot.startedAt > intervalMs)
+ slots.delete(other);
+ }
+ let slot = slots.get(key);
+ if (!slot) {
+ slot = { session, startedAt: -Infinity };
+ slots.set(key, slot);
+ }
+ if (!slot.running && !slot.queued && now() - slot.startedAt >= intervalMs) {
+ return start(slot, send, init);
+ }
+ if (!slot.queued) {
+ // Shared by every caller that joins, so no single caller's signal may
+ // cancel it; each caller still stops waiting on its own abort.
+ const { signal: _signal, ...sharedInit } = init ?? {};
+ slot.queued = startQueued(slot, send, sharedInit);
+ }
+ return abortable(shared(slot), init?.signal);
+ };
+
+ /** Forgets every request, and drops queued ones, when the session changes. */
+ const reset = () => {
+ session += 1;
+ slots = new Map();
+ };
+
+ return { coalesce, reset };
+}