perf: prevent import hanging with timeouts and batching
- verifyAndFixInteractionModesCount: paginated DB queries (500/batch) instead of loading all items into memory at once - withFurniDataLock: add 60s chain timeout to prevent deadlocks when a lock holder stalls or crashes - withGamedataLock: same timeout protection for gamedata locks - conversion-pool: add 60s per-job timeout, fall back to main thread - furni/batch: abort signal + post_import progress events + skip post-import steps when client disconnects - furni/batch-regen: abort signal + early exit when disconnected - clone/sync-all: pre-fetch furnidata once per source instead of per item (eliminates 2000+ redundant fetches) - sse-client: add 60s idle timeout to prevent infinite hangs
This commit is contained in:
1 parent
f70d96b81c
commit
7556b1f3fa
5 files changed
+222
-93
No files matched your search
@@ -2,10 +2,12 @@ import { promises as fs } from "node:fs";
|
||||
|
||||
const LOCK_STALE_MS = 60_000;
|
||||
const LOCK_ACQUIRE_TIMEOUT_MS = 30_000;
|
||||
const CHAIN_TIMEOUT_MS = 60_000;
|
||||
|
||||
// In-process serialization, keyed by absolute file path, layered on top of the
|
||||
// on-disk lock so multiple awaiters in the same Node process queue cleanly.
|
||||
const chains = new Map<string, Promise<void>>();
|
||||
const chainTimestamps = new Map<string, number>();
|
||||
|
||||
async function acquireDiskLock(lockPath: string): Promise<void> {
|
||||
const deadline = Date.now() + LOCK_ACQUIRE_TIMEOUT_MS;
|
||||
@@ -34,6 +36,48 @@ async function acquireDiskLock(lockPath: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for the previous chain holder with a timeout.
|
||||
* If the chain holder stalled (e.g. process crash, unhandled rejection),
|
||||
* we break the chain instead of waiting forever.
|
||||
*/
|
||||
async function waitForChain(filePath: string): Promise<void> {
|
||||
const prev = chains.get(filePath);
|
||||
if (!prev) return;
|
||||
|
||||
const timestamp = chainTimestamps.get(filePath) ?? 0;
|
||||
const elapsed = Date.now() - timestamp;
|
||||
|
||||
if (elapsed > CHAIN_TIMEOUT_MS) {
|
||||
// Previous holder took too long — break the chain
|
||||
console.warn(
|
||||
`[gamedata-lock] Breaking stale chain for ${filePath} (held for ${elapsed}ms)`,
|
||||
);
|
||||
chains.delete(filePath);
|
||||
chainTimestamps.delete(filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await Promise.race([
|
||||
prev,
|
||||
new Promise((_, reject) =>
|
||||
setTimeout(
|
||||
() => reject(new Error("chain timeout")),
|
||||
Math.max(1, CHAIN_TIMEOUT_MS - elapsed),
|
||||
),
|
||||
),
|
||||
]);
|
||||
} catch {
|
||||
// Chain holder timed out or crashed — break the chain
|
||||
console.warn(
|
||||
`[gamedata-lock] Chain timeout for ${filePath}, breaking chain`,
|
||||
);
|
||||
chains.delete(filePath);
|
||||
chainTimestamps.delete(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
export async function withGamedataLock<T>(
|
||||
filePath: string,
|
||||
fn: () => Promise<T>,
|
||||
@@ -42,15 +86,20 @@ export async function withGamedataLock<T>(
|
||||
const acquired = new Promise<void>((r) => {
|
||||
release = r;
|
||||
});
|
||||
const prev = chains.get(filePath) ?? Promise.resolve();
|
||||
|
||||
await waitForChain(filePath);
|
||||
|
||||
chains.set(filePath, acquired);
|
||||
await prev;
|
||||
chainTimestamps.set(filePath, Date.now());
|
||||
|
||||
const lockPath = `${filePath}.lock`;
|
||||
await acquireDiskLock(lockPath);
|
||||
try {
|
||||
return await fn();
|
||||
} finally {
|
||||
await fs.unlink(/*turbopackIgnore: true*/ lockPath).catch(() => {});
|
||||
chains.delete(filePath);
|
||||
chainTimestamps.delete(filePath);
|
||||
release();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user