⚡ perf(mcp): 隔离同步数据库调用与事件循环 - #156
Merged
Merged
Conversation
Owner
Author
阶段回帖 1/2:实现完成来源与范围
实现证据
设计边界
|
Owner
Author
阶段回帖 2/2:验证完成环境与输入
命令与原始结果验收映射
边界与未执行项
|
ZhaoXingPeng
force-pushed
the
perf/155-async-db-boundary
branch
from
September 9, 2026 01:51
48da618 to
58c4648
Compare
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.
关联 Issue
Closes #155
本 PR 将同步数据库调用移出 MCP 事件循环,并为共享连接建立明确的并发边界。
背景(Situation)
MCP handler 使用 async def,但查询、发现、元数据、代码生成分析和 ER 路径直接调用同步数据库驱动。网络等待或大型 SQLite 查询会占住事件循环;SQLite 单连接跨 worker 使用也需要线程配置和串行策略。
任务(Task)
在不替换同步驱动、不改变 SQL、事务、权限、同步 API 或 MCP 响应合同的前提下,将阻塞数据库阶段移入 worker;保证同一连接串行、不同连接可并行等待,并覆盖关闭竞争和 SQLite worker。
行动(Action)
验证(Verification)
环境:Windows;Python 3.10.1;pytest 9.0.3;pytest-asyncio 1.3.0;SQLite 内存/文件 fixture。
实验与证据(Evidence)
受控 driver 在 execute 内阻塞 60ms,同时 heartbeat 每 2ms 运行 40ms,等待期间获得至少 2 次调度。同连接 fake cursor 最大活动数为 1;两个不同 connection_id 可同时进入 worker;close 在 query 完成前等待并在之后清理连接、配置和锁。SQLite worker 使用 SELECT 1 AS value,无 thread-affinity 错误。以上为受控 stub 与 SQLite 证据,不代表真实网络数据库吞吐。
兼容性、风险与回滚
保留同步 ConnectionManager 方法签名、异常类型、SQL、事务和 Raw Response。线程调度增加少量开销;同一连接仍串行;取消 async handler 不会强杀底层 worker。真实 MySQL/PostgreSQL 驱动线程模型需在容器中验证。恢复提交 48da618 可移除本主题,不涉及用户数据迁移;后续建立固定数据库、查询集和硬件条件下的性能基线。