fix(export): 增加文档级组合复杂度预算与并发渲染限制

针对 PR 审阅 P1「组合复杂度仍可长时间占满导出线程」与 P3「EXPORT_OUTPUT_TOO_LARGE 误标 HTTP 413」:

- plot: FunctionPlot 记录整块 AST 节点数(node_count),parser 累计
- html: 单篇文档累计节点预算 _MAX_TOTAL_PLOT_NODES=8000,超限回退占位
- service: 并发渲染信号量 MAX_CONCURRENT_RENDERS=2,超限额任务排队等待
- docs: 错误码区分同步 HTTP 错误与异步任务错误,EXPORT_OUTPUT_TOO_LARGE 由
  error_code 返回而非 HTTP 413
- 补充节点预算与并发限制两条回归测试(全量 627 通过)

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
yxx
2026-09-06 14:01:29 +08:00
co-authored by Claude Code
parent 124024a547
commit c9c5f81d49
7 changed files with 130 additions and 41 deletions
+34
View File
@@ -449,3 +449,37 @@ def test_export_output_too_large(monkeypatch) -> None:
assert finished.error_code == "EXPORT_OUTPUT_TOO_LARGE"
assert finished.file is None
assert not (get_settings().exports_path / f"{finished.job_id}.html").exists()
def test_export_limits_concurrent_rendering(monkeypatch) -> None:
# P1:并发渲染受 MAX_CONCURRENT_RENDERS 限制,大量任务不会同时占满工作线程
import threading
import time
real_render = export_service._render_document
active = 0
peak = 0
lock = threading.Lock()
def slow_render(document, options):
nonlocal active, peak
with lock:
active += 1
peak = max(peak, active)
time.sleep(0.05)
with lock:
active -= 1
return real_render(document, options)
monkeypatch.setattr(export_service, "_render_document", slow_render)
async def _go():
jobs = [
await export_service.create_export(_markdown_request(f"# t{i}"))
for i in range(6)
]
return [await export_service.wait_for_export(j.job_id) for j in jobs]
finished = asyncio.run(_go())
assert all(j.status == ExportStatus.completed for j in finished)
assert peak <= export_service.MAX_CONCURRENT_RENDERS