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).
197 lines
6.4 KiB
Go
197 lines
6.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// What the ChatGPT backend says of a Codex account as it answers one of
|
|
// magpie's turns is kept as the account's usage (provider.NoteCodexLimits),
|
|
// so its usage cap holds it from the next turn on rather than once
|
|
// /wham/usage is read again (#1295). It says it as Codex 0.160.1 reads it
|
|
// (codex-api's rate-limit parsing, RateLimitEvent): headers
|
|
// x-codex-primary-used-percent, -window-minutes and -reset-at (and
|
|
// secondary-), and a codex.rate_limits event ahead of the reply,
|
|
// {"rate_limits":{"primary":{"used_percent","window_minutes","reset_at"},
|
|
// "secondary":…}}, with metered_limit_name naming a limit other than the
|
|
// account's own.
|
|
|
|
// noteCodexLimits is provider.NoteCodexLimits, which tests watch.
|
|
var noteCodexLimits = provider.NoteCodexLimits
|
|
|
|
// codexLimitsScan is as far into a reply its codex.rate_limits event is
|
|
// looked for: it comes ahead of the reply.
|
|
const codexLimitsScan = 64 << 10
|
|
|
|
// heardCodexLimits keeps what res says of p's Codex account.
|
|
func heardCodexLimits(p provider.Provider, res *http.Response) {
|
|
if p.Account == nil || p.Account.Agent != "codex" || res == nil {
|
|
return
|
|
}
|
|
agent, user := p.Account.UsageAgent(), p.Account.User
|
|
now := time.Now()
|
|
noteCodexAnswered(agent, user, now)
|
|
if ls := codexHeaderLimits(res.Header, now); len(ls) > 0 {
|
|
noteCodexLimits(agent, user, ls)
|
|
}
|
|
if res.Body != nil && res.StatusCode < 300 {
|
|
res.Body = &codexLimitsTap{ReadCloser: res.Body, note: func(ls []provider.CodexLimit) {
|
|
noteCodexLimits(agent, user, ls)
|
|
}}
|
|
}
|
|
}
|
|
|
|
// codexLearnAgain is how long a Codex account not known that answered is
|
|
// not put first again to be learned (learns): its reply told nothing of
|
|
// the week, or the reading still fails.
|
|
const codexLearnAgain = time.Hour
|
|
|
|
// codexAnswered is when each Codex account last answered, by
|
|
// agent/lower-cased user.
|
|
var codexAnswered = struct {
|
|
sync.Mutex
|
|
m map[string]time.Time
|
|
}{m: map[string]time.Time{}}
|
|
|
|
func noteCodexAnswered(agent, user string, now time.Time) {
|
|
codexAnswered.Lock()
|
|
codexAnswered.m[agent+"/"+strings.ToLower(user)] = now
|
|
codexAnswered.Unlock()
|
|
}
|
|
|
|
// codexAnsweredLately: user answered within codexLearnAgain of now.
|
|
func codexAnsweredLately(agent, user string, now time.Time) bool {
|
|
codexAnswered.Lock()
|
|
at, ok := codexAnswered.m[agent+"/"+strings.ToLower(user)]
|
|
codexAnswered.Unlock()
|
|
return ok && now.Sub(at) < codexLearnAgain
|
|
}
|
|
|
|
// codexHeaderLimits are the windows the x-codex-primary-* and
|
|
// x-codex-secondary-* headers tell; one that doesn't say how long it runs
|
|
// is left out, as no window read can be told to be it. None when
|
|
// x-codex-active-limit names the image allowance, whose window they then
|
|
// are (codexImageHeaderLimit).
|
|
func codexHeaderLimits(h http.Header, now time.Time) []provider.CodexLimit {
|
|
if imageLimitName(h.Get("x-codex-active-limit")) {
|
|
// the x-codex-* windows are the image allowance's (#6412)
|
|
return nil
|
|
}
|
|
return headerLimitsOf(h, "x-codex-", now)
|
|
}
|
|
|
|
// headerLimitsOf are the windows of the header family fam (x-codex-,
|
|
// x-imagegen-premium-) tells: its primary- and secondary- ones.
|
|
func headerLimitsOf(h http.Header, fam string, now time.Time) []provider.CodexLimit {
|
|
var out []provider.CodexLimit
|
|
for _, which := range []string{"primary", "secondary"} {
|
|
pre := fam + which + "-"
|
|
used, err := strconv.ParseFloat(strings.TrimSpace(h.Get(pre+"used-percent")), 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
mins, err := strconv.ParseInt(strings.TrimSpace(h.Get(pre+"window-minutes")), 10, 64)
|
|
if err != nil || mins <= 0 {
|
|
continue
|
|
}
|
|
l := provider.CodexLimit{Used: used, Span: time.Duration(mins) * time.Minute}
|
|
if at, err := strconv.ParseInt(strings.TrimSpace(h.Get(pre+"reset-at")), 10, 64); err == nil && at > 0 {
|
|
l.Resets = time.Unix(at, 0)
|
|
} else if after, err := strconv.ParseInt(strings.TrimSpace(h.Get(pre+"reset-after-seconds")), 10, 64); err == nil && after > 0 {
|
|
l.Resets = now.Add(time.Duration(after) * time.Second)
|
|
}
|
|
out = append(out, l)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// codexEventLimits are the windows a codex.rate_limits event's data tells,
|
|
// nil for any other event, and for one of a limit other than the
|
|
// account's own (metered_limit_name: a model's own allowance).
|
|
func codexEventLimits(data []byte, now time.Time) []provider.CodexLimit {
|
|
type window struct {
|
|
Used *float64 `json:"used_percent"`
|
|
Minutes int64 `json:"window_minutes"`
|
|
ResetAt int64 `json:"reset_at"`
|
|
ResetAfter int64 `json:"reset_after_seconds"`
|
|
}
|
|
var ev struct {
|
|
Type string `json:"type"`
|
|
Metered string `json:"metered_limit_name"`
|
|
RateLimits *struct {
|
|
Primary *window `json:"primary"`
|
|
Secondary *window `json:"secondary"`
|
|
} `json:"rate_limits"`
|
|
}
|
|
if json.Unmarshal(data, &ev) != nil || ev.Type != "codex.rate_limits" || ev.RateLimits == nil {
|
|
return nil
|
|
}
|
|
if m := strings.ReplaceAll(strings.ToLower(strings.TrimSpace(ev.Metered)), "-", "_"); m != "" && m != "codex" {
|
|
return nil
|
|
}
|
|
var out []provider.CodexLimit
|
|
for _, w := range []*window{ev.RateLimits.Primary, ev.RateLimits.Secondary} {
|
|
if w == nil || w.Used == nil || w.Minutes <= 0 {
|
|
continue
|
|
}
|
|
l := provider.CodexLimit{Used: *w.Used, Span: time.Duration(w.Minutes) * time.Minute}
|
|
if w.ResetAt > 0 {
|
|
l.Resets = time.Unix(w.ResetAt, 0)
|
|
} else if w.ResetAfter > 0 {
|
|
l.Resets = now.Add(time.Duration(w.ResetAfter) * time.Second)
|
|
}
|
|
out = append(out, l)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// codexLimitsTap passes a reply on as it comes, looking in its first
|
|
// codexLimitsScan bytes for a codex.rate_limits event.
|
|
type codexLimitsTap struct {
|
|
io.ReadCloser
|
|
note func([]provider.CodexLimit)
|
|
buf []byte
|
|
done bool
|
|
}
|
|
|
|
func (t *codexLimitsTap) Read(p []byte) (int, error) {
|
|
n, err := t.ReadCloser.Read(p)
|
|
if n > 0 && !t.done {
|
|
t.buf = append(t.buf, p[:n]...)
|
|
t.look()
|
|
}
|
|
return n, err
|
|
}
|
|
|
|
// look reads the whole lines seen so far for the event.
|
|
func (t *codexLimitsTap) look() {
|
|
for {
|
|
i := bytes.IndexByte(t.buf, '\n')
|
|
if i < 0 {
|
|
break
|
|
}
|
|
line := bytes.TrimSpace(t.buf[:i])
|
|
t.buf = t.buf[i+1:]
|
|
data, ok := bytes.CutPrefix(line, []byte("data:"))
|
|
if !ok || !bodyHas(data, `"codex.rate_limits"`) {
|
|
continue
|
|
}
|
|
if ls := codexEventLimits(bytes.TrimSpace(data), time.Now()); len(ls) > 0 {
|
|
t.note(ls)
|
|
}
|
|
t.done, t.buf = true, nil
|
|
return
|
|
}
|
|
if len(t.buf) > codexLimitsScan {
|
|
t.done, t.buf = true, nil
|
|
}
|
|
}
|