mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-15 08:03:46 +00:00
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:
parent
1ac2dec220
commit
32a48131b5
1 changed files with 18 additions and 3 deletions
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue