mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-24 10:53:12 +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.
113 lines
3.8 KiB
Go
113 lines
3.8 KiB
Go
package helps
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
|
)
|
|
|
|
// This test changes the process-wide generation and must not run in parallel.
|
|
func TestProxyPoolGenerationRejectsOldSnapshotAfterReload(t *testing.T) {
|
|
proxyPoolGeneration.Lock()
|
|
previous := proxyPoolGeneration.current
|
|
proxyTransportCache.Purge()
|
|
proxyPoolGeneration.Unlock()
|
|
t.Cleanup(func() {
|
|
proxyPoolGeneration.Lock()
|
|
proxyTransportCache.Purge()
|
|
proxyPoolGeneration.current = previous
|
|
proxyPoolGeneration.Unlock()
|
|
})
|
|
old := &config.Config{}
|
|
AdvanceProxyPoolGeneration(old)
|
|
oldClone := old.CloneForRuntime()
|
|
if oldClone.ProxyPoolGeneration != old.ProxyPoolGeneration {
|
|
t.Fatal("runtime clone lost generation")
|
|
}
|
|
first := pooledProxyTransport("direct", oldClone, poolOptInAuth("generation"))
|
|
if first == nil || proxyTransportCache.Len() != 1 {
|
|
t.Fatal("initial pool missing")
|
|
}
|
|
next := old.CloneForRuntime()
|
|
AdvanceProxyPoolGeneration(next)
|
|
if proxyTransportCache.Len() != 0 {
|
|
t.Fatal("reload did not purge pool")
|
|
}
|
|
// A request captured the old snapshot before reload and resumes only after it.
|
|
resume := make(chan struct{})
|
|
result := make(chan *http.Transport, 1)
|
|
go func() {
|
|
<-resume
|
|
result <- pooledProxyTransport("direct", oldClone, poolOptInAuth("generation"))
|
|
}()
|
|
close(resume)
|
|
stale := <-result
|
|
if stale == nil || proxyTransportCache.Len() != 0 {
|
|
t.Fatal("stale snapshot repopulated pool")
|
|
}
|
|
current := pooledProxyTransport("direct", next, poolOptInAuth("generation"))
|
|
if current == nil || proxyTransportCache.Len() != 1 {
|
|
t.Fatal("current snapshot did not populate pool")
|
|
}
|
|
if pooledProxyTransport("direct", next, poolOptInAuth("generation")) != current {
|
|
t.Fatal("current transport not reused")
|
|
}
|
|
if first == current || stale == current {
|
|
t.Fatal("old and new generations share transports")
|
|
}
|
|
}
|
|
|
|
// TestProxyPoolUnfencedSnapshotStillPools covers configs that never passed through the
|
|
// SDK generation fence: generation 0 cannot be stale, so pooling must stay on even after
|
|
// the process-wide generation has advanced. Regression for callers that build their own
|
|
// config.Config (direct api usage, helper probes) and would otherwise lose pooling
|
|
// silently once any SDK service published a config.
|
|
func TestProxyPoolUnfencedSnapshotStillPools(t *testing.T) {
|
|
ResetProxyTransports()
|
|
t.Cleanup(ResetProxyTransports)
|
|
proxyPoolGeneration.Lock()
|
|
previous := proxyPoolGeneration.current
|
|
proxyPoolGeneration.Unlock()
|
|
t.Cleanup(func() {
|
|
proxyPoolGeneration.Lock()
|
|
proxyPoolGeneration.current = previous
|
|
proxyTransportCache.Purge()
|
|
proxyPoolGeneration.Unlock()
|
|
})
|
|
|
|
// Publish a config so the process-wide generation moves past zero.
|
|
AdvanceProxyPoolGeneration(&config.Config{})
|
|
|
|
plain := &config.Config{}
|
|
if plain.ProxyPoolGeneration != 0 {
|
|
t.Fatalf("expected an unfenced snapshot, generation = %d", plain.ProxyPoolGeneration)
|
|
}
|
|
auth := poolOptInAuth("unfenced")
|
|
pooled := pooledProxyTransport("direct", plain, auth)
|
|
if pooled == nil {
|
|
t.Fatal("unfenced snapshot must still pool")
|
|
}
|
|
if pooledProxyTransport("direct", plain, auth) != pooled {
|
|
t.Fatal("unfenced snapshot did not reuse its pool")
|
|
}
|
|
|
|
// A snapshot carrying a retired generation is still fenced out.
|
|
retired := &config.Config{}
|
|
proxyPoolGeneration.RLock()
|
|
retired.ProxyPoolGeneration = proxyPoolGeneration.current + 1
|
|
proxyPoolGeneration.RUnlock()
|
|
before := proxyTransportCache.Len()
|
|
first := pooledProxyTransport("direct", retired, auth)
|
|
second := pooledProxyTransport("direct", retired, auth)
|
|
if first == nil || second == nil {
|
|
t.Fatal("fenced snapshot must still get a usable transport")
|
|
}
|
|
if first == second {
|
|
t.Fatal("fenced snapshot must not be cached")
|
|
}
|
|
if got := proxyTransportCache.Len(); got != before {
|
|
t.Fatalf("fenced snapshot repopulated the cache: %d -> %d", before, got)
|
|
}
|
|
}
|