mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
Give the generic proxy-aware clients a reusable transport per api-key credential, driven by the `connection-pool` block added in the previous commit. - Pools are keyed by credential scope (auth ID, then path/source, then a digest of refresh/access/API-key material, then anonymous), normalized proxy target (authenticated proxies fold a SHA-256 digest of user:pass into the key so each proxy identity or sticky-session username keeps its own pool without storing plaintext secrets in the cache key or logs), and the resolved settings. Two keys therefore never share a TCP/TLS connection regardless of whether the proxy is unauthenticated, authenticated, direct, or inherited. - Capacity is a 512-entry LRU: one key keeps a warm pool, 100 keys keep one each, and 100000 keys fall back to the historical one-transport-per-request behavior when the cache is full. Each transport is bounded to MaxIdleConnsPerHost instead of the cloned default of 100 idle sockets, so idle sockets stay at capacity*max no matter how many keys exist. - Dead peers are detected without request timeouts: 30s TCP keepalive on every dialed connection (including SOCKS5, which used proxy.Direct before) plus the idle timeout, 300s by default. Measured against ollama.com, an idle pooled connection was still reusable after 600s; the 90s that used to bound this path was net/http's own IdleConnTimeout, not an upstream limit. - Explicit 0 or negative values disable keep-alives (including when proxy-url is empty), invalid durations are parsed once per pool instead of warning per request, and pooling works with or without an explicit proxy-url while keeping priority for context-injected RoundTrippers when proxy-url is empty. - Config snapshots carry a generation so a request holding an obsolete snapshot cannot repopulate the cache after a reload. Generation 0 means the config never passed the fence and stays poolable, so callers that build their own config are not silently downgraded.
57 lines
2.1 KiB
Go
57 lines
2.1 KiB
Go
package helps
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
|
)
|
|
|
|
// These tests use the process-wide pool and must not run in parallel.
|
|
func TestProxyPoolSettingsAndConcurrentBuild(t *testing.T) {
|
|
ResetProxyTransports()
|
|
t.Cleanup(ResetProxyTransports)
|
|
// Writing the block without any field is the opt-in and must fall back to defaults.
|
|
auth := poolAuth("defaults", poolBlock())
|
|
cfg := testConfig()
|
|
tr := pooledProxyTransport("direct", cfg, auth)
|
|
if tr == nil || tr.IdleConnTimeout != proxyPoolDefaultIdleConnTimeout || tr.MaxIdleConnsPerHost != proxyPoolDefaultMaxIdleConnsPerHost {
|
|
t.Fatalf("unexpected defaults: %+v", tr)
|
|
}
|
|
if pooledProxyTransport("none", cfg, auth) != tr {
|
|
t.Fatal("direct and none should share transport")
|
|
}
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 32; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
if pooledProxyTransport("direct", cfg, auth) != tr {
|
|
t.Error("transport not shared")
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
for _, raw := range []string{"0s", "-1s"} {
|
|
zero := poolAuth("zero", &cliproxyauth.ProxyConnectionPoolOverride{IdleConnTimeout: raw})
|
|
if got := pooledProxyTransport("direct", cfg, zero); got == nil || !got.DisableKeepAlives {
|
|
t.Fatalf("%s must disable keep-alives", raw)
|
|
}
|
|
}
|
|
for _, max := range []int{0, -1} {
|
|
val := max
|
|
capped := poolAuth("max", &cliproxyauth.ProxyConnectionPoolOverride{MaxIdleConnsPerHost: &val})
|
|
if got := pooledProxyTransport("direct", cfg, capped); got == nil || !got.DisableKeepAlives {
|
|
t.Fatalf("max %d must disable keep-alives", max)
|
|
}
|
|
}
|
|
invalid := poolAuth("invalid", &cliproxyauth.ProxyConnectionPoolOverride{IdleConnTimeout: "invalid"})
|
|
bad := pooledProxyTransport("direct", cfg, invalid)
|
|
if bad == nil || bad.IdleConnTimeout != proxyPoolDefaultIdleConnTimeout || pooledProxyTransport("direct", cfg, invalid) != bad {
|
|
t.Fatal("invalid duration must use cached default")
|
|
}
|
|
if pooledProxyTransport("direct", nil, nil) == pooledProxyTransport("direct", nil, nil) {
|
|
t.Fatal("a credential without the block must retain unpooled behavior")
|
|
}
|
|
}
|