Files
magpie/internal/gateway/models_wait.go
T
yetone 06c4582cca remote magpie: a list changed on the other magpie reaches this one's agents at once
KaCli (Discord) runs two magpies: the server's is the gateway, and the
local one has it as a Remote magpie and writes its models into dsh. A
route or group changed on the server reached the local dsh only on the
next 10-minute fetch (#2135's RemoteMagpieEvery). The server knows when
its list changes (filememo), so it now says so.

- Server: GET /v1/models with X-Magpie-List-Wait: <secs> and
  If-None-Match answers the list with an ETag once it differs from that
  tag. It answers 304 at the end of the wait (at most 50s, under a
  proxy's 60s) or as the gateway stops. The list is looked at again on
  each magpie write and on each change another process makes to its
  files (filememo.Written). It is the list's own route, so the key,
  lanGuard, corsGuard and a key's models judge it as before. The wait is
  exempt from holdCatalog, and each look holds the catalog for itself.
  Without the header the answer is the same as before, with no ETag.
- Client: KeepRemoteListsFollowed (started beside KeepListsFresh) keeps
  one long-poll per Remote magpie. On a new tag it fetches the list
  through the same RefetchTaking, so catalog.Touched rewrites the
  agents' configs. A magpie from before answers at once with no ETag; it
  is left to the 10m schedule and asked again after an hour. A Remote
  magpie whose user set its list to "off" is not followed. The schedule
  stays as the fallback.

Run (on mote, through mote-run):
- gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; go build ./..., GOOS=linux -tags nogui and
  GOOS=windows builds: all ok.
- go test -tags nogui -count=1 ./internal/gateway ./internal/provider
  ./internal/catalog ./internal/filememo: all ok (gateway 863s, provider 152s, catalog, filememo).
- After the rebase on 650bf8412 (11 files changed under gateway and
  provider since): go vet of both, go build ./..., and -run
  "ModelsWait|RemoteMagpie|ListsFresh|HoldCatalog|Models" of both: ok.
- Two real magpies (this build, `serve`, sandbox HOMEs, 127.0.0.1:3561
  server and :3562 local, a gateway key, dsh profile web wired to
  office/up/m1). `magpie group add kacli-fast` on the server put
  office/group/kacli-fast in the local cordis.patch.yml in 1.14s
  (log: "office: its list changed there, fetched again: 3 models"). 130s
  idle after it: no further fetch.
  Control: origin/main on both, nothing in 37s. origin/main server, this
  local: nothing in 37s, logged once "doesn't wait ... fetched on its
  schedule", asked once.

Broken by hand, each red:
- Without the dispatch to the wait: TestModelsWaitTagged,
  AnswersAChange, EndsWithTheGateway.
- Without the filememo.Written wake: TestModelsWaitAnswersAChange
  (answered after 5.0s, at the others timer).
- Without RefetchTaking in the follower: TestRemoteMagpieListFollowed.
- Without the RefreshInterval > 0 check: TestRemoteMagpieOffNotFollowed
  (asked 3 times).
- Removing the holdCatalog exemption is caught by no test: a held
  catalog is still dropped on a write (heldFreshLocked), so the change is
  seen. It is kept so a 50s hold doesn't pin the build, as with a
  WebSocket.

Not tried: across two real computers on a LAN, or behind a reverse
proxy (only loopback with a key, and the LAN/no-key, foreign Origin and
Origin null refusals in TestModelsWaitGuardedAsTheList). Not tried on
Windows or Linux. Agents other than dsh weren't checked, though they go
through the same catalog.Touched sync.

Reported-by: KaCli (Discord)
2026-11-06 20:57:55 +08:00

152 lines
4.6 KiB
Go

package gateway
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"net/http"
"strconv"
"strings"
"time"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/provider"
)
// Another magpie that has this one as its provider (remote-magpie) waits
// here for this magpie's model list to change, and fetches it again when
// it has (KaCli on Discord: a route or group changed on the server reached
// the computer using it only on its next 10-minute fetch, and its agents'
// configs after that). It is the list's own route, GET /v1/models, so
// every guard a list asked for passes (the key, the origin, the network
// share, a key's models) holds for the wait as well, with
// provider.ListWaitHeader saying how many seconds to wait at most and
// If-None-Match the list it has (the ETag of its last answer). The list
// is answered, with its ETag, once it is other than that one; at the end
// of the wait, and as this gateway stops, 304 says it is the same. A list
// is looked at again on each write magpie makes and on each change
// another process makes to its files (filememo.Written), not polled.
// A client asking without the header is answered as before, no ETag.
// listWaitMost is the longest a wait for the list is held: under the
// minute a proxy in front of a gateway (nginx's proxy_read_timeout) lets
// an answer take.
const listWaitMost = 50 * time.Second
// listSettle is how long a write is given for the ones that come with it
// (a group saved, then the catalog touched) before the list is looked at.
var listSettle = 200 * time.Millisecond
// waitsForList reports whether r is another magpie waiting for the list to
// change.
func waitsForList(r *http.Request) bool {
if r.Method != http.MethodGet || r.Header.Get(provider.ListWaitHeader) == "" {
return false
}
return r.URL.Path == "/v1/models" || r.URL.Path == "/models"
}
// listWaitOf is how long r asks to wait, at most listWaitMost.
func listWaitOf(r *http.Request) time.Duration {
n, err := strconv.Atoi(strings.TrimSpace(r.Header.Get(provider.ListWaitHeader)))
if err != nil || n <= 0 {
return 0
}
return min(time.Duration(n)*time.Second, listWaitMost)
}
// listTag is a list answer's ETag: of the bytes answered, so it moves
// exactly when what another magpie would fetch does.
func listTag(status int, body []byte) string {
h := sha256.New()
h.Write([]byte(strconv.Itoa(status)))
h.Write(body)
return `"ml-` + hex.EncodeToString(h.Sum(nil)[:12]) + `"`
}
// listAnswer is the list as r would be answered it now.
type listAnswer struct {
header http.Header
status int
body bytes.Buffer
}
func (a *listAnswer) Header() http.Header { return a.header }
func (a *listAnswer) WriteHeader(code int) {
if a.status == 0 {
a.status = code
}
}
func (a *listAnswer) Write(b []byte) (int, error) {
a.WriteHeader(http.StatusOK)
return a.body.Write(b)
}
// lookAtList answers r's list into a listAnswer, holding the catalog and
// files it reads for the one look, as holdCatalog does for a request.
func (s *Server) lookAtList(r *http.Request) *listAnswer {
a := &listAnswer{header: http.Header{}}
release, held := provider.Hold(), filememo.Hold()
defer release()
defer held()
s.listModels(a, r)
if a.status == 0 {
a.status = http.StatusOK
}
return a
}
// modelsOnceChanged answers r's list once it is other than the one r's
// If-None-Match names, or 304 at the end of the wait.
func (s *Server) modelsOnceChanged(w http.ResponseWriter, r *http.Request) {
since := strings.TrimSpace(r.Header.Get("If-None-Match"))
end := time.NewTimer(listWaitOf(r))
defer end.Stop()
tag := ""
for {
// before the look, so a write made during it isn't missed
written := filememo.Written()
a := s.lookAtList(r)
tag = listTag(a.status, a.body.Bytes())
if a.status != http.StatusOK || tag != since {
for k, v := range a.header {
w.Header()[k] = v
}
if a.status == http.StatusOK {
w.Header().Set("ETag", tag)
}
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(a.status)
w.Write(a.body.Bytes())
return
}
// another process's writes are seen at once while filememo.Watch
// runs, and every few seconds where it can't
others := time.NewTimer(filememo.OthersEvery())
select {
case <-r.Context().Done():
others.Stop()
return
case <-s.streamsEnding():
case <-end.C:
case <-written:
others.Stop()
settle := time.NewTimer(listSettle)
select {
case <-r.Context().Done():
settle.Stop()
return
case <-settle.C:
}
continue
case <-others.C:
continue
}
others.Stop()
w.Header().Set("ETag", tag)
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(http.StatusNotModified)
return
}
}