Files
magpie/docs
yetone 7818dc316a gateway: a relay's Cloudflare 520 to 524 is asked again before the agent sees it, as a 502 or 503 is (vdaoren)
vdaoren on Discord: an API-key provider's 4xx/5xx now and then went
straight to the agent; they asked for retries on chosen status codes.

What a single API-key provider already did: the last one left is asked
again after a failure that passes (passing, fallback.go): 408, 500, 502,
503, 504 and 529 twice (lastRetries), a 429 that is a short rate limit
three times (rateRetries), pauses 1s, 2s, 4s or a Retry-After up to 8s,
then within the provider's patience (singlePatience, 60s by default,
`magpie provider set <id> patience=`) in rounds (busy, patience.go), all
while the reply is held and nothing has gone to the agent. A plan used
up, no balance, a 401, a request the vendor refuses for its content and a
conversation too long are the agent's at once.

What was missing: Cloudflare's own 520 (unknown error), 521 (origin
down), 522 (connection timed out), 523 (origin unreachable) and 524
(timeout), which a relay behind Cloudflare gives now and then, were in
neither list. Such a 5xx moved on to another member in a group, but on a
lone provider it reached the agent after one try. busyStatus now holds
the one list passing and busy read, with 520 to 524 added. No setting:
the default serves it, and patience= already exists for the length.

TestCloudflareErrorIsTriedAgain (retry_test.go): each of 520 to 524, as
Cloudflare words it to an API client ("error code: 520") and as its HTML
page, twice and then 200, is answered 200 after 3 tries; a stream's 524
then an SSE reply reaches the agent with nothing of the 524 in it; a 401
invalid key and a 400 content_policy_violation are tried once.
Broken by hand (busyStatus's list back to 408..529): red, every 52x case
"520 after 1 tries" and the stream "524 after 1 tries"; the 401/400 cases
pass either way.

Run on mote at this tree (base origin/main b61c71448):
- go test -tags nogui -count=1 -run 'TestCloudflareErrorIsTriedAgain|
  TestLastOneLeftIsTriedAgain|TestRateLimitIsWaitedOut|Patience|Overload|
  Busy' ./internal/gateway/: ok
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./...: ok (DARWIN-VET-OK, LINUX-VET-OK, WINDOWS-VET-OK)
- gofmt -l internal/gateway: nothing
- go test -tags nogui -timeout 45m ./...: not run before this push;
  two other sessions' full suites held mote, one suite per machine. It
  runs after the push, as agent-rules says, and a red is filed as an
  issue with its output.

Not tried: a real relay behind Cloudflare answering 52x (none at hand);
Windows/Linux runs of the tests (the change has no OS-specific code).
Docs: gateway-routing.md's busy list names busyStatus and 520 to 524.
2026-11-04 21:49:51 +08:00
..