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.
146 lines
5.7 KiB
Go
146 lines
5.7 KiB
Go
package helps
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
|
)
|
|
|
|
// These tests share the process-wide pool and must not run in parallel.
|
|
|
|
func boolPtr(v bool) *bool { return &v }
|
|
|
|
func intPtr(v int) *int { return &v }
|
|
|
|
// TestProxyPoolRequiresCredentialOptIn pins the per-api-key switch: there is no global
|
|
// switch, so only a credential carrying a connection-pool block is pooled.
|
|
func TestProxyPoolRequiresCredentialOptIn(t *testing.T) {
|
|
ResetProxyTransports()
|
|
t.Cleanup(ResetProxyTransports)
|
|
|
|
cfg := testConfig()
|
|
optedIn := poolOptInAuth("ollama-key")
|
|
plain := &cliproxyauth.Auth{ID: "other-key"}
|
|
|
|
pooled := pooledProxyTransport("direct", cfg, optedIn)
|
|
if pooled == nil {
|
|
t.Fatal("opted-in credential got no transport")
|
|
}
|
|
if pooled.IdleConnTimeout != proxyPoolDefaultIdleConnTimeout {
|
|
t.Fatalf("IdleConnTimeout = %s, want default %s", pooled.IdleConnTimeout, proxyPoolDefaultIdleConnTimeout)
|
|
}
|
|
if pooledProxyTransport("direct", cfg, optedIn) != pooled {
|
|
t.Fatal("opted-in credential does not reuse its pool")
|
|
}
|
|
if pooledProxyTransport("direct", cfg, plain) == pooled {
|
|
t.Fatal("credential without the block shared the pool")
|
|
}
|
|
first := pooledProxyTransport("direct", cfg, plain)
|
|
second := pooledProxyTransport("direct", cfg, plain)
|
|
if first == second {
|
|
t.Fatal("credential without the block must not be pooled")
|
|
}
|
|
// A nil config still honours the credential block: pooling no longer needs a config.
|
|
if tr := pooledProxyTransport("direct", nil, optedIn); tr == nil || tr != pooled {
|
|
t.Fatal("nil config must still honour the credential block")
|
|
}
|
|
}
|
|
|
|
// TestProxyPoolExplicitDisable covers a key opting out of its group block.
|
|
func TestProxyPoolExplicitDisable(t *testing.T) {
|
|
ResetProxyTransports()
|
|
t.Cleanup(ResetProxyTransports)
|
|
|
|
optedOut := poolAuth("no-pool", &cliproxyauth.ProxyConnectionPoolOverride{Enabled: boolPtr(false)})
|
|
first := pooledProxyTransport("direct", testConfig(), optedOut)
|
|
second := pooledProxyTransport("direct", testConfig(), optedOut)
|
|
if first == nil || second == nil {
|
|
t.Fatal("transport missing")
|
|
}
|
|
if first == second {
|
|
t.Fatal("credential that disabled pooling was still pooled")
|
|
}
|
|
if proxyTransportCache.Len() != 0 {
|
|
t.Fatalf("opted-out credential polluted the cache: %d entries", proxyTransportCache.Len())
|
|
}
|
|
}
|
|
|
|
// TestProxyPoolPartialSettings covers partial blocks: omitted fields use the defaults.
|
|
func TestProxyPoolPartialSettings(t *testing.T) {
|
|
ResetProxyTransports()
|
|
t.Cleanup(ResetProxyTransports)
|
|
|
|
partial := poolAuth("partial", &cliproxyauth.ProxyConnectionPoolOverride{IdleConnTimeout: "45s"})
|
|
tr := pooledProxyTransport("direct", testConfig(), partial)
|
|
if tr == nil {
|
|
t.Fatal("pooled transport missing")
|
|
}
|
|
if tr.IdleConnTimeout != 45*time.Second {
|
|
t.Fatalf("IdleConnTimeout = %s, want 45s from the block", tr.IdleConnTimeout)
|
|
}
|
|
if tr.MaxIdleConnsPerHost != proxyPoolDefaultMaxIdleConnsPerHost {
|
|
t.Fatalf("MaxIdleConnsPerHost = %d, want the default %d", tr.MaxIdleConnsPerHost, proxyPoolDefaultMaxIdleConnsPerHost)
|
|
}
|
|
|
|
other := poolAuth("partial-2", &cliproxyauth.ProxyConnectionPoolOverride{IdleConnTimeout: "60s"})
|
|
if pooledProxyTransport("direct", testConfig(), other) == tr {
|
|
t.Fatal("different settings shared one pool")
|
|
}
|
|
}
|
|
|
|
// TestProxyPoolCredentialOptInWithoutProxyURL pins that pooling does not require a
|
|
// proxy-url: the inherited environment-proxy behavior is preserved.
|
|
func TestProxyPoolCredentialOptInWithoutProxyURL(t *testing.T) {
|
|
ResetProxyTransports()
|
|
t.Cleanup(ResetProxyTransports)
|
|
|
|
cfg := testConfig()
|
|
auth := poolOptInAuth("no-proxy-url")
|
|
pooled := pooledProxyTransport("", cfg, auth)
|
|
if pooled == nil {
|
|
t.Fatal("opted-in credential without proxy-url got no transport")
|
|
}
|
|
if pooled.Proxy == nil {
|
|
t.Fatal("inherited environment proxy behavior must be preserved")
|
|
}
|
|
if pooledProxyTransport("", cfg, auth) != pooled {
|
|
t.Fatal("inherited pool not reused")
|
|
}
|
|
// A credential without the block keeps the shared default transport.
|
|
if tr := pooledProxyTransport("", cfg, &cliproxyauth.Auth{ID: "plain"}); tr != nil {
|
|
t.Fatal("unopted credential must keep using http.DefaultTransport")
|
|
}
|
|
// The generic client must wire the pooled transport in.
|
|
client := NewProxyAwareHTTPClient(context.Background(), cfg, auth, 0)
|
|
if client.Transport != pooled {
|
|
t.Fatalf("client transport = %T, want the pooled transport", client.Transport)
|
|
}
|
|
// When proxy-url is empty, an explicitly injected context RoundTripper must keep
|
|
// priority over the implicit credential pool, matching NewUtlsHTTPClient.
|
|
injected := roundTripperFunc(func(*http.Request) (*http.Response, error) { return nil, nil })
|
|
ctxWithRT := context.WithValue(context.Background(), "cliproxy.roundtripper", injected)
|
|
injectedClient := NewProxyAwareHTTPClient(ctxWithRT, cfg, auth, 0)
|
|
if injectedClient.Transport == pooled {
|
|
t.Fatal("context RoundTripper must take priority when proxy-url is empty")
|
|
}
|
|
// Zero or negative pool bounds with an empty proxy-url must still return a
|
|
// transport with DisableKeepAlives=true rather than nil (which would fall back
|
|
// to http.DefaultTransport and keep pooling).
|
|
for _, raw := range []string{"0s", "-1s"} {
|
|
zeroIdle := poolAuth("zero-no-proxy", &cliproxyauth.ProxyConnectionPoolOverride{IdleConnTimeout: raw})
|
|
if got := pooledProxyTransport("", cfg, zeroIdle); got == nil || !got.DisableKeepAlives {
|
|
t.Fatalf("empty proxy-url with idle %s must disable keep-alives, got %+v", raw, got)
|
|
}
|
|
}
|
|
for _, max := range []int{0, -1} {
|
|
val := max
|
|
zeroMax := poolAuth("max-no-proxy", &cliproxyauth.ProxyConnectionPoolOverride{MaxIdleConnsPerHost: &val})
|
|
if got := pooledProxyTransport("", cfg, zeroMax); got == nil || !got.DisableKeepAlives {
|
|
t.Fatalf("empty proxy-url with max %d must disable keep-alives, got %+v", max, got)
|
|
}
|
|
}
|
|
}
|