From 99f4207ecac06001bf9923d83432ceb25aeaa4b6 Mon Sep 17 00:00:00 2001 From: Bene Date: Sun, 22 Nov 2026 12:22:52 +0100 Subject: [PATCH 1/4] proxy: add DNS pre-resolution for provider endpoints On config load/reload, asynchronously pre-resolve DNS for all enabled provider BaseURLs so the first request doesn't pay a cold DNS lookup. Failures are logged but don't block the proxy. --- pkg/proxy/server.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkg/proxy/server.go b/pkg/proxy/server.go index b7ef842..bc8f074 100644 --- a/pkg/proxy/server.go +++ b/pkg/proxy/server.go @@ -117,9 +117,50 @@ func (s *Server) UpdateConfig(cfg *config.Config) error { return err } s.conf.Store(&configState{cfg: cfg, filters: filters}) + + // Pre-resolve DNS for all enabled providers so the first request + // doesn't pay a cold DNS lookup. Runs async to avoid blocking startup/reload. + go preResolveProviders(cfg) return nil } +// preResolveProviders fires off DNS lookups for all enabled provider BaseURLs. +// Best-effort: failures are logged but don't block the proxy. +func preResolveProviders(cfg *config.Config) { + seen := make(map[string]bool) + for _, p := range cfg.Providers { + if !p.Enabled || p.BaseURL == "" { + continue + } + host := extractHost(p.BaseURL) + if host == "" || seen[host] { + continue + } + seen[host] = true + go func(h string) { + if _, err := net.LookupHost(h); err != nil { + log.Printf("dns pre-resolve: %s: %v", h, err) + } else { + log.Printf("dns pre-resolve: %s ok", h) + } + }(host) + } +} + +// extractHost returns the host:port from a URL like "https://api.openai.com/v1". +func extractHost(baseURL string) string { + // Strip scheme + u := baseURL + if i := strings.Index(u, "://"); i >= 0 { + u = u[i+3:] + } + // Strip path + if i := strings.IndexByte(u, '/'); i >= 0 { + u = u[:i] + } + return u +} + // UpdateCatalog hot-reloads the model catalog. func (s *Server) UpdateCatalog(cat *catalog.Catalog) { s.catalog.Store(newCatalogSnapshot(cat)) From 492c450d7523dcb582b5fc7cd4cb1b2f0684960b Mon Sep 17 00:00:00 2001 From: Bene Date: Mon, 30 Nov 2026 11:55:43 +0100 Subject: [PATCH 2/4] Fix data race in preResolveProviders The goroutine spawned by UpdateConfig was reading cfg.Providers while the caller could mutate it after UpdateConfig returned (as done in TestDirectModel_UsesBuildBody). Snapshot the DNS hosts before spawning the goroutine so the lookup runs on a detached copy. --- pkg/proxy/server.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/proxy/server.go b/pkg/proxy/server.go index bc8f074..538ab2e 100644 --- a/pkg/proxy/server.go +++ b/pkg/proxy/server.go @@ -120,14 +120,16 @@ func (s *Server) UpdateConfig(cfg *config.Config) error { // Pre-resolve DNS for all enabled providers so the first request // doesn't pay a cold DNS lookup. Runs async to avoid blocking startup/reload. - go preResolveProviders(cfg) + // Snapshot the hosts before spawning the goroutine so the caller can + // safely mutate cfg after UpdateConfig returns without racing the lookup. + go preResolveProviders(dnsHostsToResolve(cfg)) return nil } -// preResolveProviders fires off DNS lookups for all enabled provider BaseURLs. -// Best-effort: failures are logged but don't block the proxy. -func preResolveProviders(cfg *config.Config) { +// dnsHostsToResolve returns the unique hosts of enabled providers with a BaseURL. +func dnsHostsToResolve(cfg *config.Config) []string { seen := make(map[string]bool) + var hosts []string for _, p := range cfg.Providers { if !p.Enabled || p.BaseURL == "" { continue @@ -137,6 +139,15 @@ func preResolveProviders(cfg *config.Config) { continue } seen[host] = true + hosts = append(hosts, host) + } + return hosts +} + +// preResolveProviders fires off DNS lookups for the given hosts. +// Best-effort: failures are logged but don't block the proxy. +func preResolveProviders(hosts []string) { + for _, host := range hosts { go func(h string) { if _, err := net.LookupHost(h); err != nil { log.Printf("dns pre-resolve: %s: %v", h, err) From d371b08ab39375cb2448c7dab6db99000260a163 Mon Sep 17 00:00:00 2001 From: Bene Date: Mon, 30 Nov 2026 12:07:28 +0100 Subject: [PATCH 3/4] test: add test for DNS pre-resolution with bad hostname Verifies that UpdateConfig with an unresolvable provider hostname: - Returns without error (does not block on DNS) - Logs the failure asynchronously - Leaves the server functional for subsequent requests --- pkg/proxy/server_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pkg/proxy/server_test.go b/pkg/proxy/server_test.go index 1013ca6..41f23b3 100644 --- a/pkg/proxy/server_test.go +++ b/pkg/proxy/server_test.go @@ -239,6 +239,44 @@ func TestChatCompletions_ProfileStreamOverrideChangesRouting(t *testing.T) { } } +// DNS pre-resolution: a bad hostname on reload is logged and never blocks. +func TestUpdateConfig_BadHostnameDoesNotBlock(t *testing.T) { + s := newTestServer("http://127.0.0.1:1", 0) + + // A config with a host that cannot resolve — UpdateConfig must not return + // an error and must not block waiting for DNS. + badCfg := &config.Config{ + Providers: []config.ProviderConfig{ + {ID: "bad", BaseURL: "http://this-host-does-not-exist.example.invalid/v1", Enabled: true}, + {ID: "gemini", BaseURL: "http://127.0.0.1:1/v1", Enabled: true}, + }, + Fallback: config.FallbackConfig{MaxAttempts: 3}, + } + badCfg.Proxy.Strategy = "adaptive" + badCfg.Proxy.CacheTTL = 0 + + done := make(chan error, 1) + go func() { done <- s.UpdateConfig(badCfg) }() + + select { + case err := <-done: + if err != nil { + t.Fatalf("UpdateConfig with bad hostname returned error: %v", err) + } + case <-time.After(5 * time.Second): + t.Fatal("UpdateConfig blocked on DNS lookup for bad hostname") + } + + // The server should still be usable after the reload. + rec := httptest.NewRecorder() + s.handleChatCompletions(rec, httptest.NewRequest(http.MethodPost, "/v1/chat/completions", + strings.NewReader(`{"model":"auto","messages":[{"role":"user","content":"hi"}]}`))) + // Expect 503 (no reachable provider) — not a hang or a startup failure. + if rec.Code != http.StatusServiceUnavailable { + t.Fatalf("after bad-hostname reload: status = %d, want 503; body %s", rec.Code, rec.Body) + } +} + // readFields re-reads everything a profile can change, and resets what the // profile removed. func TestRequest_ReadFieldsAfterProfileChange(t *testing.T) { From 5bf5a720e51fcbab921530989675e67375148342 Mon Sep 17 00:00:00 2001 From: Bene Date: Mon, 30 Nov 2026 12:11:13 +0100 Subject: [PATCH 4/4] docs: note DNS pre-resolution is best-effort without caching resolver --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 88b271f..a8ab83f 100644 --- a/README.md +++ b/README.md @@ -353,6 +353,7 @@ configs/ bundled config and the free-model catalog - **A stream that breaks after its first token cannot fall back.** The client already has part of the answer, so the stream ends with an SSE error event (`stream_interrupted`), never with a clean end that would look like a complete answer. Gemini is still buffered (its thought blocks are stripped from the full answer) and re-emitted as SSE. - **The catalog can lag a provider's change by up to a week.** Models that vanish are cooled down on the first 404 and skipped. - **Local quota tracking is best-effort.** The proxy honours the providers' rate-limit headers, but under heavy concurrency a provider may still send the occasional 429, which simply triggers the fallback. +- **DNS pre-resolution at startup is best-effort.** The proxy resolves provider hostnames on config load/reload so the first request doesn't pay a cold lookup. However, **Go's resolver does not cache by default** — the warm-up only helps if your OS runs a caching resolver (systemd-resolved, mDNSResponder, nscd, etc.). On a bare Linux box or a minimal container without one, the first request still does a DNS lookup. ## Development