perf(query-cache): invalidate namespaces in parallel (Q-6.3-8)

TanStack 5 invalidateQueries resolves only after the active refetches
finish. The coordinator awaited each namespace in a for loop, so one
invalidation (and lease.release(), which waits on it) took the sum of
every namespace's refetch time. The namespaces now run through
Promise.allSettled; each rejected one is still reported once as
"invalidate".

Verified: new test holds the first namespace's refetch open and failed
RED (second invalidateQueries never started); it now passes and checks
that a failure is reported once. All lane gates pass except the known
removal-fixture test; provider-guardian-transaction timed out once under
load and passed on rerun.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:41:45 +09:00
co-authored by Claude Opus 5.5
parent c7052ef09a
commit fcd37a9d16
2 changed files with 44 additions and 7 deletions
@@ -76,16 +76,19 @@ export function createTanStackCacheCoordinator(
namespaces.set(queryNamespaceIdentityKey(namespace), namespace);
}
}
for (const namespace of namespaces.values()) {
try {
await dependencies.queryClient.invalidateQueries({
// Q-6.3-8. invalidateQueries resolves only after the active refetches
// finish, so the namespaces run side by side instead of one after another.
const results = await Promise.allSettled(
[...namespaces.values()].map(async (namespace) =>
dependencies.queryClient.invalidateQueries({
queryKey: createQueryInvalidationPrefix(namespace),
exact: false,
refetchType: "active",
});
} catch {
report("invalidate");
}
}),
),
);
for (const result of results) {
if (result.status === "rejected") report("invalidate");
}
}
@@ -212,6 +212,40 @@ describe("TanStack cross-context cache coordinator", () => {
expect(invalidate).toHaveBeenCalledTimes(3);
});
it("starts every namespace refetch at once and reports each failure", async () => {
// Q-6.3-8. invalidateQueries resolves only after active refetches finish,
// so awaiting namespaces one by one added their refetch times together.
const client = createClient();
const pending: Array<() => void> = [];
const invalidate = vi
.spyOn(client, "invalidateQueries")
.mockImplementationOnce(
() => new Promise<void>((resolve) => pending.push(resolve)),
)
.mockImplementationOnce(() => Promise.reject(new Error("refetch failed")));
const record = vi.fn();
const coordinator = createTanStackCacheCoordinator({
queryClient: client,
invalidationIndex: invalidationIndex(),
topicVersions: topicVersions(),
diagnostics: { record } as never,
});
let settled = false;
const done = coordinator.invalidate([TOPIC_A]).then(() => {
settled = true;
});
await vi.waitFor(() => expect(invalidate).toHaveBeenCalledTimes(2));
expect(settled).toBe(false);
pending.forEach((resolve) => resolve());
await done;
expect(record).toHaveBeenCalledOnce();
expect(record.mock.calls[0]?.[0]).toMatchObject({
context: { operation: "invalidate" },
});
});
it("coalesces remote hints while a local mutation lease is held", async () => {
const client = createClient();
const harness = crossContextHarness();