refactor(http): share SSRF-safe transport across model clients

Extract NewSSRFSafeTransport and NewSSRFSafeHTTPClientWithTransport so
embedding and rerank can reuse one connection pool without allocating a
throwaway transport per client. SSRF dial and redirect policies are
unchanged; each client still keeps its own timeout.
This commit is contained in:
wizardchen
2026-07-23 11:59:12 +08:00
committed by lyingbug
parent ccf02e1a2a
commit 146e341748
4 changed files with 134 additions and 42 deletions
+8 -8
View File
@@ -10,11 +10,11 @@ import (
// sharedEmbeddingHTTPTransport keeps a single SSRF-safe connection pool for
// all embedding clients. Embedders are recreated as model configuration changes,
// but their outbound connections can be safely reused across client instances.
var sharedEmbeddingHTTPTransport = func() http.RoundTripper {
cfg := secutils.DefaultSSRFSafeHTTPClientConfig()
return secutils.NewSSRFSafeHTTPClient(cfg).Transport
}()
// but their outbound connections can be safely reused across client instances,
// so the transport (and its keep-alive pool) is built once at package load.
var sharedEmbeddingHTTPTransport = secutils.NewSSRFSafeTransport(
secutils.DefaultSSRFSafeHTTPClientConfig(),
)
// validateEmbeddingBaseURL checks that a resolved embedding API base URL is safe
// for outbound requests. Empty URLs are allowed (callers apply provider defaults).
@@ -30,10 +30,10 @@ func validateEmbeddingBaseURL(baseURL string) error {
// newEmbeddingHTTPClient returns an HTTP client with connection-level SSRF
// protection and redirect validation, aligned with internal/models/chat/transport.go.
// All clients share sharedEmbeddingHTTPTransport so keep-alive connections are
// pooled globally, while each keeps its own timeout.
func newEmbeddingHTTPClient(timeout time.Duration) *http.Client {
cfg := secutils.DefaultSSRFSafeHTTPClientConfig()
cfg.Timeout = timeout
client := secutils.NewSSRFSafeHTTPClient(cfg)
client.Transport = sharedEmbeddingHTTPTransport
return client
return secutils.NewSSRFSafeHTTPClientWithTransport(cfg, sharedEmbeddingHTTPTransport)
}