provider, gateway: the magpie serving the gateway fetches the partner list every hour, asked for or not

yetone: 改成后台每小时定时请求一次. Until now the list was fetched again
only when the Add sheet or the TUI asked for it, so an app left open on
another page never learned of a partner added.

KeepPartnersFresh runs beside the gateway's other Keep* loops: it
fetches when the gateway starts, then whenever partnerNext is due (an
hour, ten minutes after a failed fetch). It shares Partners'
one-fetch-at-a-time flag, so it never runs beside a fetch a page
started; while one runs it looks again a second later. Partners and
PartnersNow keep their own due check for a magpie that doesn't serve
the gateway (the CLI, a TUI beside a running app). MAGPIE_PARTNERS=off,
and every test (partnersSource), still asks nobody.

Run at this tree (base d3f3df746):
- go build ./..., GOOS=linux and GOOS=windows go vet of internal/provider
  and internal/gateway, gofmt -l clean.
- go test ./internal/provider -count=1: ok (94s).
- go test ./internal/provider -run Partner -count=3 -race: ok.
- Broken by hand: KeepPartnersFresh returning at once makes
  TestPartnersKeptFreshUnasked fail ("fetched on start").
Not tried: a real app left running for an hour against usemagpie.ai;
the internal/gateway suite (only a goroutine added to ListenAndServe).
This commit is contained in:
yetone
2026-10-23 21:45:32 +08:00
parent d3f3df746e
commit 7e0c031059
3 changed files with 91 additions and 5 deletions
+2
View File
@@ -597,6 +597,8 @@ func (s *Server) ListenAndServe(ctx context.Context) error {
go provider.KeepPluginsCheckedIn(ctx)
// and moves the built-in subscriptions being retired onto their plugins
go provider.KeepRetiringMoved(ctx)
// and fetches the partner list every hour, asked for or not
go provider.KeepPartnersFresh(ctx)
go provider.KeepPluginsListed(ctx) // models a plugin's vendor drops or adds, while magpie runs
// and fetches the lists of the providers the user asked to keep fresh (#2135)
go provider.KeepListsFresh(ctx)
+40 -5
View File
@@ -85,11 +85,10 @@ var (
// A list is fetched again after partnerEvery, so a partner added reaches
// an app left running within the hour; the list is a couple of KB and its
// icons aren't fetched again (partnerIcon).
const (
partnerEvery = time.Hour
partnerRetry = 10 * time.Minute
)
// icons aren't fetched again (partnerIcon). A var, so a test can shorten it.
var partnerEvery = time.Hour
const partnerRetry = 10 * time.Minute
var partnerID = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{1,40}$`)
@@ -158,6 +157,42 @@ func PartnersNow(wait time.Duration) []Partner {
return Partners()
}
// KeepPartnersFresh fetches the partner list when the magpie serving the
// gateway starts, and again whenever it is due (every hour, ten minutes
// after a failed fetch), whether or not a page asks for it, so a partner
// added reaches an app left running within the hour, and the list kept on
// disk is fresh for the next start.
func KeepPartnersFresh(ctx context.Context) {
if partnersSource() == "off" {
return
}
t := time.NewTimer(0)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
partnerMu.Lock()
loadPartners()
due := !partnerFetches && !time.Now().Before(partnerNext)
if due {
partnerFetches = true
}
partnerMu.Unlock()
if due {
refreshPartners()
}
partnerMu.Lock()
wait := time.Until(partnerNext)
partnerMu.Unlock()
// a fetch Partners started is still running when partnerNext
// has passed: look again in a second
t.Reset(max(wait, time.Second))
}
}
// loadPartners reads the list kept on disk the first time it's asked for;
// partnerMu held.
func loadPartners() {
+49
View File
@@ -392,3 +392,52 @@ func TestNoticePartners(t *testing.T) {
t.Fatalf("after a fetch: acme %v gamma %v", PartnerNoticed("acme"), PartnerNoticed("gamma"))
}
}
// The magpie serving the gateway fetches the list when it starts and
// again when it is due, with no page asking: an app left open on another
// page still learns of a partner added within the hour.
func TestPartnersKeptFreshUnasked(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", t.TempDir())
resetPartners()
t.Cleanup(resetPartners)
was := partnerEvery
partnerEvery = 50 * time.Millisecond
t.Cleanup(func() { partnerEvery = was })
var asked atomic.Int32
var body atomic.Value
body.Store(`{"partners":[]}`)
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
asked.Add(1)
_, _ = rw.Write([]byte(body.Load().(string)))
}))
t.Cleanup(srv.Close)
t.Setenv("MAGPIE_PARTNERS", srv.URL)
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { defer close(done); KeepPartnersFresh(ctx) }()
t.Cleanup(func() { cancel(); <-done })
waitFor := func(what string, ok func() bool) {
t.Helper()
deadline := time.Now().Add(20 * time.Second)
for !ok() {
if time.Now().After(deadline) {
t.Fatalf("%s: asked %d times", what, asked.Load())
}
time.Sleep(10 * time.Millisecond)
}
}
waitFor("fetched on start", func() bool { return asked.Load() >= 1 })
body.Store(`{"partners":[{"id":"acme","name":"Acme","chat":"https://a.example/v1"}]}`)
n := asked.Load()
waitFor("fetched again when due", func() bool { return asked.Load() > n })
// the fetch counted may still be storing its list
claimPartnerFetch()
partnerMu.Unlock()
waitFor("the partner added is held", func() bool {
partnerMu.Lock()
defer partnerMu.Unlock()
return partnerState != nil && partnerIDs(partnerState.Partners) == "acme"
})
}