Files
openclaw/src/test-utils/tracked-temp-dirs.ts
Nachiket Torwekar 53475c21b8 perf: reduce latency across async I/O hot paths
Improves gateway/device-auth/session discovery latency by caching unchanged device-auth reads, deduping session root realpaths, cleaning temp dirs in parallel, and bulk-loading APNs registrations for iOS exec approval delivery.

The maintainer fixup replaces per-device APNs registration reads with a single canonical store snapshot, preserving empty-target skip behavior and requested target ordering while avoiding delayed read failures from the bounded queue path.

Verification:
- node scripts/run-vitest.mjs src/gateway/exec-approval-ios-push.test.ts src/infra/push-apns.store.test.ts src/infra/device-auth-store.test.ts src/config/sessions/targets.test.ts src/test-utils/tracked-temp-dirs.test.ts src/utils/run-with-concurrency.test.ts
- env -u OPENCLAW_TESTBOX pnpm check:changed
- env -u OPENCLAW_TESTBOX pnpm test:changed
- pnpm exec oxfmt --check --threads=1 on touched files
- autoreview clean: no accepted/actionable findings
- before/after 500-device APNs discovery benchmark: p50 189.89 ms -> 2.03 ms
- GitHub Actions CI 26588266247 green after rerun; Real behavior proof 26588276271 green

Co-authored-by: Nachiket Torwekar <nachiket.torwekar@gmail.com>
2026-05-28 17:47:26 +01:00

74 lines
2.1 KiB
TypeScript

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
export function createTrackedTempDirs() {
const prefixRoots = new Map<string, { root: string; nextIndex: number }>();
const pendingPrefixRoots = new Map<string, Promise<{ root: string; nextIndex: number }>>();
const cleanupRoots = new Set<string>();
let globalDirIndex = 0;
const ensurePrefixRoot = async (prefix: string) => {
const cached = prefixRoots.get(prefix);
if (cached) {
return cached;
}
const pending = pendingPrefixRoots.get(prefix);
if (pending) {
return await pending;
}
const create = (async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
const state = { root, nextIndex: 0 };
prefixRoots.set(prefix, state);
cleanupRoots.add(root);
return state;
})();
pendingPrefixRoots.set(prefix, create);
try {
return await create;
} finally {
pendingPrefixRoots.delete(prefix);
}
};
return {
async make(prefix: string): Promise<string> {
const state = await ensurePrefixRoot(prefix);
const dir = path.join(state.root, `dir-${String(globalDirIndex)}`);
state.nextIndex += 1;
globalDirIndex += 1;
await fs.mkdir(dir, { recursive: true });
return dir;
},
async cleanup(): Promise<void> {
const roots = [...cleanupRoots];
pendingPrefixRoots.clear();
const dirlists = await Promise.all(
roots.map((dir) =>
fs.readdir(dir).catch((err: unknown) => {
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
return [];
}
throw err;
}),
),
);
await Promise.all(
roots.flatMap((dir, i) =>
dirlists[i].map((entry) =>
fs.rm(path.join(dir, entry), { recursive: true, force: true }),
),
),
);
for (const dir of roots) {
for (const state of prefixRoots.values()) {
if (state.root === dir) {
state.nextIndex = 0;
}
}
}
},
};
}