Files
mesh-delivery/modules/mesh-grants/internal/buscompose/kept.go
T
jschoubben a559b0e7c9 D1b: the two seats' work queues carry their settings, derived once in mesh-sdk
mesh-builds' and mesh-wants' accepted kind step names max-age 24h, max-msgs
10,000, ack-wait 60s, in-progress 20s, max-ack-pending 1,000, max-deliveries 5
and a duplicates window of 2m; the grants part makes each seat's ask stream
through mesh-sdk's buscompose.SeatQueue and says a seat whose settings it
refuses, and its own copy of the derivation goes.

Builds: design 47 H2 §777; H-A-350 DMC-1, H-A-362
2026-11-21 23:23:04 +01:00

158 lines
6.5 KiB
Go

package buscompose
import (
"maps"
"slices"
"sort"
"strings"
"time"
"github.com/nats-io/nats.go/jetstream"
sdkbc "git.novox.be/novox/mesh-sdk/go/buscompose"
"git.novox.be/novox/mesh-sdk/go/seatdef"
)
// **A seat's event kept in a stream of its own** (novox/hq design 47, *What each step says*; the seat definition's `keep`):
// the node-engine's `running.<node>.<module>` last per subject with no age limit, the gate's and the checker's `owed` for a
// week within 64 MiB. The event's subject lies under `mesh.seat.*.event.>`, which the shared events stream already takes, and
// the bus refuses two streams taking one subject: so the kept stream takes no subject of its own but sources the shared
// stream's messages of that subject, and keeps them by its own limits (the shared stream's seven days never end a
// `running`). The part makes the stream, and each hearing module's durable on it from the module's `consumes` (the bus name
// is seatdef.KeptStream's, the one name for every part that hears it).
// KeptEvent is a seat's event with a stream of its own.
type KeptEvent struct {
Seat string
Event string
// Stream is its name on the bus (seatdef.KeptStream).
Stream string
// Filter is the subject that carries every instance of the event, a part of its subject `*`.
Filter string
// Last says the stream keeps the last message of each subject; MaxAgeDays (0 for no age limit) and MaxMiB bound it
// as the event's keep says, its defaults 90 days and 16 MiB (design 47 G §396, §331).
Last bool
MaxAgeDays int
MaxMiB int
Why string
}
// KeptEventsOf is every event the seat definitions keep in a stream of its own, sorted by stream name. Pure.
func KeptEventsOf(defs map[string]seatdef.Definition) []KeptEvent {
var out []KeptEvent
for _, d := range defs {
for _, e := range d.Events {
if e.Keep == nil {
continue
}
out = append(out, KeptEvent{Seat: d.Seat, Event: e.Name, Stream: seatdef.KeptStream(d.Seat, e),
Filter: seatdef.EventSubject(d.Seat, e.Local()), Last: e.KeptLastPerSubject(), MaxAgeDays: e.KeptAgeDays(), MaxMiB: e.KeptMiB(),
Why: "the seat " + d.Seat + "'s event " + e.Name + ", kept apart from the shared events stream (design 47): " + keptHow(e)})
}
}
sort.Slice(out, func(i, j int) bool { return out[i].Stream < out[j].Stream })
return out
}
func keptHow(e seatdef.Message) string {
if e.KeptLastPerSubject() {
return "the last message of each subject, no age limit"
}
return "every message within its age and size"
}
// LastPerSubject says a hearing consumer starts from the last message of each subject, never from now.
func (k KeptEvent) LastPerSubject() bool { return k.Last }
// StreamConfig is the kept stream as it is created: sourced from the shared events stream by the event's subject, with
// its own limits as the event's keep says (design 47 G §396): its age (none for 0), its cap, and one message a subject
// when it keeps the last of each. A stream at its cap drops its oldest message (§331).
func (k KeptEvent) StreamConfig() jetstream.StreamConfig {
cfg := jetstream.StreamConfig{
Name: k.Stream,
Description: k.Why,
Storage: jetstream.FileStorage,
Replicas: 1,
Sources: []*jetstream.StreamSource{{Name: EventsStream, FilterSubject: k.Filter}},
MaxAge: time.Duration(k.MaxAgeDays) * 24 * time.Hour,
MaxBytes: int64(k.MaxMiB) << 20,
}
if k.Last {
cfg.MaxMsgsPerSubject = 1
}
return cfg
}
// KeptHeard is the part of a module's `consumes` (`<seat>.<event>`) that names a kept event, as the Heard it makes: the
// module's durable on the kept stream, filtered to the subject it consumes, from the last message of each subject for an
// event so kept. ok is false for a consume that names none.
func KeptHeard(kept []KeptEvent, consume string) (h Heard, ok bool) {
seat, rest, named := strings.Cut(consume, ".")
if !named {
return Heard{}, false
}
for _, k := range kept {
local := strings.TrimPrefix(k.Filter, seatdef.EventSubject(k.Seat, ""))
if k.Seat != seat || (rest != k.Event && rest != local) {
continue
}
return Heard{Stream: k.Stream, Emitter: k.Seat, Filters: []string{k.Filter}, LastPerSubject: k.LastPerSubject()}, true
}
return Heard{}, false
}
// SplitKept divides a module's consumes into the ones the shared events stream carries and the kept events' hearing, merged
// by stream, filters sorted without repeats.
func SplitKept(kept []KeptEvent, consumes []string) (rest []string, hears []Heard) {
byStream := map[string]*Heard{}
var order []string
for _, c := range consumes {
h, ok := KeptHeard(kept, c)
if !ok {
rest = append(rest, c)
continue
}
if byStream[h.Stream] == nil {
byStream[h.Stream] = &Heard{Stream: h.Stream, Emitter: h.Emitter, LastPerSubject: h.LastPerSubject}
order = append(order, h.Stream)
}
byStream[h.Stream].Filters = append(byStream[h.Stream].Filters, h.Filters...)
}
for _, s := range order {
h := byStream[s]
slices.Sort(h.Filters)
h.Filters = slices.Compact(h.Filters)
hears = append(hears, *h)
}
return rest, hears
}
// AskStreamsOf is the work-queue stream of each seat of the seat list that accepts work, as mesh-sdk's buscompose.SeatQueue
// derives it from the seat's accepted kinds' settings (design 47 H2 §777; H-A-362): `SEAT_<SEAT>` on
// `mesh.seat.<seat>.accept.>`, its max-age, max-msgs and duplicates window, so an ask is held until one holder takes it, and
// a JetStream publish of an ask is answered by the bus. Before G the controller made these; a seat whose queues came with G
// (mesh-builds, mesh-wants) had none, so every ask to it timed out unanswered (lead-47, 2026-11-15). A seat whose
// settings SeatQueue refuses is named in refused and gets no stream. Sorted by stream.
func AskStreamsOf(defs map[string]seatdef.Definition) (out []jetstream.StreamConfig, refused []string) {
for _, name := range slices.Sorted(maps.Keys(defs)) {
d := defs[name]
if len(d.Accepts) == 0 || !safeSubject.MatchString(d.Seat) {
continue
}
st, _, ok, err := sdkbc.SeatQueue(d.Seat, d.Accepts)
if err != nil {
refused = append(refused, err.Error())
continue
}
if !ok {
continue
}
out = append(out, jetstream.StreamConfig{Name: st.Name, Description: st.Why, Subjects: st.Subjects,
Retention: jetstream.WorkQueuePolicy, Storage: jetstream.FileStorage, Replicas: 1,
MaxAge: time.Duration(st.MaxAge) * time.Second, MaxMsgs: int64(st.MaxMsgs),
Duplicates: time.Duration(st.DuplicatesSeconds) * time.Second})
}
slices.SortFunc(out, func(a, b jetstream.StreamConfig) int { return strings.Compare(a.Name, b.Name) })
return out, refused
}