mesh-grants: read the loaded list one user per answer, so no answer exceeds one bus message
lead: principle 7, live hold 02:0x (nats_users for the whole list was 1086642 bytes, over 1048576). Builds: design 47 G, lead's handover (no design line beyond principle 7) Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package grants
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// sizeBus answers nats_users as the bus module does, and refuses an answer over one bus message.
|
||||
type sizeBus struct {
|
||||
users []User
|
||||
limit int
|
||||
}
|
||||
|
||||
func (b sizeBus) Call(key string, body any) (json.RawMessage, error) {
|
||||
part, _ := body.(map[string]any)["user"].(string)
|
||||
if part == "" {
|
||||
var names []map[string]any
|
||||
for _, u := range b.users {
|
||||
names = append(names, map[string]any{"user": u.User, "publish_grants": len(u.Publish)})
|
||||
}
|
||||
return json.Marshal(map[string]any{"users": names})
|
||||
}
|
||||
var out []User
|
||||
for _, u := range b.users {
|
||||
if strings.Contains(u.User, part) {
|
||||
out = append(out, u)
|
||||
}
|
||||
}
|
||||
raw, _ := json.Marshal(map[string]any{"users": out})
|
||||
if len(raw) > b.limit {
|
||||
return nil, errors.New("the answer is more than the bus carries in one message: ask for less")
|
||||
}
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
// lead: principle 7, live hold 02:0x: the loaded list is read one user per answer, never the whole list in one.
|
||||
func TestLoadedUsersReadsOneUserPerAnswer(t *testing.T) {
|
||||
var users []User
|
||||
for i := 0; i < 20; i++ {
|
||||
var pub []string
|
||||
for j := 0; j < 50; j++ {
|
||||
pub = append(pub, fmt.Sprintf("subject.%d.%d.>", i, j))
|
||||
}
|
||||
users = append(users, User{User: fmt.Sprintf("mod.u%02d", i), Publish: pub, Subscribe: []string{"x.>"}})
|
||||
}
|
||||
users = append(users, User{User: "controller", Publish: []string{"a"}})
|
||||
got, err := LoadedUsers(sizeBus{users: users, limit: 5000})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != len(users) {
|
||||
t.Fatalf("read %d users, want %d", len(got), len(users))
|
||||
}
|
||||
// a user whose own answer is too large stays a loud hold
|
||||
if _, err := LoadedUsers(sizeBus{users: users, limit: 100}); err == nil {
|
||||
t.Fatal("an unreadable user must hold loudly")
|
||||
}
|
||||
}
|
||||
@@ -151,7 +151,7 @@ func Digest(users []User) string {
|
||||
}
|
||||
|
||||
// LoadedUsers is the list the bus loaded, as its module's nats_users reads it from the file: every user by name, then
|
||||
// each one's grants in full.
|
||||
// each one's grants in full, one user per answer so no answer exceeds one bus message.
|
||||
func LoadedUsers(c Caller) ([]User, error) {
|
||||
raw, err := c.Call(CallUsers, map[string]any{})
|
||||
if err != nil {
|
||||
@@ -183,12 +183,9 @@ func LoadedUsers(c Caller) ([]User, error) {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Every name with a dot in one call; each name without one (the controller) alone.
|
||||
if err := ask("."); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]User, 0, len(names.Users))
|
||||
for _, n := range names.Users {
|
||||
// One user per answer: the whole list is more than one bus message (the live hold, 2026-11-14).
|
||||
if _, ok := full[n.User]; !ok {
|
||||
if err := ask(n.User); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user