89 lines
3.3 KiB
TypeScript
89 lines
3.3 KiB
TypeScript
import type { EnvironmentId, UsageSummaryInput } from "@t3tools/contracts";
|
|
import * as Schema from "effect/Schema";
|
|
import type { AtomRegistry } from "effect/unstable/reactivity";
|
|
|
|
import { EnvironmentRpcUnavailableError } from "../rpc/client.ts";
|
|
import type { createEnvironmentPresentationAtoms } from "./presentation.ts";
|
|
import { executeAtomQuery, runAtomCommand, squashAtomCommandFailure } from "./runtime.ts";
|
|
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,
|
|
server,
|
|
presentations,
|
|
environmentIds,
|
|
input,
|
|
}: {
|
|
registry: AtomRegistry.AtomRegistry;
|
|
server: Pick<
|
|
ReturnType<typeof createServerEnvironmentAtoms>,
|
|
"usageSummary" | "refreshUsageRates"
|
|
>;
|
|
presentations: Pick<ReturnType<typeof createEnvironmentPresentationAtoms>, "presentationAtom">;
|
|
environmentIds: readonly EnvironmentId[];
|
|
input: UsageSummaryInput;
|
|
}): Promise<void> {
|
|
await Promise.all(
|
|
environmentIds.map(async (environmentId) => {
|
|
const query = server.usageSummary({ environmentId, input });
|
|
const presentation = presentations.presentationAtom(environmentId);
|
|
const controller = new AbortController();
|
|
const abortWhenDisconnected = () => {
|
|
if (registry.get(presentation)?.connection.phase !== "connected") controller.abort();
|
|
};
|
|
const unsubscribe = registry.subscribe(presentation, abortWhenDisconnected);
|
|
abortWhenDisconnected();
|
|
try {
|
|
const ratesResult = await runAtomCommand(
|
|
registry,
|
|
server.refreshUsageRates,
|
|
{ environmentId, input: {} },
|
|
{ reportFailure: false },
|
|
);
|
|
const sessionUnavailable =
|
|
ratesResult._tag === "Failure" &&
|
|
isEnvironmentRpcUnavailable(squashAtomCommandFailure(ratesResult));
|
|
// Invalidate even on failure so reconnects cannot reuse the old summary.
|
|
registry.refresh(query);
|
|
if (sessionUnavailable || controller.signal.aborted) return;
|
|
await executeAtomQuery(registry, query, {
|
|
reportFailure: false,
|
|
signal: controller.signal,
|
|
});
|
|
} finally {
|
|
unsubscribe();
|
|
}
|
|
}),
|
|
);
|
|
}
|