usrname1833 on Discord asked to see the raw request and response HTTP headers (查看原始请求和原始响应的http头), to debug as with a capture. magpie kept the client's request headers and its own response headers only in the request archive, and nothing of what went upstream. Now a call keeps its headers both ways (internal/gateway/wire_headers.go): - the client's, as they reached magpie: headerLogged in Handler() clones them for every request that isn't a GET, HEAD or OPTIONS; - each request magpie sent upstream for the call: method, address, headers, and the answer's status, protocol, headers and time, or the error. rpmTransport (under s.client) records it as it is sent; provider.SendsThroughPlugin records a plugin account's hand-off, shown as handed to the plugin, since the plugin's own request to its vendor is made in Bun and magpie doesn't see it (the view says so). The first 8 tries are kept, the rest counted. Recording copies each header map; nothing is masked or sorted as a request goes. Secrets are taken out when the headers are read (Call.Headers): by what each header is (new redact.SecretHeader, now behind redact.ScrubHeader: Authorization, cookies, x-api-key, api-key, any *-Key, *-Token, *-Secret, *-Auth, *-Signature, but Idempotency-Key and a count of tokens such as anthropic-ratelimit-tokens-remaining), any Bearer or Basic value, and redact.Scrub on the rest; an address loses its password, a secret-named query value (?key=) and its fragment. They live with the call in the 40-call ring; /api/gateway/call gives them as "headers". The request archive keeps the upstream ones as "upstream" beside the request's and response's headers, masked the same way. No body is stored that wasn't before. GUI: under a recent call's bodies on Gateway and a request's on Routing, "HTTP headers" folds open the client's headers, then each upstream request beside its answer (wireHeadersView, plugins/usage/page.js); the archive's copy shows its upstream requests too. Strings in all four languages. docs/subsystems/gui-shell.md describes it. Also changed by SecretHeader: the request archive no longer masks a rate-limit count header (…-tokens-remaining), which the old name rule took for a token. (provider.SecretHeader, which backups use, is another function and unchanged.) Tests: TestRecentCallHeadersBothWays (a fake vendor; the client's key and cookie, magpie's upstream key and the vendor's Set-Cookie all masked; status, protocol and response headers kept), TestRecentCallHeadersEachTry (a 429 then the next key, both kept in order), TestPluginHandOffHeaders, TestScrubURL, TestSecretHeaderRealNames (real header names both ways), TestArchiveOnThisComputer and TestRecentCallBodiesOnDemand extended, call-headers.test.cjs (Gateway, Routing and the archive's copy; chromium and webkit; en at 1100px, zh, zh-TW, ja, de at 380px and Routing at 440; nothing out of the box, no left-border accent). Run (on mote, macOS 27.2, through mote-run, unless named): - Full suite at ebd22b1b4 (the same change on5daba8337): gofmt -l empty, GOOS=windows go vet ./... ok, GOOS=linux go vet -tags nogui ./... ok, go test -count=1 -timeout 45m ./...: every package ok but four tests, none in this change's code, each filed: TestEveryRouteHasACommandOrIsNamed (internal/cli, /api/usage/checkin-account frome87140242, #4826), TestCodexPoolSaysTheClientsVersion (#4807: another test's Codex identity, passes alone, 3 of 3 here), TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27), TestClosingWindowMarksPageOutOfSight (passes alone; filed #4924). - Rebased ona3768cdd1(it changed internal/gateway, routing/client.js and i18n.js): gofmt, go vet, Linux (nogui) and Windows vet of internal/... ok; go test of internal/gateway, gui, redact, provider: ok but TestCodexPoolSaysTheClientsVersion (#4807, ok 3 of 3 alone with this change's gateway tests) and TestMainWindowCorners (#3949); TestCoreScriptBudget ok. GUI: call-headers, routing-bodies, recent-call-bodies, request-archive and i18n-keys, 58 of 58. - Rebased again on2c90922b0(app.js and internal/provider, no file of this change): TestCoreScriptBudget and TestRecentCall* ok; i18n-keys plus call-headers 24 of 24 three times out of four at load ~130 on mote. The first try had 1 red of 24 whose name was lost (its output was grepped, not kept); it did not come back in 3 runs. - At the pushed head, rebased on898c314b7, then3e61d814eand8d4d8b2d5(gateway search, redact; vet, internal/redact and the header tests again ok) (app.css, app.js, i18n.js, gui-shell.md and internal/gateway changed): gofmt, vet of gateway and gui ok; TestCoreScriptBudget, TestRecentCall*, TestRouteCall, TestPluginHandOffHeaders, TestArchiveOnThisComputer, TestScrubURL ok; i18n-keys, call-headers, routing-bodies, recent-call-bodies 54 of 54. - call-headers.test.cjs 20 of 20 (chromium and webkit; en at 1100, zh, zh-TW, ja, de at 380; Routing at 440), screenshots looked over. - GUI: not the whole suite. mote had three other sessions' full GUI suites running and the Linux box one, so 131 files the change can reach (gateway*, routing bodies/log/steady/story/time, recent*, call-*, request-archive, masking*, usage*, i18n*, gui-*, account-eye) ran on the Windows box: 1739 of 1862. Every red file was run again at5daba8337on the same box: routing-bodies, gateway-card, gateway-systemone, masking-demo, usage-ledger's Requests and the rest are red there too (Windows WebKit and Windows-only), and account-eye, routing-log-icons, usage-ledger-leave and -probe pass alone at this change. call-headers' 4 webkit Routing reds on Windows are routing-bodies' own (".rt-day" never visible in Windows WebKit). Broken by hand, each then put back: - ScrubHeader back on the name rule: TestSecretHeaderRealNames red ("a count of tokens: [REDACTED]"). - rpm.go's upstreamBegins taken out: TestRecentCallHeadersBothWays, TestRecentCallHeadersEachTry, TestArchiveOnThisComputer and TestRecentCallBodiesOnDemand red. - scrubHeaders off the sent headers: TestPluginHandOffHeaders red with the plugin's key shown. - Gateway client.js's toggle taken out: the 5 chromium recent-call GUI tests red. Not tried: - The whole GUI suite (above), and the GUI on Linux. - A real account or the real app: only fakes. - What a plugin sends its vendor (made in Bun, not seen by magpie; the view says so), and headers Go's transport adds itself (Accept-Encoding, Content-Length). - magpie's own requests (System One's decision, titles, search) keep no headers; a gateway served by another magpie shows none. Asked by usrname1833 on Discord.
295 lines
9.4 KiB
Go
295 lines
9.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"net/http"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A provider's MaxRPM (coeo91 on Discord: OpenRouter's free models take 20
|
|
// requests a minute, which a limit at once of 1 doesn't keep to) is how
|
|
// many requests each of its keys or accounts — a candidate's who(), as for
|
|
// MaxConcurrency — sends the vendor in any 60 seconds. Every request that
|
|
// goes out counts: each try of the forward loop (a retry, the next key or
|
|
// account, a fallback), each request a try makes on the way (a second ask
|
|
// after the vendor refused a beta or a shape), and the gateway's own asks
|
|
// of the provider (count_tokens, System One's decisions, embeddings,
|
|
// drawing, video and search). One more waits until the oldest of the
|
|
// minute is a minute old: in the order they came, and never longer than
|
|
// rpmWait (2 minutes, or the provider's QueueWait when that is shorter).
|
|
// One that would wait longer is turned away at once with a 429 whose
|
|
// Retry-After is when it could go; one whose agent goes away while it
|
|
// waits gives its place back and is never sent.
|
|
//
|
|
// A try waits for room in the minute after it has its slot at once
|
|
// (concurrency.go), right before it is sent, so the time the minute counts
|
|
// is when it really goes. Like a full lane, a key or account with no room
|
|
// in the minute gives way to another of its provider's, or of its routing
|
|
// group, that has room now (laneMate).
|
|
//
|
|
// The count is kept as each request's time to go, past and to come: room
|
|
// is there when fewer than the limit fall in the last minute, and else the
|
|
// next goes a minute after the limit-th from the end. Times only grow, so
|
|
// the list stays in order; one taken back (its agent gone) only leaves
|
|
// more room. Nothing waits while holding rpms.mu.
|
|
|
|
// rpmWindow is the rolling window MaxRPM counts over; tests shorten it.
|
|
var rpmWindow = time.Minute
|
|
|
|
// rpmSlack is added to the window. A request is counted when it starts to
|
|
// go, but the vendor counts it when it arrives: the first on a connection
|
|
// arrives later than the next ones by its dial and TLS handshake, so with
|
|
// the bare window the vendor could see one more than the limit in its own
|
|
// minute (TestMaxRPMWaitsForRoomInTheMinute went red under CI load, and
|
|
// does every time when the first is held 150ms on its way).
|
|
var rpmSlack = time.Second
|
|
|
|
// rpmSpan is how long a request counted stays counted.
|
|
func rpmSpan() time.Duration { return rpmWindow + rpmSlack }
|
|
|
|
// rpmLongest is the longest a request waits for room in the minute when
|
|
// the provider's QueueWait isn't shorter; tests shorten it.
|
|
var rpmLongest = 2 * time.Minute
|
|
|
|
// rpmNow is the clock the minute is read on, and rpmTimer the timer a
|
|
// request waits for room on; tests hold both, so what they check doesn't
|
|
// rest on how fast a loaded machine gets to it (#1791).
|
|
var (
|
|
rpmNow = time.Now
|
|
rpmTimer = func(d time.Duration) (<-chan time.Time, func() bool) {
|
|
t := time.NewTimer(d)
|
|
return t.C, t.Stop
|
|
}
|
|
)
|
|
|
|
// rpms are the times of the requests of each key or account with a MaxRPM.
|
|
type rpms struct {
|
|
mu sync.Mutex
|
|
m map[string][]time.Time
|
|
}
|
|
|
|
// errRPM turns a request away that would wait longer than it may for room
|
|
// in the minute: after is how long until it could go.
|
|
type errRPM struct {
|
|
limit int
|
|
after time.Duration
|
|
wait time.Duration
|
|
}
|
|
|
|
func (e *errRPM) Error() string {
|
|
return fmt.Sprintf("%d requests a minute is its limit, and the next could go in %.0fs, longer than a request waits (%.0fs)", e.limit, math.Ceil(e.after.Seconds()), e.wait.Seconds())
|
|
}
|
|
|
|
// retryAfter is e's Retry-After, in whole seconds, at least 1.
|
|
func (e *errRPM) retryAfter() string {
|
|
return fmt.Sprint(max(int(math.Ceil(e.after.Seconds())), 1))
|
|
}
|
|
|
|
// rpmWait is how long a request to p waits for room in the minute at most.
|
|
func rpmWait(p provider.Provider) time.Duration {
|
|
if w := time.Duration(p.QueueWait) * time.Second; w > 0 && w < rpmLongest {
|
|
return w
|
|
}
|
|
return rpmLongest
|
|
}
|
|
|
|
// prune drops who's times a minute (and rpmSlack) old or older, at now.
|
|
func (l *rpms) prune(who string, now time.Time) []time.Time {
|
|
ts := l.m[who]
|
|
i := 0
|
|
for i < len(ts) && !ts[i].After(now.Add(-rpmSpan())) {
|
|
i++
|
|
}
|
|
ts = ts[i:]
|
|
if len(ts) == 0 {
|
|
delete(l.m, who)
|
|
return nil
|
|
}
|
|
l.m[who] = ts
|
|
return ts
|
|
}
|
|
|
|
// reserve takes who's next time to go, at most longest from now (0: no
|
|
// bound): now when the minute has room, else a minute (and rpmSlack) after
|
|
// the limit-th from the end.
|
|
func (l *rpms) reserve(who string, limit int, longest time.Duration, now time.Time) (time.Time, error) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
if l.m == nil {
|
|
l.m = map[string][]time.Time{}
|
|
}
|
|
ts := l.prune(who, now)
|
|
at := now
|
|
if len(ts) >= limit {
|
|
at = ts[len(ts)-limit].Add(rpmSpan())
|
|
}
|
|
if d := at.Sub(now); longest > 0 && d > longest {
|
|
return time.Time{}, &errRPM{limit: limit, after: d, wait: longest}
|
|
}
|
|
l.m[who] = append(ts, at)
|
|
return at, nil
|
|
}
|
|
|
|
// giveBack takes back a time to go that wasn't used, its agent gone.
|
|
func (l *rpms) giveBack(who string, at time.Time) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
ts := l.m[who]
|
|
for i := len(ts) - 1; i >= 0; i-- {
|
|
if ts[i].Equal(at) {
|
|
l.m[who] = append(ts[:i:i], ts[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
if len(l.m[who]) == 0 {
|
|
delete(l.m, who)
|
|
}
|
|
}
|
|
|
|
// wait waits for room in who's minute and counts the request in it. It
|
|
// answers how long it waited, and an *errRPM, with nothing counted, when
|
|
// the wait would be longer than longest, or ctx's error when ctx ended
|
|
// first, its time given back. A limit of 0 counts nothing.
|
|
func (l *rpms) wait(ctx context.Context, who string, limit int, longest time.Duration) (time.Duration, error) {
|
|
if limit <= 0 {
|
|
return 0, nil
|
|
}
|
|
now := rpmNow()
|
|
at, err := l.reserve(who, limit, longest, now)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
d := at.Sub(now)
|
|
if d <= 0 {
|
|
return 0, nil
|
|
}
|
|
defer notePhase(ctx, "rpm")()
|
|
c, stop := rpmTimer(d)
|
|
defer stop()
|
|
select {
|
|
case <-c:
|
|
return d, nil
|
|
case <-ctx.Done():
|
|
// the wait ends before its place is given back, so once the place
|
|
// is back nothing is left to send it
|
|
stop()
|
|
l.giveBack(who, at)
|
|
return rpmNow().Sub(now), ctx.Err()
|
|
}
|
|
}
|
|
|
|
// free says whether a request for who would go at once: no limit, or room
|
|
// in its minute.
|
|
func (l *rpms) free(who string, limit int) bool {
|
|
if limit <= 0 {
|
|
return true
|
|
}
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
if l.m == nil {
|
|
return true
|
|
}
|
|
return len(l.prune(who, rpmNow())) < limit
|
|
}
|
|
|
|
// used is how many requests of who's are counted in its minute now, to go
|
|
// or gone.
|
|
func (l *rpms) used(who string) int {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
if l.m == nil {
|
|
return 0
|
|
}
|
|
return len(l.prune(who, rpmNow()))
|
|
}
|
|
|
|
// rpmTicket, on a request's context, counts each request made with it
|
|
// against who's MaxRPM as it goes out (rpmTransport), waiting at most
|
|
// longest (0: no bound). paid is a place the forward loop took already,
|
|
// which the first request uses; try marks the forward loop's ticket for
|
|
// provider id's try.
|
|
type rpmTicket struct {
|
|
l *rpms
|
|
id string
|
|
try bool
|
|
who string
|
|
limit int
|
|
longest time.Duration
|
|
paid atomic.Bool
|
|
}
|
|
|
|
type rpmKey struct{}
|
|
|
|
// meterWho is p's key or account as its limits count it: candidate.who for a
|
|
// provider not planned as a candidate.
|
|
func meterWho(p provider.Provider) string {
|
|
switch {
|
|
case p.Account != nil && p.Account.User != "":
|
|
return candidate{p: p}.restKey()
|
|
case p.Account == nil && p.Key != "":
|
|
return candidate{p: p}.who()
|
|
}
|
|
return p.ID
|
|
}
|
|
|
|
// metered is ctx with requests made with it counted against p's MaxRPM
|
|
// for who, "" for p's own key or account. One counted for who already is
|
|
// kept, and so is the forward loop's for a try of p: a try's own requests
|
|
// are counted against the key or account the try is of.
|
|
func (s *Server) metered(ctx context.Context, p provider.Provider, who string) context.Context {
|
|
limit := p.RPMLimit()
|
|
if limit <= 0 {
|
|
return ctx
|
|
}
|
|
if who == "" {
|
|
who = meterWho(p)
|
|
}
|
|
if t, ok := ctx.Value(rpmKey{}).(*rpmTicket); ok && (t.who == who || t.try && t.id == p.ID) {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, rpmKey{}, &rpmTicket{l: &s.rpms, who: who, limit: limit, longest: rpmWait(p)})
|
|
}
|
|
|
|
// paidFor is ctx whose first request to who is counted already: the
|
|
// forward loop waited for it. The ones the try makes after it (a second
|
|
// ask after the vendor refused a beta or a shape) wait as long as it
|
|
// takes: the try is under way, and turned away it would rest its key or
|
|
// account for nothing. That is never long, the minute's places to come
|
|
// being bounded by rpmWait.
|
|
func (s *Server) paidFor(ctx context.Context, p provider.Provider, who string) context.Context {
|
|
t := &rpmTicket{l: &s.rpms, id: p.ID, try: true, who: who, limit: p.RPMLimit()}
|
|
t.paid.Store(true)
|
|
return context.WithValue(ctx, rpmKey{}, t)
|
|
}
|
|
|
|
// rpmTransport counts each request whose context has a ticket against its
|
|
// MaxRPM before it goes out, waiting for room in the minute.
|
|
type rpmTransport struct{ next http.RoundTripper }
|
|
|
|
func (t rpmTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
if tk, ok := r.Context().Value(rpmKey{}).(*rpmTicket); ok && !tk.paid.CompareAndSwap(true, false) {
|
|
if _, err := tk.l.wait(r.Context(), tk.who, tk.limit, tk.longest); err != nil {
|
|
if r.Body != nil {
|
|
r.Body.Close()
|
|
}
|
|
var e *errRPM
|
|
if errors.As(err, &e) {
|
|
return nil, fmt.Errorf("%s: %w", tk.who, err)
|
|
}
|
|
return nil, err
|
|
}
|
|
}
|
|
// its headers, both ways, for the call it is for (wire_headers.go)
|
|
done := upstreamBegins(r, "")
|
|
res, err := t.next.RoundTrip(r)
|
|
done(res, err)
|
|
return res, err
|
|
}
|