Repository navigation
fix(channel): 冷会话并发首访只回放一次,回放失败后不再缺历史常驻 - #1728
Merged
Merged
Conversation
同一会话的并发首访共用一次回放,回放原先跑在第一个调用方的 context 上。 它被取消时回放失败,等在这次回放上的其他消息也拿不到历史,接着把自己 推进 pipeline,会话就以只含新消息的状态算作已加载,直到被淘汰。 回放改在去掉取消的 context 上运行,保留请求携带的值。
回放失败后,这条入站消息照常投影进 pipeline,会话随之以只含新消息的 状态常驻:HasSession 为真,下一条消息不再重试回放,Server 也改用这份 缺了历史的渲染结果组装上下文,直到会话被淘汰。 回放现在报告会话是否已加载。未加载时这条消息照样渲染出自己的上下文 (Discuss 仍据此派发),但不让会话常驻,下一条消息重新回放,已持久化的 事件随之载入;回放期间被别处加载的会话照常追加。预热、持久化与投影 合为 projectInboundEvent,测试按生产顺序覆盖。
chen-ran
approved these changes
Nov 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Author
Type
Summary
Channel 重启或会话被 pipeline 淘汰(256 个会话 / 6 小时)后,会话的第一条消息要从数据库回放它的事件。#1723 已经把单次回放的数据库工作量降下来,这条路径上还有两个问题。
同一会话并发首访各自回放
HandleInbound只检查HasSession,同一会话同时到达的消息会各自回放一遍。Memoh-Cloud#695 的生产日志里,同一会话在同一毫秒完成了 3 次回放;每次回放都替换整个会话,后完成的那次还会冲掉其间推进来的事件。另外,没有历史的会话回放成功后并不算已加载,之后每条消息都要再查一次。现在同一会话的回放按 singleflight 合并:等在同一次回放上的消息共用结果;回放开始前再查一次
HasSession,在它之后到达的消息看到会话已加载,不会再回放;空历史的成功回放也算已加载。回放不随发起它的那条消息取消,否则等在这次回放上的其他消息也会拿不到历史。回放失败后,会话以缺历史的状态常驻
回放失败后,这条消息照常投影进 pipeline,会话就以只含新消息的状态常驻:
HasSession为真,下一条消息不会再重试回放,直到会话被淘汰。Discuss 的上下文直接取自 Channel 的这份渲染结果;Server 内嵌 Channel 运行时(all-in-one)时两者共用同一个 pipeline,chat 也改用它组装上下文,于是都缺了回放失败之前的历史。拆分部署下 Server 有自己的 pipeline,会回退到从数据库读取历史,chat 不受影响。main 上就是这样。现在回放会报告会话是否已加载。未加载时,这条消息照样渲染出自己的上下文(Discuss 仍据此派发),但会话不常驻(新增
Pipeline.PushResidentEvent)。下一条消息会重新回放,已持久化的事件随之载入;回放期间会话若已被别处加载,事件照常追加。预热、持久化与投影合成projectInboundEvent,测试按生产顺序覆盖这条路径。数据库连持久化也失败时,这条消息的事件只存在于它自己的这次渲染里;原先它会留在内存 pipeline 中直到被淘汰。
Related Issues
Refs felinics/Memoh-Cloud#695。#1723 处理了同一事故里单次回放的分页成本,本 PR 处理并发回放与回放失败。
Validation
分支 head
4d443a4ec(相对6a9193d64只重排了一段注释);运行环境为 main0b28f4783合入本批 4 个分支的本地集成构建(memoh-stackslot 1,Webhttp://localhost:41182,Channel 单独进程),Server 与 Channel 的运行二进制和独立重建的 SHA-256 一致。go build ./...、go vet、golangci-lint run、go test -race(internal/channel/inbound、internal/chat/timeline)通过;go test -tags integration -race覆盖上述包与cmd/agent、internal/chat/...,指向迁移到 172 的 PostgreSQL 18,27 个 Postgres 用例全部执行、无 skip。statement_timeout,并在bot_history_checkpoints上持有 5 秒排他锁,回放查询以 SQLSTATE 57014 失败;这条消息照常得到回复。故障解除后的下一条消息触发了第二次回放(载入 23 个事件),请求带着故障前的历史标记。main 上同样步骤之后不再回放。拆分部署下 main 的 Server 自己从数据库读到了历史,所以这次 live 对照证明的是"会重试回放",缺历史的后果由上面的生产顺序测试覆盖。所有数据库设置都已还原并核对。Screenshots / Recordings
截图由 Agent 通过浏览器自动化在运行中的应用里采集,不代表 Human QA。
Human QA