Skip to content

Refactor: Cache Proxy HTTP Clients with Reset on Channel Updates - #1894

Merged
seefs001 merged 3 commits into
QuantumNous:mainfrom
RedwindA:refactor/enhance-channel-proxy
Sep 29, 2025
Merged

seefs001 merged 3 commits into
QuantumNous:mainfrom
RedwindA:refactor/enhance-channel-proxy

Conversation

@RedwindA

@RedwindA RedwindA commented Sep 27, 2025 •

Copy link
Copy Markdown
Contributor

PR 类型

  • Bug 修复
  • 新功能
  • 文档更新
  • 其他

PR 是否包含破坏性更新?

  • 是
  • 否

PR 描述

close #1863

  • 引入 proxyClients 缓存,按代理 URL 存储已初始化的 *http.Client,避免重复创建客户端和连接池。
  • 命中缓存时直接复用客户端;添加 ResetProxyClientCache,在渠道增改后清空缓存并关闭空闲连接,确保配置更新即时生效。

动机

  • 频繁为同一代理 URL 构造新的 http.Client 会重复握手、建立连接池,带来额外延迟与代理端负载。
  • 渠道配置变化后,立即刷新关联的代理客户端,防止继续使用过期的代理设置。

涉及改动

  • service/http_client.go: 新增缓存 map 与互斥锁,支持缓存命中与主动刷新;在 HTTP/HTTPS 与 SOCKS5 初始化逻辑中写入缓存;提供
    ResetProxyClientCache 用于清理缓存和关闭空闲连接。
  • controller/channel.go: 在渠道新增、更新流程成功后调用 ResetProxyClientCache,触发代理客户端重建。

Summary by CodeRabbit

  • New Features

    • None.
  • Bug Fixes

    • Proxy updates now take effect immediately after adding or updating channels, reducing stale connections.
  • Refactor

    • Improved reuse of proxy clients to reduce overhead and speed repeated requests.
  • Chores

    • Enhanced internal proxy cache management to improve stability and reduce unnecessary reconnections.

@coderabbitai

coderabbitai Bot commented Sep 27, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Adds a mutex-guarded cache of proxy *http.Client instances and a ResetProxyClientCache() function in the service. Controller now calls service.ResetProxyClientCache() after channel Add and Update operations. Reset closes idle connections for cached clients and reinitializes the cache.

Changes

Cohort / File(s) Summary of edits
Controller: cache reset on channel mutations
controller/channel.go
Added import for service and calls to service.ResetProxyClientCache() after AddChannel and after UpdateChannel (post-update and cache reinit).
Service: proxy HTTP client cache and reset
service/http_client.go
Added mutex-guarded map caching proxyURL → *http.Client; implemented GetHTTPClient(proxyURL) to return http.DefaultClient for empty proxy, reuse cached clients on hits, or create & cache clients for http/https and socks5 schemes. Added ResetProxyClientCache() to call CloseIdleConnections() on cached transports and clear/reinit the cache.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant Controller
  participant Service as Service (HTTP Client Cache)
  note over Service: Map[proxyURL]*http.Client + Mutex

  Caller->>Controller: AddChannel/UpdateChannel(...)
  Controller->>Controller: Persist changes and refresh channel cache
  Controller->>Service: ResetProxyClientCache()
  activate Service
  Service->>Service: For each cached client: CloseIdleConnections()
  Service->>Service: Clear and reinit cache map
  deactivate Service
  Controller-->>Caller: Success
Loading
sequenceDiagram
  autonumber
  participant Code as Caller
  participant Service as Service (HTTP Client Cache)

  Code->>Service: GetHTTPClient(proxyURL)
  alt proxyURL == ""
    Service-->>Code: http.DefaultClient
  else proxyURL in cache
    Service-->>Code: Cached *http.Client
  else (cache miss)
    Service->>Service: Create *http.Client (http/https or socks5 transport + Timeout)
    Service->>Service: Store in cache
    Service-->>Code: New *http.Client
  end

  rect rgba(235,248,255,0.6)
  note right of Service: On channel add/update -> ResetProxyClientCache() closes idle conns and clears cache
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A rabbit hops, a cache refreshed,
Closing old holes where leaks once crept.
New clients snug in tidy rows,
Idle tunnels swept and kept.
I twitch my nose — connections rest. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title succinctly describes the central change of introducing a cache for proxy HTTP clients and resetting it on channel updates, which accurately reflects the modifications in service/http_client.go and controller/channel.go without extraneous detail.
Linked Issues Check ✅ Passed The pull request implements a thread-safe cache for proxy HTTP clients and adds a ResetProxyClientCache function that closes idle connections and clears the cache, as well as invokes this reset after channel create and update, directly addressing the connection leak described in issue #1863 and ensuring proper connection release under load.
Out of Scope Changes Check ✅ Passed All modifications are focused on introducing and integrating proxy client caching and reset behavior in service/http_client.go and controller/channel.go to address the connection leak issue, and there are no unrelated or extraneous changes outside this scope.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@RedwindA RedwindA changed the title Refactor/enhance-channel-proxy Refactor: Cache Proxy HTTP Clients with Reset on Channel Updates Sep 27, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 74122e4 and 486c828.

📒 Files selected for processing (2)
  • controller/channel.go (3 hunks)
  • service/http_client.go (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
controller/channel.go (1)
service/http_client.go (1)
  • ResetProxyClientCache (37-46)

Comment thread service/http_client.go Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 486c828 and 466d19c.

📒 Files selected for processing (1)
  • service/http_client.go (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
service/http_client.go (1)
common/constants.go (1)
  • RelayTimeout (121-121)

Comment thread service/http_client.go Resolved
@seefs001
seefs001 merged commit d916456 into QuantumNous:main Sep 29, 2025
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
…annel-proxy

Refactor: Cache Proxy HTTP Clients with Reset on Channel Updates
@RedwindA
RedwindA deleted the refactor/enhance-channel-proxy branch July 23, 2026 07:04
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
…annel-proxy

Refactor: Cache Proxy HTTP Clients with Reset on Channel Updates
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
…annel-proxy

Refactor: Cache Proxy HTTP Clients with Reset on Channel Updates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants