Skip to content

fix(v3): broker 关闭竞争 + send() 背压有界 outbox(pre-existing 可靠性)#204

Open
raysonmeng wants to merge 1 commit into
feat/v3-web-dashboardfrom
feat/v3-backlog-reliability
Open

fix(v3): broker 关闭竞争 + send() 背压有界 outbox(pre-existing 可靠性)#204
raysonmeng wants to merge 1 commit into
feat/v3-web-dashboardfrom
feat/v3-backlog-reliability

Conversation

@raysonmeng

Copy link
Copy Markdown
Owner

摘要 / Summary

修两个 pre-existing broker 可靠性 bug:① Broker.stop() 关闭竞争(同步丢弃 server.stop(true) 的 Promise → store.close() 撞 in-flight 投递);② send() 背压静默丢消息(忽略 ws.send() 返回值、无 drain handler,在线但慢的订阅者丢实时事件)。

⚠️ 请勿合并 / DO NOT MERGE —— 试用期。base = feat/v3-web-dashboard(backlog sibling)。

变更 / Changes(commit fb192cb

  • Broker.stop() 改 async + await server.stop(true) + never-reject(try/catch swallow+log,避免未 await 调用点的 unhandled rejection);cli/broker.ts shutdown 有序 await 链 webHandle.stop() → await broker.stop() → await store.close() + 10s forceExit 保险丝
  • send() 改 enqueue + flushOutbox(有界 FIFO outbox,OUTBOX_CAP=256 drop-oldest);修正 Bun 1.3.11 的 ws.send() 三态契约r>0=已发 / r<0=Bun 已缓冲会自投(不可重发否则双投)/ r===0=丢弃(需重试);drain(ws) 回调触发 flush;全程 FIFO 保序
  • 新增 broker-backpressure.test.ts(8 unit tests,三态返回 + drop-oldest + FIFO + head-of-line)

测试 / Test plan

  • bun run check 全绿(1859 pass)
  • 关键:opus reviewer 写真 Bun.serve PoC 实证了 ws.send() 三态契约——r>0 5/5 到达、r<0 5/5 到达、r===0 0/390 到达;整个「不丢、不双投」的逻辑地基被实测证实
  • Cross-review:opus 深审(含 PoC)+ 多轮 sonnet;round-3 两个 fresh reviewer 全量回归 0 真实 issue 收敛

🤖 Generated with Claude Code

修两个 pre-existing 可靠性问题:

修复 1 — Broker.stop() 关闭竞争
- broker.ts: stop() 改 async,await Bun.serve.stop(true)(WS 全关后再置 null);
  原来同步丢弃 Promise → in-flight send/drain 撞已关 store 致静默丢消息
- cli/broker.ts: shutdown 改有序 await 链 webHandle.stop() → await broker.stop()
  → await store.close() → exit(0),加 10s forceExit 保险丝防 stop() 挂死

修复 2 — send() 背压静默丢消息(R3 LOW-3)
- send() 改为 enqueue + flushOutbox(有界 FIFO outbox,OUTBOX_CAP=256,超限 drop-oldest + log)
- 关键修正 Bun 1.3.11 的 ws.send() 返回契约:
  r>0 = 已发;r<0 = Bun 已缓冲、会自行投递(**不可重发,否则双投**);r===0 = 丢弃(需重试)
- flushOutbox:r===0 保留队首等 drain;r<0 移除但停(Bun 接管该帧);r>0 移除续 flush
- drain(ws) 回调触发 flushOutbox;全程 FIFO 保序
- 新增 src/unit-test/broker-backpressure.test.ts(8 unit tests:入队/上限 drop/flush/FIFO/三态返回)

fix(broker): shutdown race + bounded send() backpressure outbox (PR7 backlog)

- Broker.stop() is now async (await Bun.serve.stop(true)); cli/broker.ts shutdown awaits
  the chain so store.close() can't race in-flight delivery. 10s forceExit fuse.
- send() enqueues to a bounded FIFO outbox + flushes on Bun's drain(ws); corrects the
  Bun ws.send() return contract (r>0 sent / r<0 buffered-by-Bun, never re-send / r===0
  dropped, retry on drain). FIFO order preserved. 8 backpressure unit tests.

bun run check 全绿(1857 pass)。Cross-review 由父 agent 编排(合并前进行,未自审定论)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012rhWKm1VUSnEVxmjpYwNfc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant