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.2 KiB
Go
43 lines
1.2 KiB
Go
package gui
|
|
|
|
import (
|
|
"html"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
webPageStart = regexp.MustCompile(`(?i)<(!doctype|html|head|body)\b`)
|
|
webPageTitle = regexp.MustCompile(`(?is)<title[^>]*>(.*?)</title>`)
|
|
)
|
|
|
|
// webPageSaid is a proxy's own web page inside an error text of magpie's:
|
|
// a request magpie made to a remote magpie or a vendor came back as
|
|
// Cloudflare's 524 page, and the Gateway page showed "<!DOCTYPE html>
|
|
// <!--[if lt IE 7]>…" (aiandcloud on X). It returns the text before the
|
|
// page and the page's title, as one short line, and false for a text with
|
|
// no page in it.
|
|
func webPageSaid(msg string) (string, bool) {
|
|
at := webPageStart.FindStringIndex(msg)
|
|
if at == nil {
|
|
return "", false
|
|
}
|
|
before := strings.TrimRight(strings.TrimSpace(msg[:at[0]]), "::—- ")
|
|
title := ""
|
|
if m := webPageTitle.FindStringSubmatch(msg[at[0]:]); m != nil {
|
|
title = strings.Join(strings.Fields(html.UnescapeString(m[1])), " ")
|
|
}
|
|
if len([]rune(before)) > 160 {
|
|
before = string([]rune(before)[:160])
|
|
}
|
|
switch {
|
|
case before != "" && title != "":
|
|
return before + ": " + title, true
|
|
case before != "":
|
|
return before, true
|
|
case title != "":
|
|
return title, true
|
|
}
|
|
return "a web page", true
|
|
}
|