feat(config): add per-api-key connection-pool settings

Add an optional `connection-pool` block to the api-key config surface: group
level on codex/claude/gemini/vertex/openai-compatibility entries and per key
inside `keys`. There is deliberately no global switch, so enabling reuse for one
key cannot change the transport lifecycle of another provider or OAuth identity.

- `connection-pool` joins the v8 shared key fields, so a group block is flattened
  into every key and each key may override it.
- A per-key block merges onto the group block: fields the key does not mention
  (or sets to null) keep inheriting the group value. Replacing the whole block
  would silently drop the group's `enabled: true` when a key only overrides a
  timeout.
- The resolved block reaches executors on `Auth.ProxyConnectionPool` (deep-copied
  on clone so auth snapshots never share mutable state). Writing the block is the
  opt-in; only an explicit `enabled: false` turns pooling off for that key.
- OpenAI-compatible keys use the same field-level inheritance against their
  provider group default, and watcher config diffs report connection-pool
  changes on hot reload.
This commit is contained in:
sususu
2026-10-29 10:21:03 +08:00
parent b63322b9c3
commit bf834311e6
11 changed files with 574 additions and 23 deletions

View File

@@ -344,3 +344,25 @@ func keyForPath(key reflect.Value) string {
}
return key.Type().String()
}
func TestParseConfigBytes_ProxyConnectionPool(t *testing.T) {
cfg, err := ParseConfigBytes([]byte("api-keys:\n codex:\n - name: pool-group\n base-url: https://ollama.com/v1\n connection-pool:\n enabled: true\n idle-conn-timeout: 15s\n max-idle-conns-per-host: 4\n keys:\n - api-key: pool-key\n"))
if err != nil {
t.Fatal(err)
}
if len(cfg.CodexKey) != 1 {
t.Fatalf("codex entries = %d, want 1", len(cfg.CodexKey))
}
pool := cfg.CodexKey[0].ConnectionPool
if pool == nil || pool.Enabled == nil || !*pool.Enabled || pool.IdleConnTimeout != "15s" || pool.MaxIdleConnsPerHost == nil || *pool.MaxIdleConnsPerHost != 4 {
t.Fatalf("unexpected pool config: %+v", pool)
}
clone := cfg.CloneForRuntime()
if clone.CodexKey[0].ConnectionPool == pool {
t.Fatal("clone shares the pool block")
}
*clone.CodexKey[0].ConnectionPool.Enabled = false
if !*cfg.CodexKey[0].ConnectionPool.Enabled {
t.Fatal("clone shares enabled pointer")
}
}

View File

@@ -202,6 +202,33 @@ type DevinConfig struct {
SensitiveWords []string `yaml:"sensitive-words,omitempty" json:"sensitive-words,omitempty"`
}
// ProxyConnectionPoolConfig configures upstream connection reuse for one api-key
// credential (group level or per key).
//
// Pools are per credential: two API keys never share a connection, and OAuth
// credentials are not poolable at all (Antigravity keeps its own switch). The
// transport cache is bounded, so a deployment with very many keys degrades to the
// historical one-transport-per-request behavior instead of growing without limit.
type ProxyConnectionPoolConfig struct {
// Enabled controls whether connection reuse is active for this credential.
// Writing the connection-pool block is the opt-in, so an omitted or true
// value enables pooling; an explicit false opts a key out of its group pool.
Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
// IdleConnTimeout is how long an idle pooled connection survives before it
// is closed. Defaults to "300s". Measured against ollama.com, an idle
// connection was still reused after 600s, so 300s sits inside the upstream
// window while covering the multi-minute gaps between interactive requests.
// Zero or a negative value disables keep-alives, so every request dials
// afresh.
IdleConnTimeout string `yaml:"idle-conn-timeout,omitempty" json:"idle-conn-timeout,omitempty"`
// MaxIdleConnsPerHost caps idle connections kept per upstream host.
// Defaults to 2, matching net/http's DefaultMaxIdleConnsPerHost. Zero or a
// negative value disables keep-alives as well.
MaxIdleConnsPerHost *int `yaml:"max-idle-conns-per-host,omitempty" json:"max-idle-conns-per-host,omitempty"`
}
// AntigravityConfig configures provider-wide Antigravity request behavior.
type AntigravityConfig struct {
// SensitiveWords is a list of words to obfuscate with zero-width characters in system instructions.
@@ -656,6 +683,11 @@ type ClaudeKey struct {
// ProxyURL overrides the global proxy setting for this API key if provided.
ProxyURL string `yaml:"proxy-url" json:"proxy-url"`
// ConnectionPool configures upstream connection reuse for this credential.
// Omitted disables pooling (preserving the fresh-transport-per-request default);
// pools stay per credential, so opting in never shares connections with another key.
ConnectionPool *ProxyConnectionPoolConfig `yaml:"connection-pool,omitempty" json:"connection-pool,omitempty"`
// Models defines upstream model names and aliases for request routing.
Models []ClaudeModel `yaml:"models" json:"models"`
@@ -782,6 +814,11 @@ type CodexKey struct {
// ProxyURL overrides the global proxy setting for this API key if provided.
ProxyURL string `yaml:"proxy-url" json:"proxy-url"`
// ConnectionPool configures upstream connection reuse for this credential.
// Omitted disables pooling (preserving the fresh-transport-per-request default);
// pools stay per credential, so opting in never shares connections with another key.
ConnectionPool *ProxyConnectionPoolConfig `yaml:"connection-pool,omitempty" json:"connection-pool,omitempty"`
// Models defines upstream model names and aliases for request routing.
Models []CodexModel `yaml:"models" json:"models"`
@@ -893,6 +930,11 @@ type GeminiKey struct {
// ProxyURL optionally overrides the global proxy for this API key.
ProxyURL string `yaml:"proxy-url,omitempty" json:"proxy-url,omitempty"`
// ConnectionPool configures upstream connection reuse for this credential.
// Omitted disables pooling (preserving the fresh-transport-per-request default);
// pools stay per credential, so opting in never shares connections with another key.
ConnectionPool *ProxyConnectionPoolConfig `yaml:"connection-pool,omitempty" json:"connection-pool,omitempty"`
// Models defines upstream model names and aliases for request routing.
Models []GeminiModel `yaml:"models,omitempty" json:"models,omitempty"`
@@ -980,6 +1022,10 @@ type OpenAICompatibility struct {
// APIKeyEntries defines API keys with optional per-key proxy configuration.
APIKeyEntries []OpenAICompatibilityAPIKey `yaml:"api-key-entries,omitempty" json:"api-key-entries,omitempty"`
// ConnectionPool is the provider-group default for upstream connection reuse.
// A per-key connection-pool entry overrides it field by field; omitted disables pooling.
ConnectionPool *ProxyConnectionPoolConfig `yaml:"connection-pool,omitempty" json:"connection-pool,omitempty"`
// Models defines the model configurations including aliases for routing.
Models []OpenAICompatibilityModel `yaml:"models" json:"models"`
@@ -1012,6 +1058,11 @@ type OpenAICompatibilityAPIKey struct {
// ProxyURL overrides the global proxy setting for this API key if provided.
ProxyURL string `yaml:"proxy-url,omitempty" json:"proxy-url,omitempty"`
// ConnectionPool configures upstream connection reuse for this key, overriding
// the provider-group default field by field. Pools stay per credential, so
// opting in never shares connections with another key.
ConnectionPool *ProxyConnectionPoolConfig `yaml:"connection-pool,omitempty" json:"connection-pool,omitempty"`
}
// OpenAICompatibilityModel represents a model configuration for OpenAI compatibility,

View File

@@ -385,6 +385,9 @@ var sharedKeyFields = map[string]bool{
"priority": true, "prefix": true, "proxy-url": true, "headers": true,
"models": true, "excluded-models": true, "disable-cooling": true,
"request-retry": true, "request-scoped-errors": true,
// connection-pool follows proxy-url: a group-level default that each key may
// override, so an operator can pool one provider without touching the others.
"connection-pool": true,
}
func expandV8Groups(groups *yaml.Node, provider string) (*yaml.Node, error) {
@@ -438,12 +441,22 @@ func expandV8Groups(groups *yaml.Node, provider string) (*yaml.Node, error) {
}
}
for i := 0; i < len(key.Content); i += 2 {
if key.Content[i].Value == "auth_index" || key.Content[i].Value == "auth-index" {
field := key.Content[i].Value
if field == "auth_index" || field == "auth-index" {
continue
}
if key.Content[i+1].Tag != "!!null" {
setYAMLPath(item, key.Content[i].Value, key.Content[i+1])
if key.Content[i+1].Tag == "!!null" {
continue
}
if field == "connection-pool" {
// A per-key block merges onto the group block so a key that overrides
// one field keeps inheriting the rest, matching the runtime resolution
// where an omitted field inherits the outer value. Replacing the whole
// block would silently drop the group's enabled: true.
setYAMLPath(item, field, mergeConnectionPoolNode(yamlPath(group, field), key.Content[i+1]))
continue
}
setYAMLPath(item, field, key.Content[i+1])
}
out.Content = append(out.Content, item)
}
@@ -451,6 +464,22 @@ func expandV8Groups(groups *yaml.Node, provider string) (*yaml.Node, error) {
return out, nil
}
// mergeConnectionPoolNode merges a per-key connection-pool block onto the group block.
// Missing sub-keys fall back to the group value; the result is always a fresh node.
func mergeConnectionPoolNode(base, override *yaml.Node) *yaml.Node {
merged := deepCopyNode(base)
if merged == nil || merged.Kind != yaml.MappingNode || override == nil || override.Kind != yaml.MappingNode {
return deepCopyNode(override)
}
for i := 0; i < len(override.Content); i += 2 {
if override.Content[i+1].Tag == "!!null" {
continue
}
setYAMLPath(merged, override.Content[i].Value, override.Content[i+1])
}
return merged
}
// groupLegacyKeys deliberately keeps one group per legacy entry: equal endpoints
// do not imply equal routing, headers, models, or credential policies.
func groupLegacyKeys(keys *yaml.Node, provider string) *yaml.Node {

View File

@@ -0,0 +1,134 @@
package config_test
import (
"testing"
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
)
// TestPerEntryConnectionPoolAccepted pins the per-credential opt-in shape: the
// global pool stays off while a single api-keys entry turns pooling on for itself.
func TestPerEntryConnectionPoolAccepted(t *testing.T) {
raw := []byte(`config-version: 8
api-keys:
codex:
- name: ollama
base-url: "https://ollama.com/v1"
proxy-url: "direct"
connection-pool:
enabled: true
idle-conn-timeout: "300s"
max-idle-conns-per-host: 2
keys:
- api-key: "ollama-key"
`)
if err := config.ValidateV8Config(raw); err != nil {
t.Fatalf("ValidateV8Config: %v", err)
}
cfg, err := config.ParseConfigBytes(raw)
if err != nil {
t.Fatalf("ParseConfigBytes: %v", err)
}
if len(cfg.CodexKey) != 1 || cfg.CodexKey[0].ConnectionPool == nil {
t.Fatalf("per-entry connection-pool not parsed: %+v", cfg.CodexKey)
}
pool := cfg.CodexKey[0].ConnectionPool
if pool.Enabled == nil || !*pool.Enabled {
t.Fatalf("enabled not parsed: %+v", pool)
}
if pool.IdleConnTimeout != "300s" {
t.Fatalf("idle-conn-timeout = %q, want 300s", pool.IdleConnTimeout)
}
if pool.MaxIdleConnsPerHost == nil || *pool.MaxIdleConnsPerHost != 2 {
t.Fatalf("max-idle-conns-per-host not parsed: %+v", pool)
}
if errLegacy := config.ValidateV8Config([]byte("config-version: 8\nrequests:\n proxy-connection-pool:\n enabled: true\n")); errLegacy == nil {
t.Fatal("a global pooling switch must not exist")
}
}
// TestPerEntryConnectionPoolRejectsUnknownKey keeps KnownFields strict inside the
// new nested block.
func TestPerEntryConnectionPoolRejectsUnknownKey(t *testing.T) {
raw := []byte(`config-version: 8
api-keys:
codex:
- name: k
keys:
- api-key: "k"
connection-pool:
enabled: true
idle-timeout: "30s"
`)
if err := config.ValidateV8Config(raw); err == nil {
t.Fatal("unknown nested key must be rejected")
}
}
// TestPerKeyConnectionPoolOverridesGroup pins per-key control inside one provider
// group: keys may switch pooling on or off independently, and a partial override keeps
// inheriting the group values it does not mention.
func TestPerKeyConnectionPoolOverridesGroup(t *testing.T) {
raw := []byte(`config-version: 8
api-keys:
codex:
- name: ollama
base-url: "https://ollama.com/v1"
connection-pool:
enabled: true
idle-conn-timeout: "300s"
max-idle-conns-per-host: 2
keys:
- api-key: "inherits-group"
- api-key: "opts-out"
connection-pool:
enabled: false
- api-key: "partial"
connection-pool:
idle-conn-timeout: "60s"
- api-key: "null-subkey"
connection-pool:
idle-conn-timeout: null
max-idle-conns-per-host: 4
`)
cfg, err := config.ParseConfigBytes(raw)
if err != nil {
t.Fatalf("ParseConfigBytes: %v", err)
}
byKey := map[string]*config.ProxyConnectionPoolConfig{}
for i := range cfg.CodexKey {
byKey[cfg.CodexKey[i].APIKey] = cfg.CodexKey[i].ConnectionPool
}
inherits := byKey["inherits-group"]
if inherits == nil || inherits.Enabled == nil || !*inherits.Enabled || inherits.IdleConnTimeout != "300s" {
t.Fatalf("key without an override must inherit the group block, got %+v", inherits)
}
optedOut := byKey["opts-out"]
if optedOut == nil || optedOut.Enabled == nil || *optedOut.Enabled {
t.Fatalf("per-key opt-out not honoured, got %+v", optedOut)
}
partial := byKey["partial"]
if partial == nil || partial.Enabled == nil || !*partial.Enabled {
t.Fatalf("partial override must keep the group's enabled value, got %+v", partial)
}
if partial.IdleConnTimeout != "60s" {
t.Fatalf("partial override idle = %q, want 60s", partial.IdleConnTimeout)
}
if partial.MaxIdleConnsPerHost == nil || *partial.MaxIdleConnsPerHost != 2 {
t.Fatalf("partial override must inherit max-idle-conns-per-host, got %+v", partial)
}
nullSubkey := byKey["null-subkey"]
if nullSubkey == nil || nullSubkey.Enabled == nil || !*nullSubkey.Enabled {
t.Fatalf("null-subkey override must inherit group enabled, got %+v", nullSubkey)
}
if nullSubkey.IdleConnTimeout != "300s" {
t.Fatalf("null sub-key must inherit group idle-conn-timeout 300s, got %q", nullSubkey.IdleConnTimeout)
}
if nullSubkey.MaxIdleConnsPerHost == nil || *nullSubkey.MaxIdleConnsPerHost != 4 {
t.Fatalf("null-subkey override max-idle-conns-per-host = %v, want 4", nullSubkey.MaxIdleConnsPerHost)
}
}

View File

@@ -39,6 +39,11 @@ type VertexCompatKey struct {
// ProxyURL optionally overrides the global proxy for this API key.
ProxyURL string `yaml:"proxy-url,omitempty" json:"proxy-url,omitempty"`
// ConnectionPool configures upstream connection reuse for this credential.
// Omitted disables pooling (preserving the fresh-transport-per-request default);
// pools stay per credential, so opting in never shares connections with another key.
ConnectionPool *ProxyConnectionPoolConfig `yaml:"connection-pool,omitempty" json:"connection-pool,omitempty"`
// Headers optionally adds extra HTTP headers for requests sent with this key.
// Commonly used for cookies, user-agent, and other authentication headers.
Headers map[string]string `yaml:"headers,omitempty" json:"headers,omitempty"`

View File

@@ -219,6 +219,7 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string {
if strings.TrimSpace(o.ProxyURL) != strings.TrimSpace(n.ProxyURL) {
changes = append(changes, fmt.Sprintf("gemini[%d].proxy-url: %s -> %s", i, formatProxyURL(o.ProxyURL), formatProxyURL(n.ProxyURL)))
}
changes = appendConnectionPoolChanges(changes, fmt.Sprintf("gemini[%d].connection-pool", i), o.ConnectionPool, n.ConnectionPool)
if strings.TrimSpace(o.Prefix) != strings.TrimSpace(n.Prefix) {
changes = append(changes, fmt.Sprintf("gemini[%d].prefix: %s -> %s", i, strings.TrimSpace(o.Prefix), strings.TrimSpace(n.Prefix)))
}
@@ -254,6 +255,7 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string {
if strings.TrimSpace(o.ProxyURL) != strings.TrimSpace(n.ProxyURL) {
changes = append(changes, fmt.Sprintf("interactions[%d].proxy-url: %s -> %s", i, formatProxyURL(o.ProxyURL), formatProxyURL(n.ProxyURL)))
}
changes = appendConnectionPoolChanges(changes, fmt.Sprintf("interactions[%d].connection-pool", i), o.ConnectionPool, n.ConnectionPool)
if strings.TrimSpace(o.Prefix) != strings.TrimSpace(n.Prefix) {
changes = append(changes, fmt.Sprintf("interactions[%d].prefix: %s -> %s", i, strings.TrimSpace(o.Prefix), strings.TrimSpace(n.Prefix)))
}
@@ -291,6 +293,7 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string {
if strings.TrimSpace(o.ProxyURL) != strings.TrimSpace(n.ProxyURL) {
changes = append(changes, fmt.Sprintf("claude[%d].proxy-url: %s -> %s", i, formatProxyURL(o.ProxyURL), formatProxyURL(n.ProxyURL)))
}
changes = appendConnectionPoolChanges(changes, fmt.Sprintf("claude[%d].connection-pool", i), o.ConnectionPool, n.ConnectionPool)
if strings.TrimSpace(o.Prefix) != strings.TrimSpace(n.Prefix) {
changes = append(changes, fmt.Sprintf("claude[%d].prefix: %s -> %s", i, strings.TrimSpace(o.Prefix), strings.TrimSpace(n.Prefix)))
}
@@ -348,6 +351,7 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string {
if strings.TrimSpace(o.ProxyURL) != strings.TrimSpace(n.ProxyURL) {
changes = append(changes, fmt.Sprintf("codex[%d].proxy-url: %s -> %s", i, formatProxyURL(o.ProxyURL), formatProxyURL(n.ProxyURL)))
}
changes = appendConnectionPoolChanges(changes, fmt.Sprintf("codex[%d].connection-pool", i), o.ConnectionPool, n.ConnectionPool)
if strings.TrimSpace(o.Prefix) != strings.TrimSpace(n.Prefix) {
changes = append(changes, fmt.Sprintf("codex[%d].prefix: %s -> %s", i, strings.TrimSpace(o.Prefix), strings.TrimSpace(n.Prefix)))
}
@@ -392,6 +396,7 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string {
if strings.TrimSpace(o.ProxyURL) != strings.TrimSpace(n.ProxyURL) {
changes = append(changes, fmt.Sprintf("xai[%d].proxy-url: %s -> %s", i, formatProxyURL(o.ProxyURL), formatProxyURL(n.ProxyURL)))
}
changes = appendConnectionPoolChanges(changes, fmt.Sprintf("xai[%d].connection-pool", i), o.ConnectionPool, n.ConnectionPool)
if strings.TrimSpace(o.Prefix) != strings.TrimSpace(n.Prefix) {
changes = append(changes, fmt.Sprintf("xai[%d].prefix: %s -> %s", i, strings.TrimSpace(o.Prefix), strings.TrimSpace(n.Prefix)))
}
@@ -435,6 +440,7 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string {
if strings.TrimSpace(o.ProxyURL) != strings.TrimSpace(n.ProxyURL) {
changes = append(changes, fmt.Sprintf("meta[%d].proxy-url: %s -> %s", i, formatProxyURL(o.ProxyURL), formatProxyURL(n.ProxyURL)))
}
changes = appendConnectionPoolChanges(changes, fmt.Sprintf("meta[%d].connection-pool", i), o.ConnectionPool, n.ConnectionPool)
if strings.TrimSpace(o.Prefix) != strings.TrimSpace(n.Prefix) {
changes = append(changes, fmt.Sprintf("meta[%d].prefix: %s -> %s", i, o.Prefix, n.Prefix))
}
@@ -527,6 +533,7 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string {
if strings.TrimSpace(o.ProxyURL) != strings.TrimSpace(n.ProxyURL) {
changes = append(changes, fmt.Sprintf("vertex[%d].proxy-url: %s -> %s", i, formatProxyURL(o.ProxyURL), formatProxyURL(n.ProxyURL)))
}
changes = appendConnectionPoolChanges(changes, fmt.Sprintf("vertex[%d].connection-pool", i), o.ConnectionPool, n.ConnectionPool)
if strings.TrimSpace(o.Prefix) != strings.TrimSpace(n.Prefix) {
changes = append(changes, fmt.Sprintf("vertex[%d].prefix: %s -> %s", i, strings.TrimSpace(o.Prefix), strings.TrimSpace(n.Prefix)))
}
@@ -589,6 +596,39 @@ func appendPayloadFilterRuleChanges(changes []string, section string, oldRules,
return append(changes, fmt.Sprintf("payload.%s: updated (%d -> %d rules)", section, len(oldRules), len(newRules)))
}
func appendConnectionPoolChanges(changes []string, prefix string, oldPool, newPool *config.ProxyConnectionPoolConfig) []string {
if oldPool == nil && newPool == nil {
return changes
}
var oldCfg, newCfg config.ProxyConnectionPoolConfig
if oldPool != nil {
oldCfg = *oldPool
}
if newPool != nil {
newCfg = *newPool
}
oldEnabled := formatConnectionPoolEnabled(oldPool)
newEnabled := formatConnectionPoolEnabled(newPool)
if oldEnabled != newEnabled {
changes = append(changes, fmt.Sprintf("%s.enabled: %s -> %s", prefix, oldEnabled, newEnabled))
}
if strings.TrimSpace(oldCfg.IdleConnTimeout) != strings.TrimSpace(newCfg.IdleConnTimeout) {
changes = append(changes, fmt.Sprintf("%s.idle-conn-timeout: %q -> %q", prefix, strings.TrimSpace(oldCfg.IdleConnTimeout), strings.TrimSpace(newCfg.IdleConnTimeout)))
}
changes = appendOptionalIntChange(changes, prefix+".max-idle-conns-per-host", oldCfg.MaxIdleConnsPerHost, newCfg.MaxIdleConnsPerHost)
return changes
}
func formatConnectionPoolEnabled(pool *config.ProxyConnectionPoolConfig) string {
if pool == nil {
return "<unset>"
}
if pool.Enabled == nil {
return "true"
}
return fmt.Sprintf("%t", *pool.Enabled)
}
func appendOptionalIntChange(changes []string, field string, oldVal, newVal *int) []string {
if optionalIntEqual(oldVal, newVal) {
return changes

View File

@@ -774,3 +774,38 @@ func TestBuildConfigChangeDetails_ClaudeNativeToolSearch(t *testing.T) {
newCfg := &config.Config{ClaudeKey: []config.ClaudeKey{{APIKey: "key", BaseURL: "https://gateway.example.com", NativeToolSearch: true}}}
expectContains(t, BuildConfigChangeDetails(oldCfg, newCfg), "claude[0].native-tool-search: false -> true")
}
func TestBuildConfigChangeDetails_APIKeyConnectionPool(t *testing.T) {
enabled := true
maxIdle := 4
oldCfg := &config.Config{
CodexKey: []config.CodexKey{{APIKey: "k", BaseURL: "https://ollama.com/v1"}},
OpenAICompatibility: []config.OpenAICompatibility{{
Name: "compat",
BaseURL: "https://compat.example.com",
APIKeyEntries: []config.OpenAICompatibilityAPIKey{{APIKey: "k"}},
}},
}
newCfg := &config.Config{
CodexKey: []config.CodexKey{{
APIKey: "k",
BaseURL: "https://ollama.com/v1",
ConnectionPool: &config.ProxyConnectionPoolConfig{
Enabled: &enabled,
IdleConnTimeout: "300s",
MaxIdleConnsPerHost: &maxIdle,
},
}},
OpenAICompatibility: []config.OpenAICompatibility{{
Name: "compat",
BaseURL: "https://compat.example.com",
ConnectionPool: &config.ProxyConnectionPoolConfig{Enabled: &enabled},
APIKeyEntries: []config.OpenAICompatibilityAPIKey{{APIKey: "k"}},
}},
}
changes := BuildConfigChangeDetails(oldCfg, newCfg)
expectContains(t, changes, "codex[0].connection-pool.enabled: <unset> -> true")
expectContains(t, changes, `codex[0].connection-pool.idle-conn-timeout: "" -> "300s"`)
expectContains(t, changes, "codex[0].connection-pool.max-idle-conns-per-host: <unset> -> 4")
expectContains(t, changes, " provider updated: compat (connection-pool updated)")
}

View File

@@ -98,12 +98,52 @@ func describeOpenAICompatibilityUpdate(oldEntry, newEntry config.OpenAICompatibi
if !equalStringMap(oldEntry.Headers, newEntry.Headers) {
details = append(details, "headers updated")
}
if openAICompatConnectionPoolChanged(oldEntry, newEntry) {
details = append(details, "connection-pool updated")
}
if len(details) == 0 {
return ""
}
return "(" + strings.Join(details, ", ") + ")"
}
func openAICompatConnectionPoolChanged(oldEntry, newEntry config.OpenAICompatibility) bool {
if !connectionPoolConfigEqual(oldEntry.ConnectionPool, newEntry.ConnectionPool) {
return true
}
if len(oldEntry.APIKeyEntries) != len(newEntry.APIKeyEntries) {
for _, item := range oldEntry.APIKeyEntries {
if item.ConnectionPool != nil {
return true
}
}
for _, item := range newEntry.APIKeyEntries {
if item.ConnectionPool != nil {
return true
}
}
return false
}
for i := range oldEntry.APIKeyEntries {
if !connectionPoolConfigEqual(oldEntry.APIKeyEntries[i].ConnectionPool, newEntry.APIKeyEntries[i].ConnectionPool) {
return true
}
}
return false
}
func connectionPoolConfigEqual(a, b *config.ProxyConnectionPoolConfig) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return optionalBoolEqual(a.Enabled, b.Enabled) &&
strings.TrimSpace(a.IdleConnTimeout) == strings.TrimSpace(b.IdleConnTimeout) &&
optionalIntEqual(a.MaxIdleConnsPerHost, b.MaxIdleConnsPerHost)
}
func countAPIKeys(entry config.OpenAICompatibility) int {
count := 0
for _, keyEntry := range entry.APIKeyEntries {

View File

@@ -21,6 +21,54 @@ func NewConfigSynthesizer() *ConfigSynthesizer {
return &ConfigSynthesizer{}
}
// authProxyConnectionPool converts a per-credential pool override from config into
// the SDK representation carried on the auth record. Values are copied so the auth
// snapshot never aliases a live config field.
func authProxyConnectionPool(pool *config.ProxyConnectionPoolConfig) *coreauth.ProxyConnectionPoolOverride {
if pool == nil {
return nil
}
out := &coreauth.ProxyConnectionPoolOverride{IdleConnTimeout: pool.IdleConnTimeout}
if pool.Enabled != nil {
enabled := *pool.Enabled
out.Enabled = &enabled
}
if pool.MaxIdleConnsPerHost != nil {
maxIdle := *pool.MaxIdleConnsPerHost
out.MaxIdleConnsPerHost = &maxIdle
}
if out.Enabled == nil && out.IdleConnTimeout == "" && out.MaxIdleConnsPerHost == nil {
return nil
}
return out
}
// openAICompatConnectionPool resolves the pool override for one OpenAI-compatible
// key: the per-key block wins field by field, so a key that overrides only one value
// keeps inheriting the provider-group default for the rest.
func openAICompatConnectionPool(compat *config.OpenAICompatibility, entry *config.OpenAICompatibilityAPIKey) *config.ProxyConnectionPoolConfig {
var group *config.ProxyConnectionPoolConfig
if compat != nil {
group = compat.ConnectionPool
}
if entry == nil || entry.ConnectionPool == nil {
return group
}
merged := *entry.ConnectionPool
if group != nil {
if merged.Enabled == nil {
merged.Enabled = group.Enabled
}
if strings.TrimSpace(merged.IdleConnTimeout) == "" {
merged.IdleConnTimeout = group.IdleConnTimeout
}
if merged.MaxIdleConnsPerHost == nil {
merged.MaxIdleConnsPerHost = group.MaxIdleConnsPerHost
}
}
return &merged
}
func addWeightToAttrs(weight *int, attrs map[string]string) {
if weight == nil {
return
@@ -122,6 +170,8 @@ func (s *ConfigSynthesizer) synthesizeGeminiKeyEntries(ctx *SynthesisContext, en
Metadata: metadata,
CreatedAt: now,
UpdatedAt: now,
ProxyConnectionPool: authProxyConnectionPool(entry.ConnectionPool),
}
ApplyAuthExcludedModelsMeta(a, entry.ExcludedModels, "apikey")
if len(a.Metadata) == 0 {
@@ -193,6 +243,8 @@ func (s *ConfigSynthesizer) synthesizeClaudeKeys(ctx *SynthesisContext) []*corea
Metadata: metadata,
CreatedAt: now,
UpdatedAt: now,
ProxyConnectionPool: authProxyConnectionPool(ck.ConnectionPool),
}
ApplyAuthExcludedModelsMeta(a, ck.ExcludedModels, "apikey")
if len(a.Metadata) == 0 {
@@ -267,16 +319,17 @@ func (s *ConfigSynthesizer) synthesizeCodexStyleKeys(ctx *SynthesisContext, entr
}
addConfigHeadersToAttrs(entry.Headers, attrs)
a := &coreauth.Auth{
ID: id,
Provider: provider,
Label: provider + "-apikey",
Prefix: prefix,
Status: coreauth.StatusActive,
ProxyURL: strings.TrimSpace(entry.ProxyURL),
Attributes: attrs,
Metadata: metadata,
CreatedAt: now,
UpdatedAt: now,
ID: id,
Provider: provider,
Label: provider + "-apikey",
Prefix: prefix,
Status: coreauth.StatusActive,
ProxyURL: strings.TrimSpace(entry.ProxyURL),
Attributes: attrs,
Metadata: metadata,
CreatedAt: now,
UpdatedAt: now,
ProxyConnectionPool: authProxyConnectionPool(entry.ConnectionPool),
}
ApplyAuthExcludedModelsMeta(a, entry.ExcludedModels, "apikey")
if len(a.Metadata) == 0 {
@@ -341,16 +394,17 @@ func (s *ConfigSynthesizer) synthesizeOpenAICompat(ctx *SynthesisContext) []*cor
}
addConfigHeadersToAttrs(compat.Headers, attrs)
a := &coreauth.Auth{
ID: id,
Provider: internalProviderKey,
Label: compat.Name,
Prefix: prefix,
Status: coreauth.StatusActive,
ProxyURL: proxyURL,
Attributes: attrs,
Metadata: metadata,
CreatedAt: now,
UpdatedAt: now,
ID: id,
Provider: internalProviderKey,
Label: compat.Name,
Prefix: prefix,
Status: coreauth.StatusActive,
ProxyURL: proxyURL,
Attributes: attrs,
Metadata: metadata,
CreatedAt: now,
UpdatedAt: now,
ProxyConnectionPool: authProxyConnectionPool(openAICompatConnectionPool(compat, entry)),
}
if len(a.Metadata) == 0 {
a.Metadata = nil
@@ -461,6 +515,8 @@ func (s *ConfigSynthesizer) synthesizeVertexCompat(ctx *SynthesisContext) []*cor
Metadata: metadata,
CreatedAt: now,
UpdatedAt: now,
ProxyConnectionPool: authProxyConnectionPool(compat.ConnectionPool),
}
ApplyAuthExcludedModelsMeta(a, compat.ExcludedModels, "apikey")
if len(a.Metadata) == 0 {

View File

@@ -1340,3 +1340,110 @@ func TestConfigSynthesizer_RequestScopedErrors(t *testing.T) {
}
}
}
// TestConfigSynthesizer_ConnectionPoolOverride pins the per-credential pool opt-in:
// the override declared on an API-key entry must reach the auth record so the
// generic proxy client can pool just that credential.
func TestConfigSynthesizer_ConnectionPoolOverride(t *testing.T) {
enabled := true
maxIdle := 4
cfg := &config.Config{
CodexKey: []config.CodexKey{{
APIKey: "ollama-key",
BaseURL: "https://ollama.com/v1",
ProxyURL: "direct",
ConnectionPool: &config.ProxyConnectionPoolConfig{
Enabled: &enabled,
IdleConnTimeout: "300s",
MaxIdleConnsPerHost: &maxIdle,
},
}},
ClaudeKey: []config.ClaudeKey{{APIKey: "claude-key", BaseURL: "https://api.anthropic.com"}},
}
auths, err := NewConfigSynthesizer().Synthesize(&SynthesisContext{
Config: cfg,
Now: time.Now(),
IDGenerator: NewStableIDGenerator(),
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var codexAuth, claudeAuth *coreauth.Auth
for _, a := range auths {
switch {
case a.Provider == "codex":
codexAuth = a
case a.Provider == "claude":
claudeAuth = a
}
}
if codexAuth == nil || claudeAuth == nil {
t.Fatalf("missing synthesised auths: codex=%v claude=%v", codexAuth, claudeAuth)
}
pool := codexAuth.ProxyConnectionPool
if pool == nil {
t.Fatal("codex override did not reach the auth record")
}
if pool.Enabled == nil || !*pool.Enabled || pool.IdleConnTimeout != "300s" || pool.MaxIdleConnsPerHost == nil || *pool.MaxIdleConnsPerHost != 4 {
t.Fatalf("unexpected override: %+v", pool)
}
if claudeAuth.ProxyConnectionPool != nil {
t.Fatal("entry without a connection-pool block must stay unpooled")
}
// The override must be copied, never aliased to live config values.
*pool.Enabled = false
if !enabled {
t.Fatal("auth override aliases the config pointer")
}
}
// TestConfigSynthesizer_OpenAICompatConnectionPoolMerge pins field-level inheritance
// for OpenAI-compatible keys: a per-key block only replaces the fields it sets.
func TestConfigSynthesizer_OpenAICompatConnectionPoolMerge(t *testing.T) {
groupEnabled := true
groupMax := 2
cfg := &config.Config{
OpenAICompatibility: []config.OpenAICompatibility{{
Name: "compat",
BaseURL: "https://compat.example.com",
ConnectionPool: &config.ProxyConnectionPoolConfig{
Enabled: &groupEnabled,
IdleConnTimeout: "300s",
MaxIdleConnsPerHost: &groupMax,
},
APIKeyEntries: []config.OpenAICompatibilityAPIKey{
{APIKey: "inherits"},
{APIKey: "partial", ConnectionPool: &config.ProxyConnectionPoolConfig{IdleConnTimeout: "60s"}},
},
}},
}
auths, err := NewConfigSynthesizer().Synthesize(&SynthesisContext{
Config: cfg,
Now: time.Now(),
IDGenerator: NewStableIDGenerator(),
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
byKey := map[string]*coreauth.ProxyConnectionPoolOverride{}
for _, a := range auths {
byKey[a.Attributes["api_key"]] = a.ProxyConnectionPool
}
inherits := byKey["inherits"]
if inherits == nil || inherits.Enabled == nil || !*inherits.Enabled || inherits.IdleConnTimeout != "300s" {
t.Fatalf("key without an override must inherit the group block: %+v", inherits)
}
if inherits.MaxIdleConnsPerHost == nil || *inherits.MaxIdleConnsPerHost != 2 {
t.Fatalf("inheriting key lost max-idle: %+v", inherits)
}
partial := byKey["partial"]
if partial == nil || partial.Enabled == nil || !*partial.Enabled {
t.Fatalf("partial override must keep the group's enabled value: %+v", partial)
}
if partial.IdleConnTimeout != "60s" {
t.Fatalf("partial override idle = %q, want 60s", partial.IdleConnTimeout)
}
if partial.MaxIdleConnsPerHost == nil || *partial.MaxIdleConnsPerHost != 2 {
t.Fatalf("partial override must inherit max-idle-conns-per-host: %+v", partial)
}
}

View File

@@ -83,6 +83,10 @@ type Auth struct {
Unavailable bool `json:"unavailable"`
// ProxyURL overrides the global proxy setting for this auth if provided.
ProxyURL string `json:"proxy_url,omitempty"`
// ProxyConnectionPool carries the connection-pool block of this api-key
// credential. Nil means no pooling, which is the default. Pools are per
// credential, so an override here never shares connections with another auth.
ProxyConnectionPool *ProxyConnectionPoolOverride `json:"proxy_connection_pool,omitempty"`
// Attributes stores provider specific metadata needed by executors (immutable configuration).
Attributes map[string]string `json:"attributes,omitempty"`
// Metadata stores runtime mutable provider state (e.g. tokens, cookies).
@@ -490,6 +494,9 @@ func (a *Auth) Clone() *Auth {
copyAuth.Attributes[key] = value
}
}
if a.ProxyConnectionPool != nil {
copyAuth.ProxyConnectionPool = a.ProxyConnectionPool.Clone()
}
if a.Metadata != nil {
copyAuth.Metadata = cloneMetadataMap(a.Metadata)
}
@@ -503,6 +510,31 @@ func (a *Auth) Clone() *Auth {
return &copyAuth
}
// ProxyConnectionPoolOverride holds the per-api-key connection-pool settings.
// Omitted fields fall back to the pool defaults (300s idle, 2 idle per host).
type ProxyConnectionPoolOverride struct {
Enabled *bool `json:"enabled,omitempty"`
IdleConnTimeout string `json:"idle-conn-timeout,omitempty"`
MaxIdleConnsPerHost *int `json:"max-idle-conns-per-host,omitempty"`
}
// Clone deep copies the override so auth snapshots never share mutable state.
func (o *ProxyConnectionPoolOverride) Clone() *ProxyConnectionPoolOverride {
if o == nil {
return nil
}
out := &ProxyConnectionPoolOverride{IdleConnTimeout: o.IdleConnTimeout}
if o.Enabled != nil {
enabled := *o.Enabled
out.Enabled = &enabled
}
if o.MaxIdleConnsPerHost != nil {
maxIdle := *o.MaxIdleConnsPerHost
out.MaxIdleConnsPerHost = &maxIdle
}
return out
}
func cloneMetadataMap(src map[string]any) map[string]any {
if src == nil {
return nil