mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 19:36:04 +00:00
fix(studio): close idle thumbnail pages so an idle Studio runs no composition
Kept-loaded thumbnail pages stayed open for the life of the server, each running a live composition, so an idle Studio cost server CPU and memory that main does not. A page now closes after 10 s unused, and a page is closed only after the frame still running on it finishes.
This commit is contained in:
@@ -56,4 +56,45 @@ describe("createThumbnailPages", () => {
|
||||
await thumbnails.withPage(browser, "/preview", "v2", load, async () => null);
|
||||
expect(load).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it("closes a page nobody has used for a while, so an idle Studio runs no composition", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const { browser, pages } = fakeBrowser();
|
||||
const thumbnails = createThumbnailPages(2, 10_000);
|
||||
const load = vi.fn(async () => {});
|
||||
|
||||
await thumbnails.withPage(browser, "/preview", "v1", load, async () => null);
|
||||
await vi.advanceTimersByTimeAsync(9_999);
|
||||
expect(pages[0]?.close).not.toHaveBeenCalled();
|
||||
await vi.advanceTimersByTimeAsync(1);
|
||||
expect(pages[0]?.close).toHaveBeenCalled();
|
||||
|
||||
await thumbnails.withPage(browser, "/preview", "v1", load, async () => null);
|
||||
expect(load).toHaveBeenCalledTimes(2);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("never closes a page under a frame that is still being taken", async () => {
|
||||
const { browser, pages } = fakeBrowser();
|
||||
const thumbnails = createThumbnailPages(1);
|
||||
const load = vi.fn(async () => {});
|
||||
let finish: () => void = () => {};
|
||||
const slow = thumbnails.withPage(
|
||||
browser,
|
||||
"/a",
|
||||
"v1",
|
||||
load,
|
||||
() => new Promise<void>((r) => (finish = r)),
|
||||
);
|
||||
await vi.waitFor(() => expect(pages).toHaveLength(1));
|
||||
|
||||
await thumbnails.withPage(browser, "/b", "v1", load, async () => null);
|
||||
expect(pages[0]?.close).not.toHaveBeenCalled();
|
||||
finish();
|
||||
await slow;
|
||||
await vi.waitFor(() => expect(pages[0]?.close).toHaveBeenCalled());
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,17 +5,25 @@ interface LoadedPage {
|
||||
version: string;
|
||||
page: Promise<Page>;
|
||||
queue: Promise<unknown>;
|
||||
idleTimer?: ReturnType<typeof setTimeout>;
|
||||
}
|
||||
|
||||
/** One loaded preview page per document URL: a thumbnail is a seek and a screenshot on it, not a
|
||||
* fresh load of the whole composition. A new `version` (project content) reloads the page, and
|
||||
* calls on one page run one at a time so their seeks never interleave. */
|
||||
export function createThumbnailPages(maxPages = 2) {
|
||||
* fresh load of the whole composition. A new `version` (project content) reloads the page, calls on
|
||||
* one page run one at a time so their seeks never interleave, and a page unused for `idleMs` closes
|
||||
* so an idle Studio keeps no composition running. */
|
||||
export function createThumbnailPages(maxPages = 2, idleMs = 10_000) {
|
||||
const pages = new Map<string, LoadedPage>();
|
||||
const drop = (url: string) => {
|
||||
const entry = pages.get(url);
|
||||
if (!entry) return;
|
||||
pages.delete(url);
|
||||
void entry?.page.then((page) => page.close()).catch(() => {});
|
||||
clearTimeout(entry.idleTimer);
|
||||
// After any frame still running on it.
|
||||
void entry.queue
|
||||
.then(() => entry.page)
|
||||
.then((page) => page.close())
|
||||
.catch(() => {});
|
||||
};
|
||||
return {
|
||||
async withPage<T>(
|
||||
@@ -40,6 +48,7 @@ export function createThumbnailPages(maxPages = 2) {
|
||||
while (pages.size > maxPages) drop(pages.keys().next().value!);
|
||||
}
|
||||
const current = entry;
|
||||
clearTimeout(current.idleTimer);
|
||||
const run = current.queue.then(async () => use(await current.page));
|
||||
current.queue = run.catch(() => {});
|
||||
try {
|
||||
@@ -47,6 +56,14 @@ export function createThumbnailPages(maxPages = 2) {
|
||||
} catch (error) {
|
||||
if (pages.get(url) === current) drop(url);
|
||||
throw error;
|
||||
} finally {
|
||||
if (pages.get(url) === current) {
|
||||
clearTimeout(current.idleTimer);
|
||||
current.idleTimer = setTimeout(() => {
|
||||
if (pages.get(url) === current) drop(url);
|
||||
}, idleMs);
|
||||
current.idleTimer.unref?.();
|
||||
}
|
||||
}
|
||||
},
|
||||
closeAll(): void {
|
||||
|
||||
Reference in New Issue
Block a user