Клиент RouterOS открывал новое TCP-соединение на каждый запрос, а одна запись MCP делает 100–470 запросов. Замер на CHR: последовательный GET 105 -> 54 мс (CHR1) и 215 -> 114 мс (CHR2). Свободное соединение закрывается через 2 с простоя (RouterOS держит его 30 с), предел 2 сессии на роутер сохранён. Co-Authored-By: Claude Opus 5.5 <[email protected]>
23 lines
1.4 KiB
TypeScript
23 lines
1.4 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { createServer } from "node:http"
|
|
import { setTimeout as delay } from "node:timers/promises"
|
|
import { MikrotikClient, ROS_IDLE_SOCKET_MS } from "./mikrotik.js"
|
|
|
|
let connections = 0, closedSockets = 0
|
|
const server = createServer((_req, res) => { res.writeHead(200, { "content-type": "application/json" }); res.end("[]") })
|
|
server.keepAliveTimeout = 30000 // RouterOS keeps idle REST connections for 30 s
|
|
server.on("connection", socket => { connections++; socket.on("close", () => closedSockets++) })
|
|
await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve))
|
|
const client = new MikrotikClient({ host: "127.0.0.1", port: (server.address() as { port: number }).port, useSsl: false, verifySsl: false, username: "fixture", password: "fixture-only" })
|
|
try {
|
|
for (let i = 0; i < 5; i++) assert.deepEqual(await client.get("/ip/address"), [])
|
|
assert.equal(connections, 1, "a burst of sequential reads reuses one connection")
|
|
await delay(ROS_IDLE_SOCKET_MS + 500)
|
|
assert.equal(closedSockets, 1, "the client drops an idle connection long before RouterOS would")
|
|
assert.deepEqual(await client.get("/ip/address"), [])
|
|
assert.equal(connections, 2)
|
|
} finally {
|
|
server.closeAllConnections(); await new Promise<void>(resolve => server.close(() => resolve()))
|
|
}
|
|
console.log("RouterOS REST: sequential reads share a connection; idle connection closed after", ROS_IDLE_SOCKET_MS, "ms")
|