Files
SnapOtter/apps/api/src/lib/timeout.ts
T
ashim-hq be254f9ca6 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
2026-04-20 21:46:07 +08:00

27 lines
772 B
TypeScript

import { env } from "../config.js";
type ToolCategory = "sharp" | "ai_cpu" | "ai_gpu" | "external" | "python";
const TIMEOUT_RATES: Record<ToolCategory, number> = {
sharp: 2,
ai_cpu: 30,
ai_gpu: 5,
external: 10,
python: 15,
};
export function computeTimeout(megapixels: number, category: ToolCategory, fileCount = 1): number {
if (env.PROCESSING_TIMEOUT_S > 0) {
return env.PROCESSING_TIMEOUT_S * 1000;
}
const perFile = Math.max(60_000, megapixels * TIMEOUT_RATES[category] * 1000);
return perFile * fileCount;
}
export function computeExternalToolTimeout(megapixels: number): number {
if (env.PROCESSING_TIMEOUT_S > 0) {
return env.PROCESSING_TIMEOUT_S * 1000;
}
return Math.max(60_000, megapixels * TIMEOUT_RATES.external * 1000);
}