|
| 1 | +/** |
| 2 | + * 双车道派发(控制优先级):业务洪峰下心跳依然可达。 |
| 3 | + * |
| 4 | + * 回归背景:业务/控制共队列时,业务 handler 洪峰打满队列 → 心跳被 |
| 5 | + * fail-fast 拒绝 → Agent 判定会话死亡 → 过载升级为连接雪崩。 |
| 6 | + * 双车道后控制消息(心跳/注册/drain)走独立车道,永不拒绝。 |
| 7 | + */ |
| 8 | + |
| 9 | +import { TCPTransport, TCPTransportConfig } from "./tcp_transport"; |
| 10 | +import { |
| 11 | + MSG_INVOKE_REQUEST, |
| 12 | + MSG_INVOKE_RESPONSE, |
| 13 | + MSG_PROVIDER_HEARTBEAT_REQUEST, |
| 14 | + MSG_PROVIDER_HEARTBEAT_RESPONSE, |
| 15 | + MSG_PROVIDER_DRAIN_REQUEST, |
| 16 | + MSG_PROVIDER_CONNECT_REQUEST, |
| 17 | + MSG_REGISTER_REQUEST, |
| 18 | + MSG_REGISTER_CAPABILITIES_REQ, |
| 19 | + isControlRequest, |
| 20 | +} from "./protocol"; |
| 21 | + |
| 22 | +const VERSION = 0x01; |
| 23 | + |
| 24 | +function encodeMessage(msgId: number, reqId: number, body: Buffer): Buffer { |
| 25 | + const header = Buffer.alloc(8); |
| 26 | + header.writeUInt8(VERSION, 0); |
| 27 | + header.writeUIntBE(msgId, 1, 3); |
| 28 | + header.writeUInt32BE(reqId, 4); |
| 29 | + return Buffer.concat([header, body]); |
| 30 | +} |
| 31 | + |
| 32 | +function frame(payload: Buffer): Buffer { |
| 33 | + const prefix = Buffer.alloc(4); |
| 34 | + prefix.writeUInt32BE(payload.length, 0); |
| 35 | + return Buffer.concat([prefix, payload]); |
| 36 | +} |
| 37 | + |
| 38 | +describe("dual-lane inbound dispatch", () => { |
| 39 | + it("classifies control vs business messages", () => { |
| 40 | + for (const id of [ |
| 41 | + MSG_PROVIDER_HEARTBEAT_REQUEST, |
| 42 | + MSG_PROVIDER_CONNECT_REQUEST, |
| 43 | + MSG_PROVIDER_DRAIN_REQUEST, |
| 44 | + MSG_REGISTER_REQUEST, |
| 45 | + MSG_REGISTER_CAPABILITIES_REQ, |
| 46 | + ]) { |
| 47 | + expect(isControlRequest(id)).toBe(true); |
| 48 | + } |
| 49 | + for (const id of [ |
| 50 | + MSG_INVOKE_REQUEST, |
| 51 | + MSG_INVOKE_RESPONSE, |
| 52 | + 0x030103, // start task |
| 53 | + 0x030107, // cancel task |
| 54 | + ]) { |
| 55 | + expect(isControlRequest(id)).toBe(false); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + it("answers heartbeats while the business lane is saturated", async () => { |
| 60 | + const server = await startFakeServer(); |
| 61 | + const { address, sockets, send, nextFrame, close } = server; |
| 62 | + |
| 63 | + const t = new TCPTransport({ |
| 64 | + address, |
| 65 | + inboundWorkers: 1, |
| 66 | + }); |
| 67 | + let businessStarted = false; |
| 68 | + t.setHandler((msgId, _reqId, _body) => { |
| 69 | + if (msgId === MSG_PROVIDER_HEARTBEAT_REQUEST) { |
| 70 | + return Buffer.from("pong"); |
| 71 | + } |
| 72 | + businessStarted = true; |
| 73 | + return new Promise<Buffer>((resolve) => { |
| 74 | + // 占住唯一业务 worker 3 秒:队列随即打满 |
| 75 | + setTimeout(() => resolve(Buffer.alloc(0)), 3000); |
| 76 | + }); |
| 77 | + }); |
| 78 | + await t.connect(); |
| 79 | + |
| 80 | + try { |
| 81 | + // 打满业务车道(worker 1 + 队列 4 → 连发 6 个业务请求) |
| 82 | + for (let reqId = 101; reqId <= 106; reqId++) { |
| 83 | + send(MSG_INVOKE_REQUEST, reqId, Buffer.from("x")); |
| 84 | + } |
| 85 | + await waitFor(() => businessStarted); |
| 86 | + |
| 87 | + // 洪峰中的心跳:控制车道应立即处理 |
| 88 | + const start = Date.now(); |
| 89 | + send(MSG_PROVIDER_HEARTBEAT_REQUEST, 900, Buffer.alloc(0)); |
| 90 | + const resp = await nextFrame( |
| 91 | + (f) => f.msgId === MSG_PROVIDER_HEARTBEAT_RESPONSE, |
| 92 | + 2000, |
| 93 | + ); |
| 94 | + expect(resp).not.toBeNull(); |
| 95 | + expect(resp?.reqId).toBe(900); |
| 96 | + expect(resp?.body.toString()).toBe("pong"); |
| 97 | + expect(Date.now() - start).toBeLessThan(2000); |
| 98 | + |
| 99 | + // 对照:业务队列满 → 新业务请求被 fail-fast 回空帧 |
| 100 | + send(MSG_INVOKE_REQUEST, 901, Buffer.from("x")); |
| 101 | + const busy = await nextFrame( |
| 102 | + (f) => f.msgId === MSG_INVOKE_RESPONSE && f.reqId === 901, |
| 103 | + 2000, |
| 104 | + ); |
| 105 | + expect(busy).not.toBeNull(); |
| 106 | + expect(busy?.body.length).toBe(0); |
| 107 | + } finally { |
| 108 | + t.close(); |
| 109 | + close(); |
| 110 | + } |
| 111 | + expect(sockets.length).toBeGreaterThanOrEqual(0); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +import { createServer, Server, Socket } from "net"; |
| 116 | + |
| 117 | +interface DecodedFrame { |
| 118 | + msgId: number; |
| 119 | + reqId: number; |
| 120 | + body: Buffer; |
| 121 | +} |
| 122 | + |
| 123 | +type FakeServer = { |
| 124 | + address: string; |
| 125 | + sockets: Socket[]; |
| 126 | + send: (msgId: number, reqId: number, body: Buffer) => void; |
| 127 | + nextFrame: ( |
| 128 | + predicate: (f: DecodedFrame) => boolean, |
| 129 | + timeoutMs: number, |
| 130 | + ) => Promise<DecodedFrame | null>; |
| 131 | + close: () => Promise<void>; |
| 132 | +}; |
| 133 | + |
| 134 | +function startFakeServer(): Promise<FakeServer> { |
| 135 | + return new Promise((resolve) => { |
| 136 | + const server: Server = createServer(); |
| 137 | + const sockets: Socket[] = []; |
| 138 | + const frames: DecodedFrame[] = []; |
| 139 | + const waiters: Array<{ |
| 140 | + predicate: (f: DecodedFrame) => boolean; |
| 141 | + resolve: (f: DecodedFrame | null) => void; |
| 142 | + timer: ReturnType<typeof setTimeout>; |
| 143 | + }> = []; |
| 144 | + let buffer = Buffer.alloc(0); |
| 145 | + let address = ""; |
| 146 | + |
| 147 | + server.on("connection", (socket) => { |
| 148 | + sockets.push(socket); |
| 149 | + socket.on("data", (chunk: Buffer) => { |
| 150 | + buffer = Buffer.concat([buffer, chunk]); |
| 151 | + for (;;) { |
| 152 | + if (buffer.length < 4) return; |
| 153 | + const size = buffer.readUInt32BE(0); |
| 154 | + if (buffer.length < 4 + size) return; |
| 155 | + const payload = buffer.subarray(4, 4 + size); |
| 156 | + buffer = buffer.subarray(4 + size); |
| 157 | + if (payload.length >= 8) { |
| 158 | + const decoded: DecodedFrame = { |
| 159 | + msgId: payload.readUIntBE(1, 3), |
| 160 | + reqId: payload.readUInt32BE(4), |
| 161 | + body: Buffer.from(payload.subarray(8)), |
| 162 | + }; |
| 163 | + const idx = waiters.findIndex((w) => w.predicate(decoded)); |
| 164 | + if (idx >= 0) { |
| 165 | + const [w] = waiters.splice(idx, 1); |
| 166 | + clearTimeout(w.timer); |
| 167 | + w.resolve(decoded); |
| 168 | + } else { |
| 169 | + frames.push(decoded); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + server.listen(0, "127.0.0.1", () => { |
| 177 | + const addr = server.address(); |
| 178 | + if (addr && typeof addr === "object") { |
| 179 | + address = `127.0.0.1:${addr.port}`; |
| 180 | + } |
| 181 | + resolve({ |
| 182 | + address, |
| 183 | + sockets, |
| 184 | + send: (msgId, reqId, body) => { |
| 185 | + const raw = frame(encodeMessage(msgId, reqId, body)); |
| 186 | + for (const s of sockets) s.write(raw); |
| 187 | + }, |
| 188 | + nextFrame: (predicate, timeoutMs) => { |
| 189 | + const idx = frames.findIndex(predicate); |
| 190 | + if (idx >= 0) { |
| 191 | + const [f] = frames.splice(idx, 1); |
| 192 | + return Promise.resolve(f); |
| 193 | + } |
| 194 | + return new Promise((resolve) => { |
| 195 | + const timer = setTimeout(() => { |
| 196 | + const i = waiters.findIndex((w) => w.resolve === resolve); |
| 197 | + if (i >= 0) waiters.splice(i, 1); |
| 198 | + resolve(null); |
| 199 | + }, timeoutMs); |
| 200 | + waiters.push({ predicate, resolve, timer }); |
| 201 | + }); |
| 202 | + }, |
| 203 | + close: () => |
| 204 | + new Promise((done) => { |
| 205 | + for (const s of sockets) s.destroy(); |
| 206 | + server.close(() => done()); |
| 207 | + }), |
| 208 | + }); |
| 209 | + }); |
| 210 | + }); |
| 211 | +} |
| 212 | + |
| 213 | +function waitFor(cond: () => boolean, timeoutMs = 2000): Promise<void> { |
| 214 | + const start = Date.now(); |
| 215 | + return new Promise((resolve, reject) => { |
| 216 | + const tick = () => { |
| 217 | + if (cond()) return resolve(); |
| 218 | + if (Date.now() - start > timeoutMs) { |
| 219 | + return reject(new Error("condition not met in time")); |
| 220 | + } |
| 221 | + setTimeout(tick, 10); |
| 222 | + }; |
| 223 | + tick(); |
| 224 | + }); |
| 225 | +} |
0 commit comments