diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 2f402e5c0..6a081cf26 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -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`, diff --git a/internal/gateway/unrouted.go b/internal/gateway/unrouted.go index d37604c10..af20223e3 100644 --- a/internal/gateway/unrouted.go +++ b/internal/gateway/unrouted.go @@ -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. diff --git a/internal/gateway/unrouted_upload_bounds_test.go b/internal/gateway/unrouted_upload_bounds_test.go index 38f7c2ec0..cbc0f7b3e 100644 --- a/internal/gateway/unrouted_upload_bounds_test.go +++ b/internal/gateway/unrouted_upload_bounds_test.go @@ -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") + } +}