gateway: most's timer looks again no sooner than a second while an upload earns (#4775)

Cause: with wuuli's upload credit, a request's charged time stands
still while its body earns at least as fast as it is read. When little
was left, checkMost re-armed most's timer at that sliver, and again
every time it fired, for as long as the body kept coming. In the #4775
review, a hold frozen 100µs short of unroutedMost was looked at 6731
times in a second.

Change: checkMost waits at least unroutedRecheck (1s, or an eighth of
unroutedMost when a test shortens it) before it looks again. A hold is
answered at most that much late. Masking, the 30s unknown-work guard,
the 60s socket idle deadline, the decoded cap and the trickle's 408
are unchanged.

Test: TestUnroutedMostRecheckIsNoSpin freezes a hold's charged time a
sliver short of unroutedMost and counts the timer's looks in a second.
It allows 30.

Run, via mote-run (macOS arm64, Go 1.26.8), at this tree before the
rebase (base affa979d5):
- gofmt -l: clean. go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...: all exit 0.
- go test -tags nogui -race -count=5 of the ten upload and body tests
  (the patches' four, this series' five, TrickledBodyAnswered408): ok
  (102.5s).
- go test -tags nogui -timeout 45m ./internal/gateway/...: one red
  (771.9s), TestCodexPoolSaysTheClientsVersion. That is #4807 (another
  test's Codex identity reaching it), fixed on main by 17f2387e1, which
  this tree's base predated.
- After the rebase onto f3335dd1b (which brings 17f2387e1), at
  698362aaf (this tree, before this message was amended):
  go test -tags nogui -timeout 45m ./internal/gateway/...: ok (775.7s).
- go test -run TestUnrouted at each of the three commits: ok.

Broken by hand, each with its test (this series and the patches):
- Reset(left) without the floor: NoSpin went red (6731 looks).
- origin/main's unrouted.go with only the two names the tests need:
  wuuli's TestUnroutedProgressingUploadOutlastsMost went red with 408
  at 1s (identity, chunked, gzip, zstd), and so did
  TestUnroutedUploadTimeIsBounded (5 of 6 cases). Leng's
  TestUnroutedUploadAllowanceHasCeiling charged 11m against 3m.
  TestUnroutedUploadCreditKeepsIdleDeadline passed, as a guard that
  holds on main too should.

Not tried: a real slow link or proxy, the reporter's own conversation,
and running on Linux or Windows (vet only).

Thanks to wuuli (Xulin Wu) and lengjingxu (Leng on X, who reported it
again) for the patches this series builds on.

Co-authored-by: Xulin Wu <wuxulin@bytedance.com>
Co-authored-by: Leng <Lengjingxu@gmail.com>
This commit is contained in:
yetone
2026-11-03 13:36:52 +08:00
co-authored by Xulin Wu Leng
parent b4e3885477
commit 897e88d7e6
3 changed files with 55 additions and 3 deletions
+4 -1
View File
@@ -390,7 +390,10 @@ recorded as a prediction failure (`noPrediction`).
(`bodyArriving`, `bodyEnded`): a decoder spends its time between reads
too, and a chunked gzip body ended there was answered 504 as magpie's
own hold (`TestUnroutedChunkedBodyReadWholeHeld504` holds the other
side).
side). While an upload earns as fast as it is read, its charged time
stands still, so `checkMost` looks again no sooner than
`unroutedRecheck` (1s) rather than every sliver left
(`TestUnroutedMostRecheckIsNoSpin`).
`TestUnroutedProgressingUploadOutlastsMost` covers TLS through `Front`
with declared, chunked, gzip and zstd bodies;
`TestUnroutedUploadCreditKeepsIdleDeadline`, `TestUnroutedUploadTimeIsBounded`,
+11 -2
View File
@@ -329,7 +329,11 @@ func (u *unroutedHold) unmaskedLocked(now time.Time) time.Duration {
// checkMost is most's timer: a request held unroutedMost other than
// masking and earned upload time is answered for; one masking now is
// looked at again when that ends, and one with time left, when that is up.
// looked at again when that ends, and one with time left, when that is up,
// but no sooner than unroutedRecheck. While an upload earns at least as
// fast as it is read, its held time stands still, so what is left of it
// doesn't shrink: a sliver left would be looked at again every sliver for
// as long as the body kept coming.
func (u *unroutedHold) checkMost() {
u.wmu.Lock()
if u.mwaits > 0 {
@@ -338,7 +342,7 @@ func (u *unroutedHold) checkMost() {
return
}
if left := unroutedMost - u.unmaskedLocked(time.Now()); left > 0 {
u.most.Reset(left)
u.most.Reset(max(left, unroutedRecheck()))
u.wmu.Unlock()
return
}
@@ -346,6 +350,11 @@ func (u *unroutedHold) checkMost() {
u.giveUp(false)
}
// unroutedRecheck is the least checkMost waits before it looks again: a
// second, or an eighth of unroutedMost when a test makes that shorter. A
// hold is answered at most that late.
func unroutedRecheck() time.Duration { return min(time.Second, unroutedMost/8) }
// noteWait tells ctx's watch its request is in a wait magpie knows of
// until the func it answers is called: the time it spends there counts
// toward unroutedMost alone, not unroutedUnknown.
@@ -12,6 +12,7 @@ import (
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
@@ -149,3 +150,42 @@ func TestUnroutedEmptyEncodedStreamIsBounded(t *testing.T) {
})
}
}
// #4775 review: an upload that earns at least as fast as it is read holds
// its charged time still, here a sliver short of unroutedMost. most's
// timer looks again no sooner than unroutedRecheck, rather than every
// sliver for as long as the body keeps coming.
func TestUnroutedMostRecheckIsNoSpin(t *testing.T) {
unroutedLimits(t, time.Hour, 800*time.Millisecond)
now := time.Now()
k := &waitKind{kind: "body", n: 1, since: now}
u := &unroutedHold{f: &inflight{start: now.Add(-(unroutedMost - 100*time.Microsecond))}, kinds: []*waitKind{k}}
u.bodyRead.Store(1 << 40)
var looks, running atomic.Int64
var stopped atomic.Bool
u.wmu.Lock()
u.most = time.AfterFunc(0, func() {
running.Add(1)
defer running.Add(-1)
if stopped.Load() {
return
}
looks.Add(1)
u.checkMost()
})
u.wmu.Unlock()
time.Sleep(time.Second)
stopped.Store(true)
for running.Load() > 0 {
time.Sleep(time.Millisecond)
}
u.most.Stop()
// a second at a look per 100ms is about 10; at a look per sliver it
// is thousands
if n := looks.Load(); n > 30 {
t.Fatalf("most's timer looked %d times in a second at a hold standing still", n)
}
if n := looks.Load(); n == 0 {
t.Fatal("most's timer never looked")
}
}