mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(registry): keep known models on an empty discovery answer
A successful but empty discovery answer replaced the known model set with nothing, so a flapping upstream made every discovered model return 404 until the next refresh. The registry now treats that answer as a failure. It keeps the previous set and counts the failure toward the discovery breaker.
This commit is contained in:
+10
-1
@@ -113,6 +113,10 @@ const maxBreakerOpenDuration = 24 * time.Hour
|
||||
// successful discovery fetch) — 404 semantics for the caller.
|
||||
var errModelUnknown = errors.New("llmgateway: unknown model")
|
||||
|
||||
// errEmptyDiscovery marks a successful but empty discovery answer over a
|
||||
// known model set: it is treated as a failure so the set is kept.
|
||||
var errEmptyDiscovery = errors.New("llmgateway: discovery returned no models, keeping the previous set")
|
||||
|
||||
// errModelDenied is returned by modelRegistry.resolve when id is a known
|
||||
// model but the requesting group is not authorized for it — 403 semantics
|
||||
// for the caller. Distinct from errModelUnknown so the caller can tell "no
|
||||
@@ -381,7 +385,9 @@ func (st *providerState) tryBeginWarmFillRetry(sinceWarmFill time.Time) bool {
|
||||
// what throttles a failing provider to one attempt per interval instead of
|
||||
// retrying on every call. On success (err == nil) ids replaces the
|
||||
// discovered set; on failure the previous discovered set is kept
|
||||
// (stale-while-error). Either way, recordHealthLocked (feat/provider-health)
|
||||
// (stale-while-error). An empty success over a non-empty set counts as a
|
||||
// failure (errEmptyDiscovery), so a flapping upstream cannot erase every
|
||||
// known model. Either way, recordHealthLocked (feat/provider-health)
|
||||
// updates the discovery circuit breaker from this same outcome — a healthy
|
||||
// provider's breaker never leaves closed, so this adds no new behavior for
|
||||
// the common case, only for a provider that is actually failing.
|
||||
@@ -390,6 +396,9 @@ func (st *providerState) finishRefresh(now time.Time, ids []string, err error) {
|
||||
defer st.mu.Unlock()
|
||||
st.inFlight = false
|
||||
st.lastRefresh = now
|
||||
if err == nil && len(ids) == 0 && len(st.discovered) > 0 {
|
||||
err = errEmptyDiscovery
|
||||
}
|
||||
if err != nil {
|
||||
st.lastErr = err.Error()
|
||||
} else {
|
||||
|
||||
@@ -2645,3 +2645,48 @@ func TestModelRegistry_ListFor_Alias_ShadowedCollisionKeepsAliasEntry(t *testing
|
||||
t.Errorf("listFor = %v, want the shadowed discovered model listed as %q", got, "acme/shared-id")
|
||||
}
|
||||
}
|
||||
|
||||
// TestProviderState_FinishRefresh_EmptyAnswerKeepsKnownModels pins that a
|
||||
// successful but empty discovery answer never erases a known model set.
|
||||
func TestProviderState_FinishRefresh_EmptyAnswerKeepsKnownModels(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
first []string
|
||||
second []string
|
||||
wantModels []string
|
||||
wantErrText bool
|
||||
}{
|
||||
{name: "empty answer over known set keeps it", first: []string{"a", "b"}, second: []string{}, wantModels: []string{"a", "b"}, wantErrText: true},
|
||||
{name: "nil answer over known set keeps it", first: []string{"a"}, second: nil, wantModels: []string{"a"}, wantErrText: true},
|
||||
{name: "first fill may be empty", first: nil, second: []string{}, wantModels: nil, wantErrText: false},
|
||||
{name: "non-empty answer replaces the set", first: []string{"a", "b"}, second: []string{"c"}, wantModels: []string{"c"}, wantErrText: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
reg := newAliasTestRegistry(t, nil, nil)
|
||||
st := reg.states["openai"]
|
||||
st.finishRefresh(reg.now(), tt.first, nil)
|
||||
st.finishRefresh(reg.now(), tt.second, nil)
|
||||
st.mu.Lock()
|
||||
got := make(map[string]bool, len(st.discovered))
|
||||
for id := range st.discovered {
|
||||
got[id] = true
|
||||
}
|
||||
lastErr := st.lastErr
|
||||
st.mu.Unlock()
|
||||
if len(got) != len(tt.wantModels) {
|
||||
t.Fatalf("discovered = %v, want %v", got, tt.wantModels)
|
||||
}
|
||||
for _, id := range tt.wantModels {
|
||||
if !got[id] {
|
||||
t.Errorf("discovered = %v, missing %q", got, id)
|
||||
}
|
||||
}
|
||||
if (lastErr != "") != tt.wantErrText {
|
||||
t.Errorf("lastErr = %q, want non-empty = %v", lastErr, tt.wantErrText)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user