fix(laya): extend shared CPU ranking timeout
This commit is contained in:
@ -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 {
|
||||
|
||||
61
backend/internal/ai/laya_timeout_ticket31_test.go
Normal file
61
backend/internal/ai/laya_timeout_ticket31_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user