From 3bc690d3fb1a465cac3807fce02505befe4e6eec Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Wed, 4 Nov 2026 20:34:12 +0900 Subject: [PATCH] =?UTF-8?q?test(realtime):=20WebSocket=20=EA=B5=AC?= =?UTF-8?q?=EB=8F=85=EB=B3=84=20=EC=B4=88=EB=8B=B9=20event=20=EC=83=81?= =?UTF-8?q?=ED=95=9C=EC=9D=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A1=9C=20?= =?UTF-8?q?=EA=B3=A0=EC=A0=95=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제 WebSocket 연결은 구독마다 1초 창에 maxEventsPerSecond(256, server가 알린 값과 작은 쪽)를 넘는 event가 오면 EVENT_RATE_EXCEEDED recovery를 요청하고 QUEUE_OVERFLOW로 닫는다. 이를 확인하는 테스트가 없어 검사를 꺼도 모든 테스트가 통과한다(리뷰 변이 R7, 두 라운드째). 설계 11절 상한표에도 행이 없다. 고른 방법 - 상한을 2로 낮춘 연결에서 2개 → 1초 뒤 2개(열림 유지) → 1개 더(닫힘, EVENT_RATE_EXCEEDED)를 보내는 테스트 1건을 더한다. 창 초기화와 상한 값 출처까지 한 테스트가 본다. - 설계 11절 상한표에 행 하나를 더한다. 위험·한계 - 동작은 바꾸지 않았다. 실제 서버 부하로는 확인하지 않았다. 검증 - 새 테스트 통과(websocket-connection 24건). - 변이 3건 모두 검출: 상한 검사 끄기(R7), 1초 창 초기화 삭제, 협상된 상한 대신 256 고정. - 바꾼 파일 eslint 통과. Refs: docs/reviews/2026-11-03-maturity-review/04-adapters-realtime.md 2절 1101/04-03 --- ...ime-events-web-push-and-bounded-polling.md | 1 + .../realtime/websocket-connection.test.ts | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/docs/architecture/realtime-events-web-push-and-bounded-polling.md b/docs/architecture/realtime-events-web-push-and-bounded-polling.md index 4659ab70..a0d3a2e7 100644 --- a/docs/architecture/realtime-events-web-push-and-bounded-polling.md +++ b/docs/architecture/realtime-events-web-push-and-bounded-polling.md @@ -1837,6 +1837,7 @@ authoritative query data를 덮지 않는다. | reorder buffer | 기본 0; 선택 시 64 event, 2초 | | WebSocket outbound queued bytes | 256 KiB | | WebSocket outbound queued messages | 128 | +| WebSocket 구독별 event rate | 1초 창마다 256 event. client 상한과 server가 알린 `maxEventsPerSecond` 중 작은 값. 넘으면 그 구독에 `EVENT_RATE_EXCEEDED` recovery를 요청하고 `QUEUE_OVERFLOW`로 연결을 닫는다(1101/04-03, `websocket-connection.ts` `processEvent`) | | reconnect attempts | 연속 10회 또는 5분 | | reconnect max delay | 60초 | | aborted reconnect task drain | 2초 고정; 구현 절대 최대 30초 | diff --git a/tests/unit/realtime/websocket-connection.test.ts b/tests/unit/realtime/websocket-connection.test.ts index e01ab249..aad6dc46 100644 --- a/tests/unit/realtime/websocket-connection.test.ts +++ b/tests/unit/realtime/websocket-connection.test.ts @@ -650,6 +650,63 @@ describe("WebSocket connection generation", () => { releaseFirst?.(); }); + // 1101/04-03. Each subscription may apply at most maxEventsPerSecond events + // in a one-second window (the lower of the client ceiling and the server's + // advertised limit). One more requests recovery and closes the generation. + it("closes a subscription that exceeds its per-second event ceiling", async () => { + const runtime = setup({ ceilings: { maxEventsPerSecond: 2 } }); + await openConnection(runtime); + runtime.connection.subscribe({ + subscriptionId: "subscription.0001", + streamId: "orders.v1", + scopeBinding: "scope.0001", + stateBearing: true, + checkpoint: { + streamEpoch: "stream-epoch.0001", + lastAppliedSequence: "10", + cursor: "cursor.0010", + }, + }); + runtime.socket.message( + serverFrame({ + type: "SUBSCRIBED", + subscriptionId: "subscription.0001", + streamEpoch: "stream-epoch.0001", + acceptedCursor: "cursor.0010", + nextExpectedSequence: "11", + }), + ); + await flush(); + const event = serverFrame({ + type: "EVENT", + subscriptionId: "subscription.0001", + envelope: { + protocol: "REALTIME_EVENT_V1", + streamId: "orders.v1", + }, + }); + const deliver = async (count: number) => { + for (let index = 0; index < count; index += 1) { + runtime.socket.message(event); + for (let turn = 0; turn < 5; turn += 1) await flush(); + } + }; + + await deliver(2); + runtime.clock.current += 1_000; + await deliver(2); + expect(runtime.onEvent).toHaveBeenCalledTimes(4); + expect(runtime.connection.inspect().status).toBe("OPEN"); + + await deliver(1); + expect(runtime.onEvent).toHaveBeenCalledTimes(4); + expect(runtime.connection.inspect().status).toBe("CLOSED"); + expect(runtime.recoveries).toContainEqual({ + subscriptionId: "subscription.0001", + reason: "EVENT_RATE_EXCEEDED", + }); + }); + it("rejects an event routed through the wrong logical subscription", async () => { const runtime = setup(); await openConnection(runtime);