N1: nats_connections carries each connection's five busiest subscriptions

Also fixes the trunk race in the request test: the watch hands its handler over a channel.

Builds: design 47 H2 §813 (5); H-A-356
This commit is contained in:
2026-11-21 23:07:54 +01:00
parent 51c27203de
commit 1cbe0b1294
4 changed files with 38 additions and 15 deletions
+2 -2
View File
@@ -57,10 +57,10 @@ func tools(monitor Monitor, users func(context.Context) ([]byte, error), written
return Server(varz, health)
}},
{Name: "nats_connections",
Description: "Every client connected to the bus, named by the mesh user it is (a machine's host `node.<machine>`, a machine's runtime `<machine>.node-tools`, the controller, a person): address, uptime, round trip, traffic and pending bytes. Narrowed by a user or connection name, sorted by out_bytes (default), in_bytes, pending, msgs or subscriptions.",
Description: "Every client connected to the bus, named by the mesh user it is (a machine's host `node.<machine>`, a machine's runtime `<machine>.node-tools`, the controller, a person): address, uptime, round trip, traffic and pending bytes, and its five busiest subscriptions by messages (subject, queue group, messages). Narrowed by a user or connection name, sorted by out_bytes (default), in_bytes, pending, msgs or subscriptions.",
Input: map[string]any{"user": str("part of a user or connection name, e.g. g14"), "sort": str("out_bytes, in_bytes, pending, msgs or subscriptions"), "limit": num("at most this many (default 50)")},
Run: func(a map[string]any) (any, error) {
connz, err := get("/connz?auth=1&limit=1024")
connz, err := get("/connz?auth=1&limit=1024&subs=detail")
if err != nil {
return nil, err
}
+5 -12
View File
@@ -2,7 +2,6 @@ package main
import (
"errors"
"runtime"
"strings"
"testing"
"time"
@@ -133,20 +132,14 @@ func TestAPersonsRequestIsHandedToTheKeeperAsTheProvedCaller(t *testing.T) {
t.Errorf("an ask not raised: %v, %d requests held (was %d)", err, len(held), before)
}
// A request's end is heard on the one watch of the state, handed to the call that waits for it.
var handle func(string, []byte)
// The watch hands its handler over a channel, never a variable both goroutines touch (the race on main, H-A-356); the
// call registered its wait before it took the watch, so the handler's arrival means the call waits.
handles := make(chan func(string, []byte), 1)
watches := 0
e := newEnds(func(h func(string, []byte)) error { watches++; handle = h; return nil })
e := newEnds(func(h func(string, []byte)) error { watches++; handles <- h; return nil })
got := make(chan *keeper.Request, 1)
go func() { got <- e.wait("r1", time.Minute) }()
for {
e.mu.Lock()
ready := len(e.waiting["r1"]) == 1 && handle != nil
e.mu.Unlock()
if ready {
break
}
runtime.Gosched()
}
handle := <-handles
handle("r1", []byte(`{"id":"r1"}`))
handle("r1", []byte(`{"id":"r1","done":"2026-11-04T12:00:01Z","outcome":"created"}`))
if r := <-got; r == nil || r.Outcome != "created" || watches != 1 {
+24
View File
@@ -57,13 +57,30 @@ type Connection struct {
OutBytes int64 `json:"out_bytes"`
PendingBytes int64 `json:"pending_bytes"`
Subscriptions int64 `json:"subscriptions"`
// Busiest is the connection's busiest subscriptions by messages, at most busiestKept (design 47 H2 §813 (5), H-A-356).
Busiest []ConnSubscription `json:"busiest_subscriptions,omitempty"`
}
// ConnSubscription is one subscription of a connection: its subject, queue group and the messages it received.
type ConnSubscription struct {
Subject string `json:"subject"`
Queue string `json:"queue,omitempty"`
Msgs int64 `json:"msgs"`
}
// busiestKept is how many of a connection's subscriptions it carries, the busiest by messages (H-A-356: five).
const busiestKept = 5
func connectionsOf(connz []byte) ([]Connection, error) {
var doc struct {
Connections []struct {
Connection
AuthorizedUser string `json:"authorized_user"`
Subs []struct {
Subject string `json:"subject"`
Queue string `json:"qgroup"`
Msgs int64 `json:"msgs"`
} `json:"subscriptions_list_detail"`
} `json:"connections"`
}
if err := json.Unmarshal(connz, &doc); err != nil {
@@ -73,6 +90,13 @@ func connectionsOf(connz []byte) ([]Connection, error) {
for _, c := range doc.Connections {
conn := c.Connection
conn.User = c.AuthorizedUser
for _, sub := range c.Subs {
conn.Busiest = append(conn.Busiest, ConnSubscription{Subject: sub.Subject, Queue: sub.Queue, Msgs: sub.Msgs})
}
sort.SliceStable(conn.Busiest, func(i, j int) bool { return conn.Busiest[i].Msgs > conn.Busiest[j].Msgs })
if len(conn.Busiest) > busiestKept {
conn.Busiest = conn.Busiest[:busiestKept]
}
out = append(out, conn)
}
return out, nil
+7 -1
View File
@@ -17,7 +17,9 @@ import (
const connz = `{"connections":[
{"cid":1,"ip":"192.0.2.1","name":"mesh-host/anchor","authorized_user":"node.anchor","out_bytes":10,"in_bytes":5,"pending_bytes":0,"subscriptions":2,"in_msgs":3,"out_msgs":4},
{"cid":2,"ip":"192.0.2.2","name":"laptop.node-tools","authorized_user":"laptop.node-tools","out_bytes":900,"in_bytes":50,"pending_bytes":7,"subscriptions":300,"in_msgs":30,"out_msgs":40}]}`
{"cid":2,"ip":"192.0.2.2","name":"laptop.node-tools","authorized_user":"laptop.node-tools","out_bytes":900,"in_bytes":50,"pending_bytes":7,"subscriptions":300,"in_msgs":30,"out_msgs":40,
"subscriptions_list_detail":[{"subject":"a","msgs":1},{"subject":"b","qgroup":"q","msgs":60},{"subject":"c","msgs":5},
{"subject":"d","msgs":40},{"subject":"e","msgs":3},{"subject":"f","msgs":2}]}]}`
const subsz = `{"num_subscriptions":3,"subscriptions_list":[
{"subject":"$SRV.INFO","msgs":4,"cid":2},
@@ -59,6 +61,10 @@ func TestConnectionsAreNamedByTheirUserAndSorted(t *testing.T) {
if len(cs) != 2 || cs[0].User != "laptop.node-tools" || cs[1].User != "node.anchor" {
t.Fatalf("%+v", cs)
}
// Design 47 H2 §813 (5), H-A-356: a connection's five busiest subscriptions by messages, its queue group named.
if b := cs[0].Busiest; fmt.Sprint(b) != "[{b q 60} {d 40} {c 5} {e 3} {f 2}]" || cs[1].Busiest != nil {
t.Fatalf("busiest %v, %v", b, cs[1].Busiest)
}
narrowed, _ := Connections([]byte(connz), "anchor", "pending", 10)
if n := narrowed["total"].(int); n != 1 {
t.Fatalf("narrowed to %d", n)