The zstd subtest went red on macOS CI at 4a9cfb52e: "held 1s before
routing, its body still arriving (256.1 KB of 256.1 KB, the last 100ms
ago); in body 500ms; stopped with 408". Only time spent in a wire read
(waitedBody.Read) earns upload credit; the handler's own time between
reads (decoding, being scheduled again) counts toward unroutedMost. On
mote that time is under 2ms, but a loaded runner spent about 1s of it,
past the test's 1s unroutedMost, so the hold stopped the upload with 408.
The test now holds most at 3s and waits most+most/2 for the hold to end
by itself, well under the 6s (gzip) to 8s the wire bytes earn. The
production rule is unchanged: a stalled handler is still not an upload.
Run on mote (macOS) at this tree:
- Injected one 1.2s stall after the first wire read, outside the body
wait: old test red on all 4 subtests ("in body 0s", 408); new test
green on all 4.
- Injected 3.5s: new test red on all 4, so the bound is where the
comment says.
- Credit broken by hand (earned := 0): new test red on all 4.
- No injection: green, ~4.5s a subtest.
- go test -race -count=2 -run '^TestUnrouted' ./internal/gateway: ok
(65.8s). gofmt -l and go vet ./internal/gateway: clean.
Not tried: Linux or Windows runs (the test has no OS branch); the full
suite (run once after this batch).
191 lines
6.7 KiB
Go
191 lines
6.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"io"
|
|
"math/rand/v2"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// #4775: the client's upload can outlast the routing hold while wire
|
|
// bytes are still arriving. Exercise the listener's guards and TLS, with
|
|
// only a local fake provider. A large first write buys ample rate room;
|
|
// waiting on the watch's measured body time makes the old cutoff fail.
|
|
//
|
|
// Only time spent in a wire read earns upload time: the handler's own
|
|
// time between reads (decoding, being scheduled again) counts toward
|
|
// unroutedMost. That is well under 2ms here, but a loaded macOS runner
|
|
// spent about 1s of it reading the 256 KB, past a 1s unroutedMost
|
|
// (#5274). So most is 3s, and the wait is half of it more, well under
|
|
// the 6s (gzip) to 8s the wire bytes earn.
|
|
func TestUnroutedProgressingUploadOutlastsMost(t *testing.T) {
|
|
const most = 3 * time.Second
|
|
for _, encoding := range []string{"identity", "chunked", "gzip", "zstd"} {
|
|
t.Run(encoding, func(t *testing.T) {
|
|
unroutedLimits(t, 20*time.Millisecond, most)
|
|
s, up := slowEyed(t, 0)
|
|
srv := httptest.NewTLSServer(s.Front())
|
|
defer srv.Close()
|
|
pr, pw := io.Pipe()
|
|
defer pr.Close()
|
|
defer pw.Close()
|
|
// Incompressible text keeps the compressed wire credit well
|
|
// above the seconds spent reading, even under -race.
|
|
rng := rand.New(rand.NewPCG(1, 2))
|
|
noise := make([]byte, 192<<10)
|
|
for i := range noise {
|
|
noise[i] = byte(rng.Uint32())
|
|
}
|
|
body := []byte(`{"model":"probe/text","messages":[{"role":"user","content":"` + base64.StdEncoding.EncodeToString(noise) + `"}]}`)
|
|
wire := body
|
|
if encoding != "chunked" {
|
|
wire = encodedBody(t, body, encoding)
|
|
}
|
|
req, err := http.NewRequest("POST", srv.URL+"/v1/chat/completions", pr)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if encoding != "chunked" {
|
|
req.ContentLength = int64(len(wire))
|
|
req.Header.Set("Content-Encoding", encoding)
|
|
}
|
|
got := make(chan unroutedReply, 1)
|
|
finished := make(chan struct{})
|
|
defer func() {
|
|
pw.Close()
|
|
pr.Close()
|
|
<-finished
|
|
}()
|
|
go func() {
|
|
defer close(finished)
|
|
res, err := srv.Client().Do(req)
|
|
if err != nil {
|
|
got <- unroutedReply{err: err}
|
|
return
|
|
}
|
|
b, err := io.ReadAll(res.Body)
|
|
res.Body.Close()
|
|
got <- unroutedReply{res.StatusCode, string(b), err}
|
|
}()
|
|
if _, err := io.CopyN(pw, bytes.NewReader(wire), int64(len(wire)-1)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r := heldPast(t, watchedHold(t, s), most+most/2, got); r != nil {
|
|
t.Fatalf("progressing upload cut off before routing: %d %q %v", r.code, r.body, r.err)
|
|
}
|
|
if n := watchedHold(t, s).bodyRead.Load(); n != int64(len(wire)-1) {
|
|
t.Fatalf("upload credit counts %d bytes, want %d wire bytes", n, len(wire)-1)
|
|
}
|
|
if len(up.sent("text")) != 0 {
|
|
t.Fatal("provider asked before the upload finished")
|
|
}
|
|
if _, err := pw.Write(wire[len(wire)-1:]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pw.Close()
|
|
r := <-got
|
|
if r.err != nil || r.code != http.StatusOK {
|
|
t.Fatalf("completed upload: %d %q %v", r.code, r.body, r.err)
|
|
}
|
|
if len(up.sent("text")) != 1 {
|
|
t.Fatal("completed upload was not sent exactly once")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A stalled upload still ends on its socket's idle deadline even after
|
|
// receiving enough bytes to earn additional time at the routing hold.
|
|
func TestUnroutedUploadCreditKeepsIdleDeadline(t *testing.T) {
|
|
unroutedLimits(t, 20*time.Millisecond, time.Second)
|
|
s, up := slowEyed(t, 0)
|
|
s.requestLimits.readTimeout = 200 * time.Millisecond
|
|
srv := httptest.NewServer(s.Front())
|
|
defer srv.Close()
|
|
conn, err := net.Dial("tcp", strings.TrimPrefix(srv.URL, "http://"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
conn.SetDeadline(time.Now().Add(10 * time.Second))
|
|
body := unroutedHi + strings.Repeat(" ", 128<<10)
|
|
fmt.Fprintf(conn, "POST /v1/chat/completions HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", len(body)+1, body)
|
|
res, err := http.ReadResponse(bufio.NewReader(conn), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := io.ReadAll(res.Body)
|
|
res.Body.Close()
|
|
if err != nil || res.StatusCode != http.StatusRequestTimeout || !strings.Contains(string(b), "request body read timed out") {
|
|
t.Fatalf("idle upload: %d %s %v", res.StatusCode, b, err)
|
|
}
|
|
if len(up.sent("text")) != 0 {
|
|
t.Fatal("stalled upload reached the provider")
|
|
}
|
|
}
|
|
|
|
// Upload time cannot be banked for a later internal hang, nor can the
|
|
// declared length buy time for bytes the client has not sent. Use fixed
|
|
// times to pin the rate boundary without a scheduler-dependent race.
|
|
func TestUnroutedUploadTimeIsBounded(t *testing.T) {
|
|
now := time.Now()
|
|
for _, tc := range []struct {
|
|
name string
|
|
read int64
|
|
spent time.Duration
|
|
reading bool
|
|
want time.Duration
|
|
}{
|
|
{"no bytes", 0, 8 * time.Second, true, 10 * time.Second},
|
|
{"below rate", unroutedUploadRate, 8 * time.Second, true, 9 * time.Second},
|
|
{"at rate", 8 * unroutedUploadRate, 8 * time.Second, true, 2 * time.Second},
|
|
{"above rate", 16 * unroutedUploadRate, 8 * time.Second, true, 2 * time.Second},
|
|
{"between reads", 8 * unroutedUploadRate, 8 * time.Second, false, 2 * time.Second},
|
|
{"fast body then internal hold", 16 * unroutedUploadRate, time.Second, false, 9 * time.Second},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
k := &waitKind{kind: "body", spent: tc.spent}
|
|
if tc.reading {
|
|
k.n, k.spent, k.since = 1, 0, now.Add(-tc.spent)
|
|
}
|
|
u := &unroutedHold{f: &inflight{start: now.Add(-10 * time.Second)}, kinds: []*waitKind{k}}
|
|
u.bodyRead.Store(tc.read)
|
|
if got := u.unmaskedLocked(now); got != tc.want {
|
|
t.Fatalf("internal hold = %s, want %s", got, tc.want)
|
|
}
|
|
// Unknown-work protection is independent of earned bytes.
|
|
if got := u.unknownLocked(now); got != 10*time.Second {
|
|
t.Fatalf("upload bytes excused unknown work: %s", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Compressed bodies can carry arbitrarily many wire bytes with little
|
|
// decoded output (empty gzip members or zstd skippable frames). They must
|
|
// still exhaust the hold, even while making progress and never going idle.
|
|
func TestUnroutedUploadAllowanceHasCeiling(t *testing.T) {
|
|
now := time.Now()
|
|
for _, read := range []int64{32 << 20, 1<<63 - 1} {
|
|
u := &unroutedHold{
|
|
f: &inflight{start: now.Add(-11 * time.Minute)},
|
|
kinds: []*waitKind{{kind: "body", n: 1, since: now.Add(-10 * time.Minute)}},
|
|
}
|
|
u.bodyRead.Store(read)
|
|
// At most eight minutes of upload may be excused. Together
|
|
// with the two-minute base budget this bounds a non-masking hold.
|
|
if held := u.unmaskedLocked(now); held != 3*time.Minute {
|
|
t.Errorf("%d wire bytes: charged hold = %s, want 3m", read, held)
|
|
}
|
|
}
|
|
}
|