Changeset 0.28.8 (#194)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
@@ -51,12 +52,30 @@ type ProviderConfig struct {
|
||||
ProxyURL string // Only used when ProxyMode == "custom"
|
||||
}
|
||||
|
||||
// ── HTTP Client Pool ───────────────────────
|
||||
//
|
||||
// Provider HTTP clients are pooled by (ProxyMode, ProxyURL) tuple.
|
||||
// Previously Client() created a new http.Transport per call, leaking
|
||||
// idle connections and preventing TCP reuse across requests to the same
|
||||
// provider. With pooling, a Venice model sync followed by a completion
|
||||
// reuses the same TCP connection to api.venice.ai.
|
||||
|
||||
// clientPool holds shared *http.Client instances keyed by proxy config.
|
||||
var clientPool sync.Map // map[string]*http.Client
|
||||
|
||||
// Client returns an *http.Client configured for this provider's proxy settings.
|
||||
// Clients are cached and reused across calls with the same proxy configuration.
|
||||
// system = http.ProxyFromEnvironment (Go default), direct = no proxy,
|
||||
// custom = explicit proxy URL (http/https/socks5).
|
||||
func (c ProviderConfig) Client() *http.Client {
|
||||
key := c.ProxyMode + "|" + c.ProxyURL
|
||||
if v, ok := clientPool.Load(key); ok {
|
||||
return v.(*http.Client)
|
||||
}
|
||||
|
||||
transport := &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
MaxIdleConnsPerHost: 10,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
}
|
||||
@@ -81,7 +100,10 @@ func (c ProviderConfig) Client() *http.Client {
|
||||
default: // "system" or empty
|
||||
transport.Proxy = http.ProxyFromEnvironment
|
||||
}
|
||||
return &http.Client{Transport: transport}
|
||||
|
||||
client := &http.Client{Transport: transport}
|
||||
actual, _ := clientPool.LoadOrStore(key, client)
|
||||
return actual.(*http.Client)
|
||||
}
|
||||
|
||||
// ── Request / Response Types ────────────────
|
||||
|
||||
Reference in New Issue
Block a user