Hold dropped everything held when the last request let go, so one
request after another built the catalog again from every provider's
files and accounts (All, entries, the groups' entries, the settings):
a fifth to a third of a request's CPU with 30 providers. What is built
now stays until heldFor (1s) is past, as it already did for requests
that overlapped. It is built again at once when anything it was built
from may have moved:
- Changed() and a plugin change (dropHeld), as before;
- a write that only tells filememo (settings.Save, writePrivate, an
agent's account file): held values carry filememo.Gen(), new here, and
are dropped when it moved;
- providers.json or settings.json written by another process (the CLI's
"magpie group add"): the first Hold of a request stats both and drops
what is held when either changed, so the next request sees it, not a
second later;
- the groups served or gated (GateGroups, ServeGroups): holdGate drops
what is held on both changes (TestGroupsOnAndOff was red without it);
- another environment (a test's HOME), by a hash of os.Environ().
Other files another process writes (an agent's own sign-in file) are
seen within heldFor, as filememo already takes a file found unchanged.
Tests:
- TestHoldKeptForTheNextRequest: five requests one after another build
once. Broken by hand (held.built = nil on the last release put back):
red, "five requests one after another: built 5 times, want 1".
- TestHoldKeptUntilAWrite: each of filememo.Forget, providers.json and
settings.json written behind magpie's back, and GateGroups, builds
again once. Broken by hand, one at a time: the Gen check off, red
"filememo told: built 0 times, want 1"; the stat off, red
"providers.json written by another process: built 0 times, want 1";
holdGate's dropHeld off, red "the groups gated: built 0 times, want 1".
- TestRequestBuildsProvidersOnce now wants 1, 0, then 1 after
Changed() (a request later than 0.9s after the first may rightly
build again on a loaded machine; the test allows it).
BenchmarkGroupOverhead (internal/gateway, 200KB Anthropic body,
stream=false, 100x, two rounds alternating; load average ~450, so the
cpu-ms/op it reports is the measure, not wall ns/op). base is 8215e8275:
12x20 p0/m1 base 25.66 / 24.36 cpu-ms/op 16.8MB 41723-41827 allocs
this 22.13 / 20.67 cpu-ms/op 13.4MB 33771-33969 allocs
12x20 g4 base 30.59 / 30.68 cpu-ms/op 22.5MB 53213-53409 allocs
this 25.91 / 25.60 cpu-ms/op 19.0MB 45213-45613 allocs
30x60 p0/m1 base 54.95 / 53.26 cpu-ms/op 42.5MB 89466-90023 allocs
this 30.80 / 29.27 cpu-ms/op 23.0MB 45853-46928 allocs
30x60 g4 base 66.67 / 73.07 cpu-ms/op 68-70MB 112283-116845 allocs
this 45.59 / 48.38 cpu-ms/op 49-52MB 68540-74905 allocs
Run: go test ./internal/provider -run 'Hold|Group|Changed|Served'
-count 1: ok (91s). go test ./internal/gateway -run
'^(TestGroupsOnAndOff|TestRequestBuildsProvidersOnce)$' -count 1: ok.
The full suite at the head of this series is in its last commit.
120 lines
3.7 KiB
Go
120 lines
3.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"slices"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// The user's own routing groups — those not Auto, made on the Routing page
|
|
// or with `magpie group` — are plugins/groups' to serve (P3 of
|
|
// docs/subsystems/plugin-runtime.md). In the process serving the gateway
|
|
// (GateGroups), while that plugin is off (no ServeGroups held), they are
|
|
// neither listed nor routed: Groups, FindGroup, GroupFor and the catalog
|
|
// leave them out. The groups magpie finds (Auto) are the core's either
|
|
// way, as Smart is: they are how a model several providers serve is one
|
|
// model with failover, which a new install, the groups plugin off, has.
|
|
//
|
|
// The plugin stays on while the user has a group of their own
|
|
// (NamedGroups), so off, there is none to leave out but one added since
|
|
// the plugin last looked: a lookup that finds one asks the gate's refresh
|
|
// (the gateway's plugin runtime) first, which turns the plugin on, so a
|
|
// group made a moment ago with `magpie group add` is served at once. A
|
|
// process not serving the gateway (the CLI, the TUI) lists them all.
|
|
var groupGate struct {
|
|
sync.Mutex
|
|
gated int // GateGroups calls not yet stopped
|
|
served int // ServeGroups calls not yet removed
|
|
refresh func() // the last GateGroups' refresh
|
|
}
|
|
|
|
// groupRefreshing: a refresh is under way, so a lookup it makes (a plugin's
|
|
// InUse) doesn't start another; refreshed: when the last one began, so a
|
|
// plugins.yml that can't be read, which leaves the plugin off, is asked
|
|
// again at most once a second, not on every lookup
|
|
var (
|
|
groupRefreshing atomic.Bool
|
|
groupRefreshed atomic.Int64
|
|
)
|
|
|
|
// GateGroups makes the user's own groups wait on ServeGroups until stop:
|
|
// the gateway calls it as it starts serving. refresh, when not nil, is
|
|
// asked to bring the plugins up to date when a lookup gated finds a group
|
|
// of the user's own (served).
|
|
func GateGroups(refresh func()) (stop func()) {
|
|
groupGate.Lock()
|
|
groupGate.refresh = refresh
|
|
groupGate.Unlock()
|
|
return holdGate(&groupGate.gated)
|
|
}
|
|
|
|
// ServeGroups serves the user's own groups until remove: plugins/groups
|
|
// calls it while it is on.
|
|
func ServeGroups() (remove func()) {
|
|
return holdGate(&groupGate.served)
|
|
}
|
|
|
|
// holdGate holds n until the func it gives is called. What is built from
|
|
// the groups and held across requests (heldOf) is dropped either way, so
|
|
// the next lookup sees the change.
|
|
func holdGate(n *int) func() {
|
|
groupGate.Lock()
|
|
*n++
|
|
groupGate.Unlock()
|
|
dropHeld()
|
|
var once sync.Once
|
|
return func() {
|
|
once.Do(func() {
|
|
groupGate.Lock()
|
|
*n--
|
|
groupGate.Unlock()
|
|
dropHeld()
|
|
})
|
|
}
|
|
}
|
|
|
|
// GroupsServed says whether the user's own groups are served here now.
|
|
func GroupsServed() bool {
|
|
groupGate.Lock()
|
|
defer groupGate.Unlock()
|
|
return groupsServedLocked()
|
|
}
|
|
|
|
// served says whether the user's own groups among gs are served: gated and
|
|
// not, gs holding one asks the refresh once and looks again.
|
|
func served(gs []Group) bool {
|
|
groupGate.Lock()
|
|
ok, refresh := groupsServedLocked(), groupGate.refresh
|
|
groupGate.Unlock()
|
|
if ok || refresh == nil || !slices.ContainsFunc(gs, func(g Group) bool { return !g.Auto && !g.Hidden }) {
|
|
return ok
|
|
}
|
|
now := time.Now().UnixNano()
|
|
if now-groupRefreshed.Load() < int64(time.Second) || !groupRefreshing.CompareAndSwap(false, true) {
|
|
return false
|
|
}
|
|
groupRefreshed.Store(now)
|
|
refresh()
|
|
groupRefreshing.Store(false)
|
|
return GroupsServed()
|
|
}
|
|
|
|
func groupsServedLocked() bool { return groupGate.gated == 0 || groupGate.served > 0 }
|
|
|
|
// NamedGroups counts the user's own groups in providers.json, served or
|
|
// not; a file that can't be read is an error, not none.
|
|
func NamedGroups() (int, error) {
|
|
f, err := read()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n := 0
|
|
for _, g := range f.Groups {
|
|
if !g.Auto && !g.Hidden {
|
|
n++
|
|
}
|
|
}
|
|
return n, nil
|
|
}
|