refactor: 限制流式文本收集内存增长,降低默认连接池和缓存上限

- StreamContext.append_text 增加 16KB 上限,超出后仅计数不存储,
  避免长流式响应导致内存持续增长;token 估算改用 collected_text_length
- 降低 DB 连接池上限 (30->15) 和 HTTP 连接池上限 (200->100)
- tiktoken 编码器缓存从 32 缩减到 4(实际编码种类只有几种)
- dev.sh 添加开发环境低配连接池默认值,uvicorn 热重载仅监视 src 目录
This commit is contained in:
fawney19
2026-03-10 15:33:46 +08:00
parent cfa5535f6e
commit 6ec8df97e8
8 changed files with 68 additions and 24 deletions

View File

@@ -5,11 +5,25 @@ from src.api.handlers.base.stream_context import StreamContext
def test_collected_text_append_and_property() -> None:
ctx = StreamContext(model="test-model", api_format="openai:chat")
assert ctx.collected_text == ""
assert ctx.collected_text_length == 0
ctx.append_text("hello")
ctx.append_text(" ")
ctx.append_text("world")
assert ctx.collected_text == "hello world"
assert ctx.collected_text_length == len("hello world")
def test_collected_text_is_capped_but_total_length_is_preserved() -> None:
ctx = StreamContext(model="test-model", api_format="openai:chat")
cap = stream_context._MAX_COLLECTED_TEXT_CHARS
ctx.append_text("a" * (cap - 4))
ctx.append_text("b" * 10)
assert len(ctx.collected_text) == cap
assert ctx.collected_text == ("a" * (cap - 4)) + ("b" * 4)
assert ctx.collected_text_length == cap + 6
def test_reset_for_retry_clears_state() -> None: