mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
fix(discovery): isolate each modelBaseUrls upstream's timeout (#24)
Discovery and metadata polled the upstreams one after another on one shared context. A hanging modelBaseUrls upstream used up the deadline, and the whole provider failed again. Query all upstreams concurrently. Give each modelBaseUrls upstream its own context with 3/4 of the remaining time, or 30s without a deadline. A child timeout is recorded in upstreamErrors and skipped. Only a baseUrl failure, or a cancelled or expired parent context, fails the listing. The result order stays baseUrl first, then the sorted upstreams.
This commit is contained in:
@@ -2402,7 +2402,11 @@ providers:
|
||||
- **Discovery and `metadataPath`** query `baseUrl` and every distinct
|
||||
`modelBaseUrls` upstream, one request per upstream. An id is taken only from
|
||||
the upstream its requests route to. Discovery fails as a whole only when `baseUrl`
|
||||
fails, keeping the last-known set. A failing `modelBaseUrls` upstream does not
|
||||
fails or the whole call is cancelled or times out, keeping the last-known set.
|
||||
Upstreams are queried concurrently, and each `modelBaseUrls` upstream has its
|
||||
own timeout (3/4 of the time left, or 30s without a deadline), so a hanging
|
||||
upstream cannot use up the budget of `baseUrl` or the others. A failing or
|
||||
timed-out `modelBaseUrls` upstream does not
|
||||
degrade the provider: its explicit `models` stay served, the other upstreams'
|
||||
ids still merge, and the admin overview lists it under `upstreamErrors` (also
|
||||
logged, at most once per 10 minutes). Metadata keeps the ids of the upstreams
|
||||
|
||||
+57
-9
@@ -478,12 +478,18 @@ func (a *openaiAdapter) setUpstreamErrors(errs []upstreamFailure) {
|
||||
// upstreamErrors) and skipped: its configured models stay served, and the
|
||||
// other upstreams' ids still merge.
|
||||
func (a *openaiAdapter) listModels(ctx context.Context) ([]string, error) {
|
||||
bases := a.upstreamBases()
|
||||
listed := make([][]string, len(bases))
|
||||
errs := a.queryUpstreams(ctx, bases, func(uctx context.Context, i int) error {
|
||||
var err error
|
||||
listed[i], err = a.listModelsFrom(uctx, bases[i])
|
||||
return err
|
||||
})
|
||||
var ids []string
|
||||
var failures []upstreamFailure
|
||||
seen := make(map[string]bool)
|
||||
for i, base := range a.upstreamBases() {
|
||||
listed, err := a.listModelsFrom(ctx, base)
|
||||
if err != nil {
|
||||
for i, base := range bases {
|
||||
if err := errs[i]; err != nil {
|
||||
if i == 0 || ctx.Err() != nil {
|
||||
a.setUpstreamErrors(nil)
|
||||
return nil, err
|
||||
@@ -491,7 +497,7 @@ func (a *openaiAdapter) listModels(ctx context.Context) ([]string, error) {
|
||||
failures = append(failures, upstreamFailure{Upstream: base, Err: err.Error()})
|
||||
continue
|
||||
}
|
||||
for _, id := range listed {
|
||||
for _, id := range listed[i] {
|
||||
if a.baseForModel(id) != base || seen[id] {
|
||||
continue
|
||||
}
|
||||
@@ -503,6 +509,42 @@ func (a *openaiAdapter) listModels(ctx context.Context) ([]string, error) {
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// upstreamFallbackTimeout caps a modelBaseUrls request when the parent
|
||||
// context has no deadline.
|
||||
const upstreamFallbackTimeout = 30 * time.Second
|
||||
|
||||
// queryUpstreams runs fn for every base concurrently and returns one error
|
||||
// per base. baseURL (index 0) runs on ctx; each other upstream gets its own
|
||||
// child context, so a hanging upstream cannot use up the budget of the rest.
|
||||
func (a *openaiAdapter) queryUpstreams(ctx context.Context, bases []string, fn func(ctx context.Context, i int) error) []error {
|
||||
errs := make([]error, len(bases))
|
||||
var wg sync.WaitGroup
|
||||
for i := range bases {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
uctx := ctx
|
||||
if i > 0 {
|
||||
var cancel context.CancelFunc
|
||||
uctx, cancel = context.WithTimeout(ctx, childUpstreamTimeout(ctx))
|
||||
defer cancel()
|
||||
}
|
||||
errs[i] = fn(uctx, i)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
return errs
|
||||
}
|
||||
|
||||
// childUpstreamTimeout is 3/4 of the time left on ctx, leaving headroom for
|
||||
// the caller to finish inside the parent deadline.
|
||||
func childUpstreamTimeout(ctx context.Context) time.Duration {
|
||||
if dl, ok := ctx.Deadline(); ok {
|
||||
return time.Until(dl) * 3 / 4
|
||||
}
|
||||
return upstreamFallbackTimeout
|
||||
}
|
||||
|
||||
// upstreamBases returns baseURL followed by the other distinct modelBaseUrls
|
||||
// values in sorted order.
|
||||
func (a *openaiAdapter) upstreamBases() []string {
|
||||
@@ -594,19 +636,25 @@ func (a *openaiAdapter) fetchModelMetadata(ctx context.Context) (map[string]int,
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
bases := a.upstreamBases()
|
||||
got := make([]map[string]int, len(bases))
|
||||
errs := a.queryUpstreams(ctx, bases, func(uctx context.Context, i int) error {
|
||||
var err error
|
||||
got[i], err = a.fetchModelMetadataFrom(uctx, bases[i])
|
||||
return err
|
||||
})
|
||||
out := make(map[string]int)
|
||||
var firstErr error
|
||||
ok := false
|
||||
for _, base := range a.upstreamBases() {
|
||||
got, err := a.fetchModelMetadataFrom(ctx, base)
|
||||
if err != nil {
|
||||
for i, base := range bases {
|
||||
if errs[i] != nil {
|
||||
if firstErr == nil {
|
||||
firstErr = err
|
||||
firstErr = errs[i]
|
||||
}
|
||||
continue
|
||||
}
|
||||
ok = true
|
||||
for id, n := range got {
|
||||
for id, n := range got[i] {
|
||||
if a.baseForModel(id) == base {
|
||||
out[id] = n
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -168,3 +169,86 @@ func TestGateway_BaseURLFailure_ProviderDegradedAsBefore(t *testing.T) {
|
||||
assert.Contains(t, got.Providers[0].LastErr, "404")
|
||||
assert.Empty(t, got.Providers[0].UpstreamErrors)
|
||||
}
|
||||
|
||||
func hangingServer(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
release := make(chan struct{})
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
|
||||
select {
|
||||
case <-release:
|
||||
case <-r.Context().Done():
|
||||
}
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
t.Cleanup(func() { close(release) })
|
||||
return srv
|
||||
}
|
||||
|
||||
func hangingUpstreamAdapter(t *testing.T) (*openaiAdapter, string) {
|
||||
t.Helper()
|
||||
main := modelsServer(t, `{"data":[{"id":"main-model","max_model_len":1000}]}`, nil)
|
||||
other := modelsServer(t, `{"data":[{"id":"glm","max_model_len":2000}]}`, nil)
|
||||
hang := hangingServer(t)
|
||||
ad := newOpenAIAdapter("gx10", main.URL, "")
|
||||
ad.modelBases = map[string]string{"glm": other.URL, "rerank": hang.URL}
|
||||
ad.metadataPath = "/v1/models"
|
||||
return ad, hang.URL
|
||||
}
|
||||
|
||||
func TestOpenAIAdapter_HangingModelBaseURL_DoesNotUseUpSharedDeadline(t *testing.T) {
|
||||
t.Parallel()
|
||||
ad, hangURL := hangingUpstreamAdapter(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
|
||||
defer cancel()
|
||||
start := time.Now()
|
||||
ids, err := ad.listModels(ctx)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
require.NoError(t, err, "a hanging modelBaseUrls upstream must not fail discovery")
|
||||
assert.Equal(t, []string{"main-model", "glm"}, ids)
|
||||
assert.Less(t, elapsed, 3500*time.Millisecond, "child timeout leaves headroom inside the parent deadline")
|
||||
require.Len(t, ad.upstreamErrors(), 1)
|
||||
failure := ad.upstreamErrors()[0]
|
||||
assert.Equal(t, hangURL, failure.Upstream)
|
||||
assert.Contains(t, failure.Err, "context deadline exceeded")
|
||||
}
|
||||
|
||||
func TestOpenAIAdapter_HangingModelBaseURL_MetadataKeepsOthers(t *testing.T) {
|
||||
t.Parallel()
|
||||
ad, _ := hangingUpstreamAdapter(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
|
||||
defer cancel()
|
||||
start := time.Now()
|
||||
got, err := ad.fetchModelMetadata(ctx)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, map[string]int{"main-model": 1000, "glm": 2000}, got)
|
||||
assert.Less(t, elapsed, 3500*time.Millisecond)
|
||||
}
|
||||
|
||||
func TestOpenAIAdapter_HangingBaseURL_FailsDiscovery(t *testing.T) {
|
||||
t.Parallel()
|
||||
good := modelsServer(t, `{"data":[{"id":"glm"}]}`, nil)
|
||||
ad := newOpenAIAdapter("gx10", hangingServer(t).URL, "")
|
||||
ad.modelBases = map[string]string{"glm": good.URL}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||
defer cancel()
|
||||
_, err := ad.listModels(ctx)
|
||||
require.Error(t, err)
|
||||
assert.Empty(t, ad.upstreamErrors())
|
||||
}
|
||||
|
||||
func TestOpenAIAdapter_ParentCancel_FailsDiscovery(t *testing.T) {
|
||||
t.Parallel()
|
||||
ad, _ := hangingUpstreamAdapter(t)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
time.AfterFunc(200*time.Millisecond, cancel)
|
||||
_, err := ad.listModels(ctx)
|
||||
require.Error(t, err)
|
||||
assert.Empty(t, ad.upstreamErrors())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user