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.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1961,7 +1961,7 @@ func passing(status int, header http.Header, body []byte, again int) (time.Durat
|
||||
switch {
|
||||
case wait > longestPause:
|
||||
return 0, false
|
||||
case status == 408, status == 500, status == 502, status == 503, status == 504, status == 529:
|
||||
case busyStatus(status):
|
||||
if bytes.Contains(body, []byte(didNotStream)) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
@@ -65,16 +65,32 @@ type busyTry struct {
|
||||
// 502, or an upstream that answered a stream request with a whole reply
|
||||
// (didNotStream) isn't: each fails the same however often it is asked.
|
||||
func busy(status int, body []byte) bool {
|
||||
switch status {
|
||||
case 408, 500, 502, 503, 504, 529:
|
||||
switch {
|
||||
case busyStatus(status):
|
||||
return failure(status, body) == failOther && !protectionRefused(status, body) &&
|
||||
!bytes.Contains(body, []byte(didNotStream))
|
||||
case 429:
|
||||
case status == 429:
|
||||
return failure(status, body) == failRate && !creditWords.Match(wordsOf(body)) && !walled(body)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// busyStatus says whether an error status is one a vendor or the relay in
|
||||
// front of it gives for a moment's trouble: a timeout, an internal error,
|
||||
// a gateway or the service unavailable, Anthropic's 529 overloaded, and
|
||||
// Cloudflare's 520 to 524 (the origin gave an unknown error, is down,
|
||||
// timed out, unreachable, or slow to answer), which a relay behind
|
||||
// Cloudflare answers with now and then (vdaoren on Discord: an API-key
|
||||
// provider's 4xx/5xx went straight to the agent). The same-one retries
|
||||
// (passing) and the patience's rounds (busy) both read it.
|
||||
func busyStatus(status int) bool {
|
||||
switch status {
|
||||
case 408, 500, 502, 503, 504, 529, 520, 521, 522, 523, 524:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// noteBusy adds c to the members to ask again, once, with when its vendor
|
||||
// said it may be asked.
|
||||
func noteBusy(list []busyTry, c candidate, header http.Header) []busyTry {
|
||||
|
||||
@@ -2,6 +2,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -313,6 +314,72 @@ func TestLastOneLeftIsTriedAgain(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A relay behind Cloudflare that answers with one of Cloudflare's own
|
||||
// errors now and then (vdaoren on Discord: an API-key provider's 5xx
|
||||
// reached the agent) is asked again as for a 502 or 503, before anything
|
||||
// is sent to the agent: 520 to 524, as Cloudflare words them to an API
|
||||
// client and to a browser. A sign-in refused or a request the vendor
|
||||
// turns away is still the agent's after one try.
|
||||
func TestCloudflareErrorIsTriedAgain(t *testing.T) {
|
||||
page := func(code int, what string) string {
|
||||
return fmt.Sprintf(`<!DOCTYPE html>
|
||||
<html class="no-js" lang="en-US"> <head>
|
||||
<title>relay.example.com | %d: %s</title>
|
||||
<meta charset="UTF-8" />
|
||||
</head><body><div id="cf-wrapper"><h1><span class="inline-block">%s</span> <span class="code-label">Error code %d</span></h1>
|
||||
<span>Cloudflare Ray ID: <strong class="font-semibold">8f1e2d3c4b5a6978</strong></span></div></body></html>`, code, what, what, code)
|
||||
}
|
||||
for _, x := range []struct {
|
||||
name string
|
||||
first reply
|
||||
}{
|
||||
{"520 plain", reply{520, "text/plain; charset=UTF-8", "error code: 520"}},
|
||||
{"521 plain", reply{521, "text/plain; charset=UTF-8", "error code: 521"}},
|
||||
{"522 page", reply{522, "text/html; charset=UTF-8", page(522, "Connection timed out")}},
|
||||
{"523 plain", reply{523, "text/plain; charset=UTF-8", "error code: 523"}},
|
||||
{"524 page", reply{524, "text/html; charset=UTF-8", page(524, "A timeout occurred")}},
|
||||
} {
|
||||
fresh(t)
|
||||
a := &scripted{replies: []reply{x.first, x.first, {200, "", chatOK}}}
|
||||
scriptedOn(t, "a", provider.Chat, a)
|
||||
s := New()
|
||||
code, body := postAs(t, s, "", `{"model":"a/m","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 || !strings.Contains(body, "hello") || a.n != 3 {
|
||||
t.Errorf("%s: %d after %d tries: %s", x.name, code, a.n, body)
|
||||
continue
|
||||
}
|
||||
r := s.trace.routes[len(s.trace.routes)-1]
|
||||
if len(r.Tries) != 3 || r.Tries[0].Again == 0 || r.Tries[1].Again == 0 {
|
||||
t.Errorf("%s: tries %+v", x.name, r.Tries)
|
||||
}
|
||||
}
|
||||
|
||||
// a stream: the agent is sent nothing of the failed tries, only the
|
||||
// reply that came
|
||||
fresh(t)
|
||||
good := "data: {\"id\":\"ok\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"hello\"},\"finish_reason\":null}]}\n\n" +
|
||||
"data: {\"id\":\"ok\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n"
|
||||
a := &scripted{replies: []reply{{524, "text/html; charset=UTF-8", page(524, "A timeout occurred")}, {200, "text/event-stream", good}}}
|
||||
scriptedOn(t, "a", provider.Chat, a)
|
||||
code, body := postAs(t, New(), "", `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 || !strings.Contains(body, "hello") || strings.Contains(body, "524") || strings.Contains(body, "timeout") || a.n != 2 {
|
||||
t.Errorf("stream: %d after %d tries: %s", code, a.n, body)
|
||||
}
|
||||
|
||||
// what fails the same however often it is asked is the agent's at once
|
||||
for _, x := range []reply{
|
||||
{401, "", `{"error":{"message":"Invalid API key","type":"invalid_request_error","code":"invalid_api_key"}}`},
|
||||
{400, "", `{"error":{"message":"Your request was rejected as a result of our safety system.","type":"invalid_request_error","code":"content_policy_violation"}}`},
|
||||
} {
|
||||
fresh(t)
|
||||
a := &scripted{replies: []reply{x, {200, "", chatOK}}}
|
||||
scriptedOn(t, "a", provider.Chat, a)
|
||||
if code, body := postAs(t, New(), "", `{"model":"a/m","messages":[{"role":"user","content":"hi"}]}`); code != x.code || a.n != 1 {
|
||||
t.Errorf("%d: %d after %d tries: %s", x.code, code, a.n, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A rate limit on the last one left is waited out, a moment, whether the
|
||||
// vendor says how long or not (#503: a relay's 429 with no Retry-After went
|
||||
// straight to the agent); one that won't clear in seconds — a plan used up,
|
||||
|
||||
Reference in New Issue
Block a user