feat: dynamic timeouts — scale with image size, respect PROCESSING_TIMEOUT_S

Create timeout.ts utility for dynamic timeout computation.
Replace hardcoded timeouts across the stack:
- tool-factory worker: 30s → dynamic based on megapixels
- Python bridge default: 300s → 600s (or env override)
- background-removal: fixed → dynamic based on image size
- OCR: fixed 600s → dynamic based on image size
- seam-carving: 120s → dynamic based on image size
- ExifTool: 30s → 60s
- HEIC converter: 30s → 120s
- SQLite busy_timeout: 5s → 10s
This commit is contained in:
ashim-hq
2026-04-20 21:46:07 +08:00
parent 00041d535d
commit be254f9ca6
9 changed files with 59 additions and 12 deletions
+5 -2
View File
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
import { readFile, unlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import sharp from "sharp";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface RemoveBackgroundOptions {
@@ -21,8 +22,10 @@ export async function removeBackground(
await writeFile(inputPath, inputBuffer);
try {
// BiRefNet models need longer timeout (up to 10 min for first load)
const timeout = options.model?.startsWith("birefnet") ? 600000 : 300000;
const meta = await sharp(inputBuffer).metadata();
const megapixels = ((meta.width ?? 0) * (meta.height ?? 0)) / 1_000_000;
const baseTimeout = options.model?.startsWith("birefnet") ? 600000 : 300000;
const timeout = Math.max(baseTimeout, megapixels * 30 * 1000);
const { stdout } = await runPythonWithProgress(
"remove_bg.py",
[inputPath, outputPath, JSON.stringify(options)],