Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 52 additions & 0 deletions pkg/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,61 @@ 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.
// 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
}

// 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
}
host := extractHost(p.BaseURL)
if host == "" || seen[host] {
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)
} 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))
Expand Down
38 changes: 38 additions & 0 deletions pkg/proxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading