diff --git a/internal/provider/group.go b/internal/provider/group.go index 861cf1f41..a1f694337 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -1593,9 +1593,18 @@ func GroupsWith(id string) []Group { // by the model's "provider/model" id; a member fixed at an effort counts as // its model, and a group removed is none. A provider kept for routing // groups (Provider.Unlisted) reaches agents through these alone: a model -// of it in none of them is used by nothing. +// of it in none of them is used by nothing. Held while a request holds the +// catalog, so it is shared, only for reading: the providers' page asked +// for it once for each provider, 11.6 MB of what twenty polls allocated. func MemberGroups() map[string][]string { - entries := providerEntries() + return heldFrom("member-groups", providerEntries(), memberGroupsIn) +} + +// memberGroupsBuilt counts MemberGroups built, for a test. +var memberGroupsBuilt atomic.Int64 + +func memberGroupsIn(entries []Entry) map[string][]string { + memberGroupsBuilt.Add(1) out := map[string][]string{} for _, g := range groupsIn(entries) { if g.Hidden { diff --git a/internal/provider/member_groups_held_test.go b/internal/provider/member_groups_held_test.go new file mode 100644 index 000000000..3eb28ab1e --- /dev/null +++ b/internal/provider/member_groups_held_test.go @@ -0,0 +1,40 @@ +package provider + +import ( + "slices" + "testing" +) + +// The providers' page asked MemberGroups once for each provider, and each +// built the groups' members over the whole catalog again: 11.6 MB of +// what twenty polls allocated. While a request holds the catalog it is +// built once, and a group saved is in the next request's. +func TestMemberGroupsBuiltOnceWhileHeld(t *testing.T) { + groupHeldFixture(t) + look := func() map[string][]string { + defer Hold()() + var in map[string][]string + for range 12 { + in = MemberGroups() + } + return in + } + look() + before := memberGroupsBuilt.Load() + release := Hold() + for range 20 { + if gs := MemberGroups()["p2/m1"]; !slices.Contains(gs, GroupPrefix+"g4") { + t.Fatalf("p2/m1 is in %q, not group/g4", gs) + } + } + release() + if n := memberGroupsBuilt.Load() - before; n > 1 { + t.Fatalf("20 looks under one Hold built the member groups %d times, want 1 at most", n) + } + if err := SaveGroup(Group{ID: "g5", Name: "G5", Members: []string{"p5/m5"}, Routing: Ordered}); err != nil { + t.Fatal(err) + } + if gs := look()["p5/m5"]; !slices.Contains(gs, GroupPrefix+"g5") { + t.Fatalf("a group saved: p5/m5 is in %q, not group/g5", gs) + } +} diff --git a/internal/provider/models.go b/internal/provider/models.go index d6cb94fe8..f5a7aaf38 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -2820,8 +2820,12 @@ func unlistedByGroups(entries []Entry) map[string][]string { // names: a provider's editor names them, as a model its own switch put // back for agents is still left out by them. A signed-in subscription's // models, which agents keep (SubscriptionKept), aren't among them. +// Held as MemberGroups is: shared, only for reading. func GroupKept() map[string][]string { - entries := providerEntries() + return heldFrom("group-kept", providerEntries(), groupKeptIn) +} + +func groupKeptIn(entries []Entry) map[string][]string { kept := unlistedByGroups(entries) if len(kept) == 0 { return nil