usrname1833 on Discord: with the computer offline, a routing group's
members failed to connect one after another, each rested, and the first
stayed behind the others once the network was back.
A 502 whose error says no connection was made (dial, lookup, refused, no
route, TLS handshake timeout, and Bun's ConnectionRefused / ENOTFOUND
for a plugin's fetch) now asks whether this computer has a network,
unless any upstream answered in the last 15s: no interface up with a
global address means none; otherwise up to three hosts of the user's
other providers (not p's own, not proxied ones, not localhost / LAN) are
dialled at once (2s), and none taking the connection means offline. The
verdict is kept 5s and asked by one request at a time. Offline, the
member is passed over as a down proxy is (failProxy): no rest, no count,
the trace says Fail "offline". One provider that can't connect while
another's host takes a connection, or while replies came lately, still
rests as before.
Covered where every rest passes: restAfterMarked (so decide_group's and
count_tokens' rests too), the serve loop's next-member path, and the last
member's label. Routing page: "network down" tag and a story for the
next-member and last-member cases, in zh, zh-TW, ja and de.
Run (mote, at this tree):
- gofmt -l clean; go vet ./internal/gateway on darwin, GOOS=linux,
GOOS=windows: ok
- go test -run 'TestOfflineRestsNobody|TestProxyDownDoesNotRest' -v
./internal/gateway: ok; go test -timeout 45m ./internal/gateway:
ok (708s)
- GUI: routing-offline.test.cjs (new) 10/10 and routing-proxy 4/4,
Chromium + WebKit, en zh zh-TW ja de. Every routing-*.test.cjs at once
(load 30-44 on mote): 1997 pass, 29 fail. The untouched base 3d063e34f
gives 1987 pass, 29 fail in the same files, with the subtests shuffled
(#4585). i18n-keys: only "Unmarked" is red (#4524), as on the base.
Broken by hand: offlineFailure returning false -> TestOfflineRestsNobody
red ("nothing reachable" and "no interface up": Fail other, Rest set);
client.js at HEAD -> routing-offline 0/5 in Chromium.
Not tried: a real machine taken off the network; a plugin (Bun) fetch's
real offline error text (matched from Bun's error names only); providers
behind a proxy (left to failProxy and not dialled); a group whose
members all sit on the local network (near hosts aren't dialled, so
with an interface up their failures still rest); the full ./... suite.
141 lines
4.5 KiB
Go
141 lines
4.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// TestOfflineRestsNobody (usrname1833 on Discord): with this computer off
|
|
// the network, a routing group's members each failed to connect, each
|
|
// rested, and the first stayed behind the others once the network was
|
|
// back. A connection that fails while nothing can be reached rests nobody;
|
|
// one provider failing while another's host takes a connection, or
|
|
// while replies came in lately, still rests that one.
|
|
func TestOfflineRestsNobody(t *testing.T) {
|
|
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
|
|
t.Setenv(k, "")
|
|
}
|
|
wasNear, wasNet := nearHost, hasNetwork
|
|
t.Cleanup(func() {
|
|
nearHost, hasNetwork = wasNear, wasNet
|
|
forgetNetwork()
|
|
})
|
|
// the test's servers are all on loopback: they stand for the
|
|
// providers' hosts out on the network
|
|
nearHost = func(string) bool { return false }
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.ReadAll(r.Body)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"id":"c","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"from b"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
deadA := deadAddr(t, up.Listener.Addr().String())
|
|
deadB := deadAddr(t, up.Listener.Addr().String(), deadA)
|
|
|
|
resting := func() int {
|
|
restingUntil.Lock()
|
|
defer restingUntil.Unlock()
|
|
n := 0
|
|
for _, until := range restingUntil.m {
|
|
if until.After(time.Now()) {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
setup := func(t *testing.T, bURL string, netUp bool) *Server {
|
|
t.Helper()
|
|
fresh(t)
|
|
forgetNetwork()
|
|
hasNetwork = func() bool { return netUp }
|
|
for _, p := range []provider.Provider{
|
|
{ID: "a", Name: "A", Key: "ka", Models: []string{"m"}, Chat: "http://" + deadA + "/v1"},
|
|
{ID: "b", Name: "B", Key: "kb", Models: []string{"m"}, Chat: bURL},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "Pair", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return New()
|
|
}
|
|
ask := func(t *testing.T, s *Server) (int, string, *Route) {
|
|
t.Helper()
|
|
code, body := postAs(t, s, "", `{"model":"group/pair","messages":[{"role":"user","content":"hi"}]}`)
|
|
return code, body, s.trace.routes[len(s.trace.routes)-1]
|
|
}
|
|
|
|
t.Run("nothing reachable", func(t *testing.T) {
|
|
s := setup(t, "http://"+deadB+"/v1", true)
|
|
code, body, r := ask(t, s)
|
|
if code < 500 {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
if len(r.Tries) < 2 {
|
|
t.Fatalf("tries %+v", r.Tries)
|
|
}
|
|
if f := r.Tries[0]; f.Fail != failOffline || f.Rest != nil || !strings.Contains(f.Error, deadA) {
|
|
t.Fatalf("a's try %+v", f)
|
|
}
|
|
if f := r.Tries[len(r.Tries)-1]; f.Fail != failOffline || f.Rest != nil && f.Rest.Until.After(time.Now()) {
|
|
t.Fatalf("b's try %+v", f)
|
|
}
|
|
if n := resting(); n != 0 {
|
|
t.Fatalf("%d set aside while this computer was off the network", n)
|
|
}
|
|
})
|
|
|
|
t.Run("no interface up", func(t *testing.T) {
|
|
s := setup(t, up.URL+"/v1", false)
|
|
code, body, r := ask(t, s)
|
|
if code != 200 || !strings.Contains(body, "from b") {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
if f := r.Tries[0]; f.Fail != failOffline || f.Rest != nil {
|
|
t.Fatalf("a's try %+v", f)
|
|
}
|
|
if n := resting(); n != 0 {
|
|
t.Fatalf("%d set aside with no network", n)
|
|
}
|
|
})
|
|
|
|
t.Run("another provider reachable", func(t *testing.T) {
|
|
s := setup(t, up.URL+"/v1", true)
|
|
code, body, r := ask(t, s)
|
|
if code != 200 || !strings.Contains(body, "from b") {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
if f := r.Tries[0]; f.Fail == failOffline || f.Rest == nil || !f.Rest.Until.After(time.Now()) {
|
|
t.Fatalf("a's own failure didn't rest it: %+v", f)
|
|
}
|
|
if n := resting(); n != 1 {
|
|
t.Fatalf("%d resting, want a alone", n)
|
|
}
|
|
})
|
|
|
|
t.Run("answered lately", func(t *testing.T) {
|
|
s := setup(t, "http://"+deadB+"/v1", true)
|
|
heardNet()
|
|
_, _, r := ask(t, s)
|
|
if f := r.Tries[0]; f.Fail == failOffline || f.Rest == nil || !f.Rest.Until.After(time.Now()) {
|
|
t.Fatalf("a's failure after a reply came didn't rest it: %+v", f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// forgetNetwork forgets what was heard of the network and the verdict kept.
|
|
func forgetNetwork() {
|
|
lastHeard.Store(0)
|
|
offlineSeen.Lock()
|
|
offlineSeen.at, offlineSeen.off = time.Time{}, false
|
|
offlineSeen.Unlock()
|
|
}
|