feat(ov): idempotent batched likes and realtime broadcast

P2 Task 7(点赞批次端点 + ov 实时网关):
- interactions 模块(仅 ov 装配):POST /public/albums/{token}/photos/
  {photoId}/likes——相册不可见/照片未发布 404、delta 1..20 与
  sessionId/batchId 1..64 校验 400;单事务 like_batches 唯一插入
  (P2002=幂等重试,batchAccepted=true 不重加)→ photos_ov.likeTotal/
  countVersion 原子自增回读 → outbox_stat_events(kind=likes) 兜底;
  Redis ov(REDIS_URL_OV)INCRBY+TTL 会话/IP 双窗口限流(默认
  500/2000,超限 429 RATE_LIMITED),幂等重试先于限流不耗额度
- 过期批次重放边界(24h 清理后的 batchId 重插会再次计入):接受该
  边界——无永久账本可查(保留全量批次行违背清理策略),会话/IP 窗口
  上限即重放天然封顶(同一 sessionId 重放挤占自身额度),偏差已在
  likes.service 头注释登记,Task 8 统计侧按清理后语义处理
- realtime:ContractWsAdapter(契约 {type,...} 帧分发,ws 与 HTTP 同
  端口挂 /v1/realtime)+ RealtimeGateway 相册房间模型——subscribe 逐
  相册校验 isAlbumVisibleOv,失败回 error 帧且连接保持;应用层 ping→
  pong 回显 ts;传输层 HEARTBEAT_MS 周期 ping 踢死连接;点赞提交经
  EventEmitter2(单进程,多实例 Redis pub/sub 登记 P5)触发
  likes.burst 500ms 合并窗(delta 聚合、originBatchIds 供客户端过滤
  自身批次);sync-inbox 应用事务提交后发 OV_PHOTO_APPLIED →
  photo.published/hidden/deleted 按房间广播(stale/duplicate/暂存/
  缺席即撤下不转发)
- 契约:REALTIME_WS_PATH、ServerMessage 增 error 帧(穷尽锁 7 成员
  + 类型测试)、LIKE_SESSION_WINDOW_DELTA/LIKE_IP_WINDOW_DELTA/
  LIKE_BURST_MERGE_MS 常量与 .env.example
- e2e(likes-realtime.e2e-spec.ts,6 用例):A09 三批累加/幂等重试/
  outbox 行、小额度 429、ping/pong+幽灵 token error 帧连接保持、
  A10 双房间隔离(先证非空调订)、合并窗单 burst(delta 求和+双
  originBatchIds)、cn 隐藏→房间收 photo.hidden+隐藏后点赞 404;
  凡进程内起 AppOvModule 的 spec(sync×2/public-ov/emergency)补装
  ContractWsAdapter 与生产 main-ov 一致——api e2e 168(162+6)

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-09-18 17:51:55 +08:00
parent 0fcf18cc5c
commit 85b2da3db5
23 changed files with 1722 additions and 215 deletions
@@ -63,6 +63,16 @@ describe('ServerMessage', () => {
expect(pong.type).toBe('pong');
});
it('error 帧携带域码(subscribe 校验失败等场景)', () => {
const error: ServerMessage = { type: 'error', code: 'ALBUM_NOT_PUBLIC' };
if (error.type !== 'error') throw new Error('unreachable');
expect(error.code).toBe('ALBUM_NOT_PUBLIC');
// message 可选:缺省合法(客户端只应消费 code)。
// @ts-expect-error 非 error 分支没有 code
const absent = (burst as ServerMessage).code;
expect(absent).toBeUndefined();
});
it('非法 type 在编译期被拒绝', () => {
// @ts-expect-error type 必须严格属于联合成员
const invalid: ServerMessage = { ...burst, type: 'likes.update' };
@@ -81,7 +91,7 @@ describe('ServerMessage', () => {
expect(wrongTypes.type).toBe('likes.burst');
});
it('type 联合与 6 个消息成员 1:1(穷尽锁定)', () => {
it('type 联合与 7 个消息成员 1:1(穷尽锁定)', () => {
const exhaustive: Equal<
ServerMessage['type'],
| 'photo.published'
@@ -90,6 +100,7 @@ describe('ServerMessage', () => {
| 'likes.burst'
| 'stats.updated'
| 'pong'
| 'error'
> = true;
expect(exhaustive).toBe(true);
});
@@ -102,6 +113,7 @@ describe('ServerMessage', () => {
{ type: 'photo.deleted', photoId: 'p', albumId: 'a', entityVersion: 3 },
{ type: 'stats.updated', albumId: 'a', viewCount: 99, countVersion: 9 },
{ type: 'pong', ts: 1758187200000 },
{ type: 'error', code: 'ALBUM_NOT_PUBLIC', message: '相册不存在或未公开' },
];
const kinds = messages.map((m) => {
switch (m.type) {
@@ -115,6 +127,8 @@ describe('ServerMessage', () => {
return `stats:${m.viewCount}`;
case 'pong':
return `pong:${m.ts}`;
case 'error':
return `error:${m.code}`;
}
});
expect(kinds).toEqual([
@@ -124,6 +138,7 @@ describe('ServerMessage', () => {
'photo:3',
'stats:99',
'pong:1758187200000',
'error:ALBUM_NOT_PUBLIC',
]);
});
});