fix(laya): extend shared CPU ranking timeout

This commit is contained in:
2026-11-23 20:39:37 +01:00
parent cb06d3432a
commit bf7f94bc80
8 changed files with 100 additions and 9 deletions

View File

@ -42,9 +42,9 @@ func NewLayaHTTPRanker(cfg LayaHTTPConfig) (*LayaHTTPRanker, error) {
return nil, errors.New("laya: invalid base URL")
}
if cfg.Timeout == 0 {
cfg.Timeout = 120 * time.Second
cfg.Timeout = 5 * time.Minute
}
if cfg.Timeout < 0 {
if cfg.Timeout < 0 || cfg.Timeout > 10*time.Minute {
return nil, errors.New("laya: invalid timeout")
}
if cfg.BatchSize == 0 {

View File

@ -0,0 +1,61 @@
package ai
import (
"context"
"github.com/tradarr/backend/internal/generation"
"io"
"net/http"
"strings"
"testing"
"time"
)
type ticket31Transport func(*http.Request) (*http.Response, error)
func (f ticket31Transport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func TestTicket31LayaTimeoutDefaultsAndOverrides(t *testing.T) {
for _, duration := range []time.Duration{0, 45 * time.Second, 10 * time.Minute} {
ranker, err := NewLayaHTTPRanker(LayaHTTPConfig{BaseURL: "http://fake.invalid", Timeout: duration})
if err != nil {
t.Fatal(err)
}
expected := duration
if duration == 0 {
expected = 5 * time.Minute
}
if ranker.timeout != expected {
t.Fatalf("timeout=%v want=%v", ranker.timeout, expected)
}
}
if _, err := NewLayaHTTPRanker(LayaHTTPConfig{BaseURL: "http://fake.invalid", Timeout: 10*time.Minute + time.Nanosecond}); err == nil {
t.Fatal("accepted >10m")
}
}
func TestTicket31SharedDeadlineCancelsWholeMultiBatchRank(t *testing.T) {
ranker, _ := NewLayaHTTPRanker(LayaHTTPConfig{BaseURL: "http://fake.invalid", Timeout: 20 * time.Millisecond, BatchSize: 1})
calls := 0
var shared time.Time
ranker.client = &http.Client{Transport: ticket31Transport(func(r *http.Request) (*http.Response, error) {
calls++
deadline, ok := r.Context().Deadline()
if !ok {
t.Fatal("missing deadline")
}
if calls == 1 {
shared = deadline
} else if !deadline.Equal(shared) {
t.Fatal("deadline reset between batches")
}
if calls == 2 {
<-r.Context().Done()
return nil, r.Context().Err()
}
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"results":[{"answers":{"relevance":{"type":"score","score":0.1,"answer_confidence":0.99}},"usage":{"truncated":false}}]}`)), Header: make(http.Header)}, nil
})}
got, err := ranker.Rank(context.Background(), "title", nil, []generation.Candidate{{ID: "1"}, {ID: "2"}, {ID: "3"}})
if calls != 2 || err == nil || err.Error() != "laya: timeout" || got != nil {
t.Fatalf("calls=%d err=%v partial=%v", calls, err, got != nil)
}
}

View File

@ -64,7 +64,7 @@ func Load() (*Config, error) {
if layaURL == "" {
layaURL = "http://laya:8000"
}
layaTimeout := 120 * time.Second
layaTimeout := 5 * time.Minute
if value := os.Getenv("LAYA_TIMEOUT"); value != "" {
parsed, err := time.ParseDuration(value)
if err != nil || parsed <= 0 || parsed > 10*time.Minute {

View File

@ -17,7 +17,7 @@ func TestLayaEnvironment(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if cfg.LayaURL != "http://laya:8000" || cfg.LayaTimeout != 120*time.Second {
if cfg.LayaURL != "http://laya:8000" || cfg.LayaTimeout != 5*time.Minute {
t.Fatal("incorrect Laya defaults")
}
t.Setenv("LAYA_URL", "http://127.0.0.1:8001")
@ -27,7 +27,12 @@ func TestLayaEnvironment(t *testing.T) {
if err != nil || cfg.LayaURL != "http://127.0.0.1:8001" || cfg.LayaTimeout != 45*time.Second || cfg.LayaAPIKey != "test-secret" {
t.Fatalf("environment not loaded: %v", err)
}
for _, value := range []string{"invalid", "0s", "-1s", "11m"} {
t.Setenv("LAYA_TIMEOUT", "10m")
cfg, err = Load()
if err != nil || cfg.LayaTimeout != 10*time.Minute {
t.Fatalf("maximum override rejected: %v", err)
}
for _, value := range []string{"invalid", "0s", "-1s", "11m", "10m1ns"} {
t.Setenv("LAYA_TIMEOUT", value)
if _, err := Load(); err == nil {
t.Errorf("accepted timeout %s", value)

View File

@ -0,0 +1,25 @@
package server
import (
"gopkg.in/yaml.v3"
"os"
"testing"
)
func TestTicket31ComposeLayaTimeoutDefaultAndOverride(t *testing.T) {
for _, path := range []string{"../../../docker-compose.yml", "../../../docker-compose.prod.yml"} {
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var document struct {
Services map[string]struct{ Environment map[string]string }
}
if err = yaml.Unmarshal(data, &document); err != nil {
t.Fatal(err)
}
if got := document.Services["backend"].Environment["LAYA_TIMEOUT"]; got != "${LAYA_TIMEOUT:-5m}" {
t.Errorf("%s timeout=%q", path, got)
}
}
}