Files
magpie/internal/gateway/queue_keepalive_test.go
T
yetone c0a13dee3c gateway: a queued stream's agent, kept alive while it waited, goes on hearing a keepalive every 15s once the request goes to its vendor, not up to 30s of nothing
keepQueued keeps a streaming agent alive while its request waits for a
slot (MaxConcurrency) or for room in its minute (MaxRPM): past
keepHeldAfter it sends the stream's 200 and a comment every
keepaliveEvery (15s each). When the wait ends, keepQueued stops and the
try's watch starts, and watch only called keepQuiet once keepHeldAfter
(15s) had passed since it began, as if the agent had heard nothing yet.
An agent sent its last keepalive just before the handoff then went up
to keepHeldAfter + keepaliveEvery, ~30s, with nothing while the vendor
was quiet, twice the cadence magpie promises. Found while fixing
TestQueuedStreamKeptAliveForTheMinute's flake (4caae1c8).

watch now calls keepQuiet at once when the request's agent has already
been sent the stream's 200 (h.alive.sent), as keepQueued does;
keepAlive still paces the comments by keepaliveEvery since the last
one (a.at), so nothing is sent more often. scan's keepalive for a held
stream of frames takes the same condition. The 200 is already out in
that case, so the wait keepHeldAfter gave (a refusal still going to
another account with nothing sent) buys nothing.

The scan change is for the same rule in the other place that waits
keepHeldAfter; it isn't observable by itself, since watch's tick
keeps a held stream alive too. pauseAlive counts keepHeldAfter from the
request's start, not from a handoff, so it has no such gap.

Test: TestQueuedStreamKeptAliveOnceSentToItsVendor (chat, responses,
messages). The timing is injected, not waited for: keepHeldAfter
1.5s, keepaliveEvery 100ms, a 4s minute so the queued request is kept
alive before its handoff even when it arrives late under load, and a
vendor that stays quiet 1s on every request after the first. As
3f4287e6's tests do, it checks the gateway's own writes in order rather
than gaps a client measures (they stretch under load): wroteConn now
also logs every write on the agent's connection, and the vendor notes
when it answers. The agent must have been written to before the vendor
was asked, and again between the vendor being asked and its answer:
10 keepalives are due in that second, and the old watch, begun before
the vendor was asked, writes none until keepHeldAfter after it began.

Run:
- watch's change reverted by hand: all 3 subtests fail with "the
  gateway wrote nothing to the kept-alive agent in the 1.001s between
  the vendor being asked and its answer, want a keepalive every
  100ms"; with it, they pass.
- go test -race -count=5 -run TestQueued ./internal/gateway.
- gofmt -l internal/gateway; go vet, GOOS=linux and GOOS=windows go vet
  -tags nogui ./internal/gateway.
- go test -tags nogui -timeout 40m ./... under the suite lock.
2026-10-11 06:19:16 +08:00

443 lines
18 KiB
Go

package gateway
import (
"bufio"
"context"
"io"
"net"
"net/http"
"net/http/httptest"
"net/http/httptrace"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// streamVendor streams a short reply on chat completions, Responses and
// Messages; fail is how many requests it answers 500 "overloaded" first,
// after the first it lets through.
type streamVendor struct {
mu sync.Mutex
seen int
at []time.Time // when each request arrived
fail int
block chan struct{} // the first request waits on it, if set
later time.Duration // the requests after the first wait this, quiet
// when it began to answer each request, after block and later
answered []time.Time
}
// arrived is when the vendor's n-th request (from 0) arrived.
func (v *streamVendor) arrived(n int) (time.Time, bool) {
v.mu.Lock()
defer v.mu.Unlock()
if n < len(v.at) {
return v.at[n], true
}
return time.Time{}, false
}
func (v *streamVendor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
v.mu.Lock()
v.seen++
v.at = append(v.at, time.Now())
n := v.seen
failing := n > 1 && v.fail > 0
if failing {
v.fail--
}
v.mu.Unlock()
if n == 1 && v.block != nil {
select {
case <-v.block:
case <-r.Context().Done():
return
}
}
if n > 1 && v.later > 0 {
select {
case <-time.After(v.later):
case <-r.Context().Done():
return
}
}
v.mu.Lock()
v.answered = append(v.answered, time.Now())
v.mu.Unlock()
if failing {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(500)
io.WriteString(w, `{"error":{"type":"api_error","message":"the vendor is overloaded"}}`)
return
}
w.Header().Set("Content-Type", "text/event-stream")
switch {
case strings.HasSuffix(r.URL.Path, "/messages"):
io.WriteString(w, "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_1\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"m\",\"content\":[],\"usage\":{\"input_tokens\":3,\"output_tokens\":0}}}\n\n"+
"event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n"+
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"queued ok\"}}\n\n"+
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"+
"event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":2}}\n\n"+
"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n")
case strings.HasSuffix(r.URL.Path, "/responses"):
io.WriteString(w, quietCreated+quietDelta("queued ok")+quietDone)
default:
io.WriteString(w, "data: {\"id\":\"c1\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"queued ok\"}}]}\n\n"+
"data: {\"id\":\"c1\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":2,\"total_tokens\":5}}\n\n"+
"data: [DONE]\n\n")
}
}
// queueGateway is the real server (lanGuard in front) with a provider "q"
// of one key, set up by p, and every keepalive within milliseconds.
func queueGateway(t *testing.T, v *streamVendor, set func(*provider.Provider)) string {
t.Helper()
fresh(t)
keepFast(t, keepaliveGap, 40*time.Millisecond, keepaliveLongest)
k, w := keepHeldAfter, watchEvery
keepHeldAfter, watchEvery = 80*time.Millisecond, 10*time.Millisecond
t.Cleanup(func() { keepHeldAfter, watchEvery = k, w })
up := httptest.NewServer(v)
t.Cleanup(up.Close)
p := provider.Provider{ID: "q", Name: "Queued", Key: "k", Models: []string{"m"}, Chat: up.URL + "/v1", Responses: up.URL + "/v1", Anthropic: up.URL}
set(&p)
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
gw := httptest.NewUnstartedServer(lanGuard(New().Handler()))
gw.Listener = wroteListener{gw.Listener}
gw.Start()
t.Cleanup(gw.Close)
return gw.URL
}
// wrote is when the gateway first wrote to each agent's connection, by
// the agent's address: the time the gateway sent it, which a client's own
// read can't tell under load, its goroutine waiting to run.
var wrote sync.Map // string -> *atomic.Int64
type wroteListener struct{ net.Listener }
func (l wroteListener) Accept() (net.Conn, error) {
c, err := l.Listener.Accept()
if err != nil {
return c, err
}
at, all := new(atomic.Int64), new(writeLog)
wrote.Store(c.RemoteAddr().String(), at)
writes.Store(c.RemoteAddr().String(), all)
return wroteConn{c, at, all}, nil
}
// writes is every time the gateway wrote to each agent's connection, by
// the agent's address.
var writes sync.Map // string -> *writeLog
type writeLog struct {
mu sync.Mutex
at []time.Time
}
// within says how many writes were after from and before to.
func (l *writeLog) within(from, to time.Time) int {
l.mu.Lock()
defer l.mu.Unlock()
n := 0
for _, t := range l.at {
if t.After(from) && t.Before(to) {
n++
}
}
return n
}
type wroteConn struct {
net.Conn
at *atomic.Int64
all *writeLog
}
func (c wroteConn) Write(b []byte) (int, error) {
n, err := c.Conn.Write(b)
if n > 0 {
now := time.Now()
c.at.CompareAndSwap(0, now.UnixNano())
c.all.mu.Lock()
c.all.at = append(c.all.at, now)
c.all.mu.Unlock()
}
return n, err
}
var queueAsks = []struct{ name, path, body string }{
{"chat", "/v1/chat/completions", `{"model":"q/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`},
{"responses", "/v1/responses", `{"model":"q/m","stream":true,"input":"hi"}`},
{"messages", "/v1/messages", `{"model":"q/m","stream":true,"max_tokens":50,"messages":[{"role":"user","content":"hi"}]}`},
}
// queueMinute is the minute of the tests that wait for room in it. A queued
// request waits for what is left of it when it arrives, which under load
// is late: it is long enough that what is left stays far over
// keepHeldAfter (a request ~1.4s late was seen with 8 busy goroutines per
// core).
const queueMinute = 3 * time.Second
// askHeard sends body to path as an agent would and returns the status, the
// stream read, when the gateway first wrote to the agent (wrote), and the
// read's error. heard, if set, is called once the agent has a first byte.
func askHeard(gw, path, body string, heard func()) (int, string, time.Time, error) {
req, _ := http.NewRequest("POST", gw+path, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
var from string
req = req.WithContext(httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{
GotConn: func(i httptrace.GotConnInfo) { from = i.Conn.LocalAddr().String() },
GotFirstResponseByte: func() {
if heard != nil {
heard()
}
},
}))
res, err := (&http.Client{Transport: &http.Transport{DisableKeepAlives: true}, Timeout: 30 * time.Second}).Do(req)
if err != nil {
return 0, "", time.Time{}, err
}
defer res.Body.Close()
var b strings.Builder
_, err = io.Copy(&b, bufio.NewReader(res.Body))
var first time.Time
if at, ok := wrote.Load(from); ok {
if n := at.(*atomic.Int64).Load(); n != 0 {
first = time.Unix(0, n)
}
}
return res.StatusCode, b.String(), first, err
}
// waitSeen waits until v has had n requests.
func waitSeen(t *testing.T, v *streamVendor, n int) {
t.Helper()
for end := time.Now().Add(10 * time.Second); ; time.Sleep(5 * time.Millisecond) {
if _, ok := v.arrived(n - 1); ok {
return
}
if time.Now().After(end) {
t.Fatalf("the vendor never had %d requests", n)
}
}
}
// coeo91 on Discord: with a Requests per minute limit, a request held for
// room in the minute had its agent hear nothing at all — WorkBuddy, Trae
// and Qoder said it timed out. Through the real server, a streaming
// request that waits for the minute (queueMinute) hears the stream's 200 and
// keepalives before the vendor is asked, then the vendor's reply, in each
// protocol's stream. The test asks for that order, as the gateway wrote
// it, rather than for a client's idle timeout: under load (machine load
// ~130, 20582866a) every gap a client measures stretched, the first
// request's too, the client's goroutine waiting to run.
func TestQueuedStreamKeptAliveForTheMinute(t *testing.T) {
for _, a := range queueAsks {
t.Run(a.name, func(t *testing.T) {
shortMinute(t, queueMinute, time.Minute)
v := &streamVendor{}
gw := queueGateway(t, v, func(p *provider.Provider) { p.MaxRPM = 1 })
if code, body, _, err := askHeard(gw, a.path, a.body, nil); err != nil || code != 200 {
t.Fatalf("first: %d %v %s", code, err, body)
}
code, body, heard, err := askHeard(gw, a.path, a.body, nil)
if err != nil || code != 200 || !strings.Contains(body, ": keepalive") || !strings.Contains(body, "queued ok") {
t.Fatalf("queued = %d %v %q, want 200, keepalives, then the reply", code, err, body)
}
first, _ := v.arrived(0)
asked, ok := v.arrived(1)
if !ok || heard.IsZero() || !heard.Before(asked) {
t.Fatalf("the gateway first wrote to the agent at %s, the vendor was asked at %s: nothing reached it while it waited", heard.Format("15:04:05.000"), asked.Format("15:04:05.000"))
}
// it waited for the minute: the vendor sees the first late
// under load (rpmSlack, zero here), so half of it
if d := asked.Sub(first); d < queueMinute/2 {
t.Fatalf("the vendor was asked %s after the first, inside the minute", d)
}
})
}
}
// Once a queued request's agent has been kept alive, it hears a keepalive
// every keepaliveEvery after the request goes to its vendor too, not
// keepHeldAfter after that: the vendor's watch waited keepHeldAfter
// before its first keepalive, as if the agent had heard nothing, so an
// agent sent one just before the handoff went up to keepHeldAfter plus
// keepaliveEvery (30s with the real 15s each) with none. Here
// keepHeldAfter is 1.5s, keepaliveEvery 100ms, and the vendor stays
// quiet 1s on the queued request: the gateway must write to the agent
// between the vendor being asked and its answer, where the old watch,
// begun before the vendor was asked, wrote nothing until keepHeldAfter
// after it began. The check is on the gateway's own writes, in order,
// not on gaps a client measures, which stretch under load (3f4287e6).
func TestQueuedStreamKeptAliveOnceSentToItsVendor(t *testing.T) {
for _, a := range queueAsks {
t.Run(a.name, func(t *testing.T) {
// what is left of the minute when the queued request arrives,
// late under load, stays over keepHeldAfter, so it is kept
// alive before the handoff
shortMinute(t, 4*time.Second, time.Minute)
v := &streamVendor{later: time.Second}
gw := queueGateway(t, v, func(p *provider.Provider) { p.MaxRPM = 1 })
keepFast(t, keepaliveGap, 100*time.Millisecond, keepaliveLongest)
keepHeldAfter = 1500 * time.Millisecond // put back by queueGateway's cleanup
if code, body, _, err := askHeard(gw, a.path, a.body, nil); err != nil || code != 200 {
t.Fatalf("first: %d %v %s", code, err, body)
}
req, _ := http.NewRequest("POST", gw+a.path, strings.NewReader(a.body))
req.Header.Set("Content-Type", "application/json")
var from string
req = req.WithContext(httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{
GotConn: func(i httptrace.GotConnInfo) { from = i.Conn.LocalAddr().String() },
}))
res, err := (&http.Client{Transport: &http.Transport{DisableKeepAlives: true}, Timeout: 30 * time.Second}).Do(req)
if err != nil {
t.Fatal(err)
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil || res.StatusCode != 200 || !strings.Contains(string(b), ": keepalive") || !strings.Contains(string(b), "queued ok") {
t.Fatalf("queued = %d %v %q, want 200, keepalives, then the reply", res.StatusCode, err, b)
}
l, ok := writes.Load(from)
if !ok {
t.Fatal("no writes recorded for the agent's connection")
}
log := l.(*writeLog)
v.mu.Lock()
asked, answered := v.at[1], v.answered[1]
v.mu.Unlock()
if log.within(time.Time{}, asked) == 0 {
t.Fatalf("the agent wasn't kept alive before the vendor was asked")
}
if n := log.within(asked, answered); n == 0 {
t.Fatalf("the gateway wrote nothing to the kept-alive agent in the %s between the vendor being asked and its answer, want a keepalive every %s", answered.Sub(asked).Round(time.Millisecond), keepaliveEvery)
}
})
}
}
// A slot of MaxConcurrency waited for is kept alive the same way.
func TestQueuedStreamKeptAliveForASlot(t *testing.T) {
one := 1
v := &streamVendor{block: make(chan struct{})}
gw := queueGateway(t, v, func(p *provider.Provider) { p.MaxConcurrency = &one })
first := make(chan error, 1)
go func() {
_, _, _, err := askHeard(gw, queueAsks[0].path, queueAsks[0].body, nil)
first <- err
}()
waitSeen(t, v, 1) // the first has the slot
// the slot is freed once the queued agent has heard something, or
// after 5s when nothing reaches it
var once sync.Once
free := func() { once.Do(func() { close(v.block) }) }
time.AfterFunc(5*time.Second, free)
code, body, heard, err := askHeard(gw, queueAsks[0].path, queueAsks[0].body, free)
if err != nil || code != 200 || !strings.Contains(body, ": keepalive") || !strings.Contains(body, "queued ok") {
t.Fatalf("queued for a slot = %d %v %q, want 200, keepalives, then the reply", code, err, body)
}
if asked, ok := v.arrived(1); !ok || heard.IsZero() || !heard.Before(asked) {
t.Fatalf("the gateway first wrote to the agent at %s, the vendor was asked at %s: nothing reached it while it waited", heard.Format("15:04:05.000"), asked.Format("15:04:05.000"))
}
if err := <-first; err != nil {
t.Fatal(err)
}
}
// Once a queued request's agent has the stream's 200, the vendor failing
// after it reaches the agent as its stream's error event, in its
// protocol's own shape, not as a status the agent can no longer be told,
// nor a JSON error body inside the stream.
func TestQueuedStreamFailureIsTheStreamsError(t *testing.T) {
wants := map[string][]string{
"chat": {`"error"`, "the vendor is overloaded"},
"responses": {"event: response.failed", `"status":"failed"`, "the vendor is overloaded"},
"messages": {"event: error", `"type":"error"`, "the vendor is overloaded"},
}
for _, a := range queueAsks {
t.Run(a.name, func(t *testing.T) {
shortMinute(t, queueMinute, time.Minute)
gw := queueGateway(t, &streamVendor{fail: 10}, func(p *provider.Provider) { p.MaxRPM = 1 })
if code, body, _, err := askHeard(gw, a.path, a.body, nil); err != nil || code != 200 {
t.Fatalf("first: %d %v %s", code, err, body)
}
code, body, _, err := askHeard(gw, a.path, a.body, nil)
if err != nil || code != 200 || !strings.Contains(body, ": keepalive") {
t.Fatalf("queued = %d %v %q, want the stream's 200 and keepalives", code, err, body)
}
for _, w := range wants[a.name] {
if !strings.Contains(body, w) {
t.Fatalf("stream %q doesn't say %s", body, w)
}
}
if strings.Contains(body, `{"error":{"type":"api_error","message":"the vendor is overloaded"}}`+"\n") || strings.HasPrefix(strings.TrimLeft(strings.ReplaceAll(body, ": keepalive\n", ""), "\n"), "{") {
t.Fatalf("the vendor's JSON error went into the stream raw: %q", body)
}
})
}
}
// A request that waits out its queue (QueueWait) after keepalives were
// sent is told so as its stream's error, saying the limit.
func TestQueuedStreamWaitedOutIsTheStreamsError(t *testing.T) {
one := 1
v := &streamVendor{block: make(chan struct{})}
defer close(v.block)
gw := queueGateway(t, v, func(p *provider.Provider) { p.MaxConcurrency, p.QueueWait = &one, 1 })
go askHeard(gw, queueAsks[2].path, queueAsks[2].body, nil)
waitSeen(t, v, 1) // the first has the slot
code, body, _, err := askHeard(gw, queueAsks[2].path, queueAsks[2].body, nil)
if err != nil || code != 200 || !strings.Contains(body, ": keepalive") || !strings.Contains(body, "event: error") || !strings.Contains(body, "requests at once is its limit") {
t.Fatalf("waited out = %d %v %q, want keepalives, then the limit as the stream's error", code, err, body)
}
}
// A wait shorter than keepHeldAfter sends nothing ahead: turned away, it
// is still a 429 with its Retry-After.
func TestQueuedBrieflyStill429(t *testing.T) {
shortMinute(t, 10*time.Second, 50*time.Millisecond)
gw := queueGateway(t, &streamVendor{}, func(p *provider.Provider) { p.MaxRPM = 1 })
if code, _, _, err := askHeard(gw, queueAsks[0].path, queueAsks[0].body, nil); err != nil || code != 200 {
t.Fatalf("first: %d %v", code, err)
}
code, body, _, err := askHeard(gw, queueAsks[0].path, queueAsks[0].body, nil)
if err != nil || code != 429 || strings.Contains(body, "keepalive") {
t.Fatalf("second = %d %v %q, want a plain 429", code, err, body)
}
}
// A try that wasn't held — the last one, its retries spent — and whose
// agent was kept alive while it waited is held from then on: the vendor's
// error is the stream's to tell (failTo), not a JSON body written into
// the stream the agent already has.
func TestQueuedUnheldTryHeldOnceKeptAlive(t *testing.T) {
k, w := keepHeldAfter, watchEvery
keepHeldAfter, watchEvery = 20*time.Millisecond, 5*time.Millisecond
t.Cleanup(func() { keepHeldAfter, watchEvery = k, w })
rec := httptest.NewRecorder()
hw := newHoldWriter(rec, false)
hw.alive, hw.streams = &keptAlive{proto: provider.Chat}, true
stop := hw.keepQueued()
time.Sleep(80 * time.Millisecond)
stop()
if !hw.alive.sent {
t.Fatal("the agent wasn't kept alive while its try waited")
}
hw.Header().Set("Content-Type", "application/json")
hw.WriteHeader(400)
hw.Write([]byte(`{"error":{"message":"bad request"}}`))
if hw.passing || strings.Contains(rec.Body.String(), "bad request") {
t.Fatalf("the vendor's error went into the kept-alive stream: passing %v, %q", hw.passing, rec.Body.String())
}
}