Cache app workspace actor handle across requests

Every request was calling getOrCreate on the Rivet engine API
to resolve the workspace actor, even though it's always the same
actor. Cache the handle and invalidate on error so retries
re-resolve. This eliminates redundant cross-region round-trips
to api.rivet.dev on every request.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Nathan Flurry 2026-03-12 22:16:48 -07:00
parent 1ac2dec220
commit 32a48131b5

View file

@ -118,15 +118,30 @@ export async function startBackend(options: BackendStartOptions = {}): Promise<v
}),
);
const appWorkspace = async () =>
await withRetries(
let cachedAppWorkspace: any | null = null;
const appWorkspace = async () => {
if (cachedAppWorkspace) return cachedAppWorkspace;
const handle = await withRetries(
async () =>
await actorClient.workspace.getOrCreate(workspaceKey(APP_SHELL_WORKSPACE_ID), {
createWithInput: APP_SHELL_WORKSPACE_ID,
}),
);
cachedAppWorkspace = handle;
return handle;
};
const appWorkspaceAction = async <T>(run: (workspace: any) => Promise<T>): Promise<T> => await withRetries(async () => await run(await appWorkspace()));
const appWorkspaceAction = async <T>(run: (workspace: any) => Promise<T>): Promise<T> =>
await withRetries(async () => {
try {
return await run(await appWorkspace());
} catch (error) {
// Invalidate cache on connection/actor errors so next retry re-resolves
cachedAppWorkspace = null;
throw error;
}
});
const resolveSessionId = async (c: any): Promise<string> => {
const requested = c.req.header("x-foundry-session");