Skip to content

perf: 稳定性和性能优化#7

Merged
chicogong merged 2 commits into
masterfrom
perf/stability-optimizations
Dec 24, 2025
Merged

perf: 稳定性和性能优化#7
chicogong merged 2 commits into
masterfrom
perf/stability-optimizations

Conversation

@chicogong

Copy link
Copy Markdown
Owner

Summary

本 PR 包含多项稳定性和性能优化:

线程安全

  • 为 session 字典添加 RLock 锁,防止并发竞态条件
  • 所有 session 操作(get/remove/get_all)均为线程安全

WebSocket 改进

  • 添加 60 秒消息超时,防止僵尸连接占用资源
  • 超时后优雅关闭连接

安全性

  • 新增敏感数据掩码工具 utils/security.py
  • API 密钥和 Token 在日志中自动掩码
  • 新增 Config.get_service_config_masked() 方法用于安全日志

HTTP 客户端管理

  • 新增共享 HTTP 客户端管理器 utils/http_client.py
  • 连接池复用,可配置最大连接数
  • 启用 HTTP/2 支持
  • 使用 asyncio.Lock 保证线程安全

代码质量

  • 移除 TTS 服务中冗余的 HTTP 客户端管理代码
  • 修复 ruff lint 问题

Changes

文件 修改内容
session.py 添加 _sessions_lock 线程安全锁
websocket/handler.py 添加 WebSocket 消息超时
utils/security.py 新增敏感数据掩码工具
utils/http_client.py 新增 HTTP 客户端管理器
config.py 添加 get_service_config_masked()
services/tts/*.py 使用共享 HTTP 客户端
app.py 关闭时清理 HTTP 客户端

Test plan

  • 114 个单元测试全部通过
  • Ruff 代码检查通过
  • 服务启动测试通过
  • WebSocket 连接测试通过

🤖 Generated with [CodeBuddy Code]

## Thread Safety
- Add RLock to session dictionary for thread-safe access
- Prevent race conditions in concurrent session operations

## WebSocket Improvements
- Add 60-second message timeout to prevent zombie connections
- Graceful connection close on timeout

## Security
- Add sensitive data masking utility (utils/security.py)
- Mask API keys and tokens in logs
- Add Config.get_service_config_masked() for safe logging

## HTTP Client Management
- Add shared HTTP client manager (utils/http_client.py)
- Connection pooling with configurable limits
- HTTP/2 support enabled
- Thread-safe client acquisition with asyncio.Lock

## Code Quality
- Remove redundant HTTP client management from TTS services
- Fix ruff lint issues

🤖 Generated with [CodeBuddy Code]

Co-Authored-By: CodeBuddy Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread utils/http_client.py
Comment on lines +60 to +63
cls._client = httpx.AsyncClient(
timeout=timeout_config,
limits=limits,
http2=True, # Enable HTTP/2 for better performance

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Enabling HTTP/2 without declaring required dependency

HTTPClientManager now constructs the shared client with http2=True, but the dependency manifests still declare only httpx>=0.25.2 (no httpx[http2] or h2). On an environment installed from those manifests, creating this client will raise an ImportError for the missing HTTP/2 extras and all TTS HTTP requests will fail at runtime. Either drop HTTP/2 here or add the appropriate extras dependency.

Useful? React with 👍 / 👎.

- Fix frontend/backend command protocol mismatch (command -> type)
- Add TextInputCommand model for text input support
- Implement text input handler in WebSocket handler
- Add text input event binding in frontend app.js
- Add HTTP/2 dependency (httpx[http2]) for better performance
- Fix user message display (use final_transcript type)

🤖 Generated with [CodeBuddy Code]

Co-Authored-By: CodeBuddy Code
@chicogong chicogong merged commit 24c9f3f into master Dec 24, 2025
0 of 2 checks passed
@chicogong chicogong deleted the perf/stability-optimizations branch December 24, 2025 07:29
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