fix(usage): refresh limits when the tab opens (#11928)

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Bilal BakrandJulius Marminge authored and GitHub committed 2026-09-15 15:46:42 -07:00
1 parent f4600d77dd
commit 87a12b53fd
8 files changed
+380 -36

No files matched your search

@@ -6,11 +6,11 @@ import {
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
import { afterEach, describe, expect, it } from "vite-plus/test";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
import type { EnvironmentPresentation } from "../connection/presentation.ts";
import { EnvironmentRpcUnavailableError } from "../rpc/client.ts";
import { refreshUsage } from "./usage.ts";
import { refreshUsage, refreshUsageLimits } from "./usage.ts";
const input = {
sinceDay: UsageDay.make("2026-09-05"),
@@ -182,3 +182,46 @@ describe("manual usage refresh", () => {
unmount();
});
});
describe("limits refresh cooldown", () => {
it("joins manual calls and gates automatic refreshes after success or failure", async () => {
const clock = vi.spyOn(Date, "now").mockReturnValue(1_000);
try {
for (const fails of [false, true]) {
const id = EnvironmentId.make(`limits-${fails}`);
const pending = Promise.withResolvers<string>();
const refresh = vi.fn(() => pending.promise);
const first = refreshUsageLimits(id, refresh, true);
await refreshUsageLimits(id, refresh, true);
const manual = refreshUsageLimits(id, refresh);
const settled = vi.fn();
void manual.then(settled, settled);
expect(settled).not.toHaveBeenCalled();
expect(refresh).toHaveBeenCalledTimes(1);
if (fails) {
const firstFailure = expect(first).rejects.toThrow("unavailable");
const manualFailure = expect(manual).rejects.toThrow("unavailable");
pending.reject(new Error("unavailable"));
await Promise.all([firstFailure, manualFailure]);
} else {
pending.resolve("quota");
expect(await first).toBe("quota");
expect(await manual).toBe("quota");
}
expect(settled).toHaveBeenCalledTimes(1);
const next = vi.fn(async () => undefined);
clock.mockReturnValue(300_999);
await refreshUsageLimits(id, next, true);
expect(next).not.toHaveBeenCalled();
clock.mockReturnValue(301_000);
await refreshUsageLimits(id, next, true);
expect(next).toHaveBeenCalledTimes(1);
await refreshUsageLimits(id, next);
expect(next).toHaveBeenCalledTimes(2);
clock.mockReturnValue(1_000);
}
} finally {
clock.mockRestore();
}
});
});
@@ -9,6 +9,33 @@ import type { createServerEnvironmentAtoms } from "./server.ts";
const isEnvironmentRpcUnavailable = Schema.is(EnvironmentRpcUnavailableError);
const limitsRefreshAfter = new Map<EnvironmentId, number>();
const limitsRefreshes = new Map<EnvironmentId, Promise<unknown>>();
export async function refreshUsageLimits<A>(
environmentId: EnvironmentId,
refresh: () => Promise<A>,
automatic = false,
): Promise<A | undefined> {
const pending = limitsRefreshes.get(environmentId);
if (pending !== undefined) {
// Manual refresh waits for the current check; automatic refresh does not repeat it.
return automatic ? undefined : ((await pending) as A);
}
const refreshAfter = limitsRefreshAfter.get(environmentId) ?? 0;
// @effect-diagnostics-next-line globalDate:off
if (automatic && Date.now() < refreshAfter) return;
const current = Promise.resolve()
.then(refresh)
.finally(() => {
limitsRefreshes.delete(environmentId);
// @effect-diagnostics-next-line globalDate:off
limitsRefreshAfter.set(environmentId, Date.now() + 5 * 60_000);
});
limitsRefreshes.set(environmentId, current);
return await current;
}
/** Refresh pricing, then await each selected environment's rescan while it remains connected. */
export async function refreshUsage({
registry,