Files
MikrotikManager/backend/src/modules/mcp/state-file-cache.test.ts
T
DenozordecandClaude Opus 5.5 af4203d9f1 fix(mcp): ускорить сервисы WireGuard - собственная проверка раз на этап, файлы состояния через stat
Профиль приёмки создания WireGuard (81 с): ~3000 свежих проверок прав MCP, ~20000
проверок источника прав и ~63000 чтений файлов состояния с диска (24 с) - всё перед
каждым чтением роутера.
- сервисы WireGuard (создание интерфейса и пира, изменение, импорт): собственная
  проверка (ключи, защиты, допуск) раз на этап, чтения переиспользуют доказательство
  моложе 2 с; перед каждой нативной записью права MCP и защиты по-прежнему
  проверяются заново (executeGuardedConfigurationWrite), у создания интерфейса и
  собственная проверка перед отправкой свежая;
- assertVerifiedConfigurationReadAccess - проверка этапа только для чтений;
  assertVerifiedConfigurationAccess остаётся свежей (записи и публикации);
- access-generation и отметки отзыва портала проверяются через stat и перечитываются
  только при изменении файла (замена видна сразу); ключ шифрования читается как раньше.

Приёмка создания WireGuard 81 -> 14 с отдельно; шарды WireGuard 224/198 -> 69/66 с;
весь набор зелёный, 149 строк результатов на месте.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 13:59:39 +07:00

23 lines
1.3 KiB
TypeScript

import assert from "node:assert/strict"
import { mkdtemp, rename, rm, writeFile } from "node:fs/promises"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { readStateFile } from "./state-file-cache.js"
const directory = await mkdtemp(join(tmpdir(), "mcp-state-cache-")), file = join(directory, "access-generation")
try {
await writeFile(file, Buffer.alloc(32, 1))
assert.deepEqual(await readStateFile(file), Buffer.alloc(32, 1))
const copy = await readStateFile(file); copy.fill(9)
assert.deepEqual(await readStateFile(file), Buffer.alloc(32, 1), "callers get a copy; zeroizing it never touches the cache")
// Rotation by atomic replace is seen on the very next call.
await writeFile(file + ".tmp", Buffer.alloc(32, 2)); await rename(file + ".tmp", file)
assert.deepEqual(await readStateFile(file), Buffer.alloc(32, 2))
// In-place overwrite is seen as well.
await writeFile(file, Buffer.alloc(32, 3))
assert.deepEqual(await readStateFile(file), Buffer.alloc(32, 3))
await rm(file)
await assert.rejects(readStateFile(file), (error: NodeJS.ErrnoException) => error.code === "ENOENT")
} finally { await rm(directory, { recursive: true, force: true }) }
console.log("MCP state file cache: copy per call, atomic replace/in-place overwrite seen at once, removal is ENOENT")