Files
magpie/internal/gateway/patience.go
T
yetone 63c91cae5f gateway: the body's key checks look for the needle's rarest byte
Each request ran ~45 bytes.Contains over its whole body to ask whether
a key is there at all ("developer", "compaction, "display", the
passthrough key...). Most needles begin with a quote, JSON's commonest
byte, so bytes.Index stopped at every quote, gave up on that and
compared at every byte: on a 2 MB body ~9 ms a request, 36% of the
gateway's CPU in BenchmarkLargeRequest's profile.

bodyHas/bodyIndex find the needle's rarest byte (a static rank of the
bytes JSON, English and code use most) with IndexByte and compare the
needle only there; a needle of common bytes only hands the rest to
strings.Index after 64 misses, so it is never slower than before, and
it allocates nothing (a []byte(sep) of a needle over 32 bytes did).
148 literal and constant needles in internal/gateway use it, and
mayHoldHEIC's "eXB" walk uses bodyIndex.

mayHoldOddBase64 walked every byte through a switch (~2 ms a 2 MB
body); a run it looks for ends a string, so it now looks back from each
quote with a class table and gives the same answer.

Run on mica (Apple M6) at this tree:
- BenchmarkBodyHas, 7 needles over a 2 MB body: 4.43 ms -> 0.38 ms,
  0 allocs (was 1).
- BenchmarkLargeRequest -benchtime 200x, before -> after:
  anthropic 31.2 -> 28.9 ms, anthropic-group 31.4 -> 27.8,
  chat 35.7 -> 32.8, responses 39.8 -> 29.8.
- go vet ./internal/gateway; gofmt -l clean.
- go test ./internal/gateway -count 1 -timeout 45m: ok (858s).
- Broken by hand: bodyIndex's start off by one -> TestBodyIndexIsIndex
  red (wrong index on 5 needles and the random bodies); the URL-safe
  class never counted -> TestOddBase64LooksAsItWalked red. Both green
  again restored.

TestBodyIndexIsIndex holds bodyIndex to bytes.Index on a large
conversation and 3000 random bodies of common and rare bytes;
TestOddBase64LooksAsItWalked holds the new check to the old byte walk
(kept in the test) on 20000 random bodies and the three protocols'
large conversations.

Real account: the built app (mote, sandbox HOME) sent a 6KB request to
DeepSeek on /v1/responses and /v1/messages, both 200 "pong". Not
tried: Codex (the sandbox's sign-in is stale, and renewing it there
would rotate the live refresh token).
2026-11-14 05:51:42 +08:00

304 lines
11 KiB
Go

package gateway
import (
"context"
"net/http"
"slices"
"time"
"github.com/yetone/magpie/internal/lazyre"
"github.com/yetone/magpie/internal/provider"
)
// didNotStream is in the error for an upstream that answered a stream
// request with a whole reply: no ask of it will read as a stream, however
// often it goes out, so it is no failure a moment may mend — the same-one
// retries (passing) and a group's patience (busy) both leave it alone.
const didNotStream = "did not stream"
// A group's patience (#1418): when every member has failed in a way that
// passes — overloaded, busy, rate limited for a moment — and nothing of a
// reply has gone to the agent, the request isn't ended on the last one's
// error. After a pause, longer each round and never shorter than a
// member's Retry-After, those members are asked again in turn, for as long
// as the group's Patience (provider.Group.Waits) lasts, counted from the
// first time every member had failed, when the agent would have had the
// error. The members take turns: the last one isn't first asked again
// alone, as the last one left of a plain provider is (passing), while
// another member is busy too. A streaming agent is kept alive with SSE
// comments through the pause, as through a held stream. A plain provider,
// which the agent picked as itself, is kept the same way once the retries
// passing gives the last one left are spent, its accounts its members,
// for its own Patience, unset singlePatience.
// singlePatience is how long a request to one provider, not a group, is
// kept once it has failed in a way that passes and the same-one retries
// (passing) are spent: as long as a group that sets no Patience waits
// (zoecats on Discord), unless the provider sets its own Patience (magpie
// provider set <id> patience=), which wins. A group's, set or not, is the
// group's.
var singlePatience = provider.PatienceDefault * time.Second
// replanRounds is how many times the members are asked again, at most:
// a backstop that never comes before the patience does. The pauses alone
// (2s, 4s, 8s, 16s, then 30s each) add up to PatienceLongest in 11 rounds,
// so a group set to wait 240s waits 240s; with 6 it gave up at 90s of
// pauses, whatever it was set to (real-jacket on #1418: patience=240,
// replan=6, ~103s in all).
const replanRounds = 12
// replanLongest is the longest pause between rounds: 30 times retryPause,
// 30s.
func replanLongest() time.Duration { return 30 * retryPause }
// busyTry is a member that failed in a way that passes, and when it may
// be asked again: now, or when its Retry-After says.
type busyTry struct {
c candidate
ready time.Time
}
// busy says whether a failure is one a member may be over in a moment:
// overloaded, unavailable, timed out, or a rate limit that isn't the
// plan's or the money's, nor an account's token wall (walled). A proxy
// that refused the connection, a quota used up, response protection's
// 502, a void cut at its length (cutVoidWords, #2972), an upstream
// that answered a stream request with a whole reply (didNotStream), or
// the Qoder plugin's queue answer (queued) isn't: each fails the same
// however often it is asked.
func busy(status int, body []byte) bool {
switch {
case busyStatus(status):
return failure(status, body) == failOther && !protectionRefused(status, body) &&
!bodyHas(body, didNotStream) &&
!bodyHas(body, cutVoidWords) &&
!queued(body)
case status == 429:
return failure(status, body) == failRate && !creditWords.Match(wordsOf(body)) && !walled(body)
}
return false
}
// queuedWords matches the 503 the Qoder plugin answers when Qoder put a
// request in the model's queue (its code 10605) and magpie has another
// candidate to ask (#4049: "Qoder queued the request for efficient (this
// plan's queue: p3, 3 ahead); try again in 30s", or "Qoder is busy for
// efficient (this plan's queue: p3); …" when the model's service is out).
// Asked again as a group's patience asks, the members only meet the same
// queue again, every body sent again on every account, a whole round at
// a time (#5346: 217 accounts, one request tried 656 times, 20 GB of
// re-uploaded bodies a day). The last one left is asked untold, and the
// plugin waits the queue out as Qoder's own client does, asking its
// queue status rather than sending the request again. Only the plugin's
// own words are matched: a relay's 503 that names its own queue ("queue
// full", 排队中) is a moment's overload, waited out as before.
var queuedWords = lazyre.New(`(?i)\bqueued the request\b|\bthis plan's queue:`)
// queued says whether a failure's body is the Qoder plugin's queue answer.
func queued(body []byte) bool {
return queuedWords.Match(wordsOf(body))
}
// busyStatus says whether an error status is one a vendor or the relay in
// front of it gives for a moment's trouble: a timeout, an internal error,
// a gateway or the service unavailable, Anthropic's 529 overloaded, and
// Cloudflare's 520 to 524 (the origin gave an unknown error, is down,
// timed out, unreachable, or slow to answer), which a relay behind
// Cloudflare answers with now and then (vdaoren on Discord: an API-key
// provider's 4xx/5xx went straight to the agent). The same-one retries
// (passing) and the patience's rounds (busy) both read it.
func busyStatus(status int) bool {
switch status {
case 408, 500, 502, 503, 504, 529, 520, 521, 522, 523, 524:
return true
}
return false
}
// noteBusy adds c to the members to ask again, once, with when its vendor
// said it may be asked.
func noteBusy(list []busyTry, c candidate, header http.Header) []busyTry {
if slices.ContainsFunc(list, func(b busyTry) bool { return b.c.restKey() == c.restKey() && b.c.effort == c.effort }) {
return list
}
now := time.Now()
return append(list, busyTry{c: c, ready: now.Add(retryAfter(header, now))})
}
// waitsOf is how long a member may be kept for replan: its own Patience
// seconds when set (including PatienceOff → 0), else the asked group's
// Waits (fallback).
func waitsOf(patience int, fallback time.Duration) time.Duration {
switch {
case patience < 0:
return 0
case patience == 0:
return fallback
}
return time.Duration(min(patience, provider.PatienceLongest)) * time.Second
}
// maxWaits is the longest waitsOf among cands, for holding the last
// failure while any member's budget may still cover a replan (#2410).
func maxWaits(cands []candidate, fallback time.Duration) time.Duration {
var m time.Duration
for _, c := range cands {
if w := waitsOf(c.patience, fallback); w > m {
m = w
}
}
return m
}
// replan is, of the members that failed so in this round, those to ask
// again and the pause before: retryPause doubled each round up to
// replanLongest, and as long as the soonest Retry-After. Each member's
// own Patience budget (#2410) is counted from waitFrom; a member whose
// budget is spent, or Off, is left out. ok is false when the rounds are
// spent, or no member still has budget covering the pause.
func replan(list []busyTry, round int, waitFrom time.Time, fallback time.Duration) ([]candidate, time.Duration, bool) {
if len(list) == 0 || round >= replanRounds {
return nil, 0, false
}
now := time.Now()
wait := min(retryPause<<(round+1), replanLongest())
// a vendor's time is read in whole seconds: one back within a second
// past a member's budget is asked as the budget ends, not given up on
// (zoecats on Discord: "will reset in 1 minutes" against the minute a
// lone provider waits by default, which the moment between the
// failure and its note put a hair past)
list = slices.Clone(list)
for i, b := range list {
if budget := waitsOf(b.c.patience, fallback); budget > 0 {
end := waitFrom.Add(budget)
if b.ready.After(end) && !b.ready.After(end.Add(time.Second)) {
list[i].ready = end
}
}
}
soonest := list[0].ready
for _, b := range list[1:] {
soonest = minTime(soonest, b.ready)
}
wait = max(wait, soonest.Sub(now))
at := now.Add(wait)
var alive []busyTry
for _, b := range list {
budget := waitsOf(b.c.patience, fallback)
if budget <= 0 {
continue
}
if at.After(waitFrom.Add(budget)) {
continue // this member's budget is spent
}
alive = append(alive, b)
}
if len(alive) == 0 {
return nil, 0, false
}
var next, later []candidate
for _, b := range alive {
if b.ready.After(at) {
later = append(later, b.c) // still asking for time: after those back
continue
}
next = append(next, b.c)
}
return append(next, later...), wait, true
}
func minTime(a, b time.Time) time.Time {
if b.Before(a) {
return b
}
return a
}
// pauseAlive waits d, keeping a streaming agent alive as a held stream
// does once the request has gone keepHeldAfter without a word. false: the
// agent went away first. A pause ends early once moved (when given) says
// a provider about to be asked again was saved at another address
// (readdressedOf).
func pauseAlive(ctx context.Context, w http.ResponseWriter, kept *keptAlive, streams bool, start time.Time, d time.Duration, moved func() bool) bool {
t := time.NewTimer(d)
defer t.Stop()
edits := editsOf(moved)
if moved != nil && moved() {
return true // saved elsewhere while it was asked
}
var hw *holdWriter
if streams && kept != nil && kept.proto != provider.Gemini {
hw = newHoldWriter(w, true)
hw.alive, hw.streams = kept, true
}
tick := time.NewTicker(min(watchEvery, max(d, time.Millisecond)))
defer tick.Stop()
for {
if hw != nil && time.Since(start) >= keepHeldAfter {
hw.keepAlive()
}
select {
case <-t.C:
return true
case <-ctx.Done():
return false
case <-tick.C:
case <-edits:
if moved() {
return true
}
edits = provider.Edited()
}
}
}
// pauseUntilReaddressed waits d, or until moved (when given) says a
// provider about to be asked again was saved at another address. false:
// ctx ended first.
func pauseUntilReaddressed(ctx context.Context, d time.Duration, moved func() bool) bool {
t := time.NewTimer(d)
defer t.Stop()
edits := editsOf(moved)
if moved != nil && moved() {
return true // saved elsewhere while it was asked
}
for {
select {
case <-t.C:
return true
case <-ctx.Done():
return false
case <-edits:
if moved() {
return true
}
edits = provider.Edited()
}
}
}
// editsOf is provider.Edited when there is a moved to ask, else a channel
// that never fires.
func editsOf(moved func() bool) <-chan struct{} {
if moved == nil {
return nil
}
return provider.Edited()
}
// readdressedOf says whether any of cs is a Remote magpie saved at another
// address since the request read it: the user typed its new address in
// while the request paused before asking it again (le13ure on X). nil
// when none of cs is a Remote magpie, for a pause that never wakes early.
func readdressedOf(cs []candidate) func() bool {
if !slices.ContainsFunc(cs, func(c candidate) bool { return c.p.IsRemoteMagpie() }) {
return nil
}
return func() bool {
return slices.ContainsFunc(cs, func(c candidate) bool {
_, ok := provider.Readdressed(c.p)
return ok
})
}
}