aiandcloud (2.98B tokens, 12,654 requests a day, server mode behind a
proxy) saw Gateway keys draw a Cloudflare 524 page as raw HTML.
Slow list: every poll of /api/caller-keys read up to a month of ledger
rows once per window per key. budget.AllOf now reads each distinct window
once for all keys, and keeps the settled blocks' sums with a cursor
(usage.VisitFrom), so a poll only reads what was appended since; the
unfinished tail block is always read again. A rewritten or shorter log
resets the cursor. All(k) is AllOf of one key.
Numbers (BenchmarkAll vs BenchmarkReadEach, 8 keys x day/week/month,
31 days x 12000 rows/day = 372k rows, warm): 211-228 ms before, 23.5 ms
after.
Raw HTML: fail() now turns an error string holding a web page (a remote
magpie's or a vendor's 524/502 page inside the JSON error) into a short
said/what pair, "A proxy in front of magpie answered, not magpie ({what})"
with the page's title, translated in all 5 languages (key already in
i18n.js). The browser already handled non-JSON bodies (notJSON, #2032).
Run (Windows box, mote ssh was down earlier): go test budget, usage, gui
(AllOf, Budget, FailSays, I18n, Keys); gateway-keys-proxy-page and
gateway-key-limit GUI files in chromium and webkit, 5 languages: 24/24;
gofmt; vet windows, linux nogui, darwin (mote, only a clang deprecation
warning). Broken by hand: with the old fail() TestFailSaysAProxyPageByItsTitle
is red; with VisitFrom's reset() removed TestAllOfIsWhatEachWindowRead is red.
Not tried: a user-scale cold ledger, a server behind a real Cloudflare,
the full suite before the tag (started after).
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package usage
|
|
|
|
import "time"
|
|
|
|
// Cursor is where a reader stopped in the log: the full blocks it has
|
|
// read, which the log keeps as they are while it only grows. The zero
|
|
// Cursor starts at the beginning.
|
|
type Cursor struct {
|
|
path string
|
|
n int
|
|
last *rowChunk
|
|
}
|
|
|
|
// VisitFrom is Visit for a reader that keeps what it counted of the rows
|
|
// before cur: settled is called with each record of the full blocks after
|
|
// cur, which the reader keeps and the next call starts after; tail is
|
|
// called with the rows of the last, partial block, which it counts again
|
|
// each call. A cur the log has left behind (rewritten, cleared, another
|
|
// file) starts over: reset is called first, and settled gets every record. A
|
|
// reader of the Gateway page's key limits read a month of the log, row by
|
|
// row, for each poll.
|
|
func VisitFrom(since time.Time, cur Cursor, reset func(), settled, tail func(Record)) Cursor {
|
|
s := readLogSnapshot()
|
|
n := cur.n
|
|
full := len(s.blocks)
|
|
if full > 0 && s.blocks[full-1].Count < logBlockRows {
|
|
full--
|
|
}
|
|
if n > full || n > 0 && (cur.path != s.path || s.blocks[n-1] != cur.last) {
|
|
n = 0
|
|
reset()
|
|
}
|
|
visitBlocks(s.blocks[n:full], since, settled)
|
|
if full < len(s.blocks) {
|
|
visitBlocks(s.blocks[full:], since, tail)
|
|
}
|
|
next := Cursor{path: s.path, n: full}
|
|
if full > 0 {
|
|
next.last = s.blocks[full-1]
|
|
}
|
|
return next
|
|
}
|