Skip to content

feat: share httpx client + cache ProxyDecider across modules#4

Closed
power721 wants to merge 1 commit into
masterfrom
worktree-shared-httpx-client
Closed

feat: share httpx client + cache ProxyDecider across modules#4
power721 wants to merge 1 commit into
masterfrom
worktree-shared-httpx-client

Conversation

@power721
Copy link
Copy Markdown
Owner

Summary

  • 引入 NetworkClient:按 ProxyDecision.kind 维护 ≤3 个长存 httpx.Client(direct / system / manual:)+ 1 个长存 requests.Session,池上限 10,线程安全
  • AppCoordinator 集中持有 NetworkClient,并为 5 个调用点(plugin spider session、poster_loader、main_window get+post、player_window get)注册 loader,全部走共享客户端
  • 代理配置改动检测:AdvancedSettingsDialog 经包装的 save_config 仅在 proxy 三字段变化时调用 invalidate_proxy();direct 客户端不受影响保留 keep-alive
  • 消除每次请求的 TCP+TLS 握手 + 重复 SQLite load_config() 调用

主要文件改动

文件 改动
src/atv_player/network_client.py 新增:NetworkClient + _default_client_factory / _default_session_factory
src/atv_player/app.py AppCoordinator 持有 NetworkClient;_proxy_http_* 简化;5 个 loader 注册;close() 关闭网络层
src/atv_player/plugins/compat/base/spider.py set_session_loader + fetch/post 走 Session
src/atv_player/ui/poster_loader.py set_http_get_loader,默认 get=None 时走 loader
src/atv_player/ui/main_window.py 5 个直接 httpx.get/post 改为 _resolve_http_get/_resolve_http_post
src/atv_player/ui/player_window.py 1 个直接 httpx.get(外挂字幕)改用 loader;移除冗余 get=httpx.get

Test plan

  • 79 个新增 TDD 测试全部通过(6 个新测试文件)
  • 174/174 直接相关测试通过(network_client、proxy_invalidation_wrapper、spider_session_loader、poster_loader、network_proxy、hls_proxy_*、spider_plugin_manager、spider_plugin_loader、poster_loader_network、main_window_network_loaders、player_window_network_loader)
  • ruff check 在 6 个改动文件中全绿
  • 与 origin/master 基线对比 test_browse_page_ui / test_build / test_mpv_widget:失败集完全一致(15 条),零新增回归
  • 手测:启动应用、播放视频、切换代理设置后请求立即走新代理

已知 pre-existing 测试问题(与本 PR 无关)

test_app.pytest_main_window_ui.pytest_player_window_ui.py 中部分测试在 origin/master 上即已 hang/fail(mock 漏 get 参数、plugin 启动加载相关),跳过比对而非引入。

🤖 Generated with Claude Code

Introduce NetworkClient that maintains a small pool of long-lived
httpx.Client instances keyed by ProxyDecision (direct/system/manual:<url>)
plus a shared requests.Session, eliminating per-request TCP+TLS
handshakes across proxy server, plugin spider compat layer, poster
loader, main window hot-search loaders, player window subtitle fetch,
and danmaku/metadata service factories.

AdvancedSettingsDialog saves now compare the proxy signature and
invalidate only the system/manual clients on change while keeping
the direct client warm.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@power721 power721 closed this May 19, 2026
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