Builds: design 47 G §614, A-5 (G-20) Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
179 lines
7.0 KiB
Go
179 lines
7.0 KiB
Go
package meshcatalogclient
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-sdk/go/buscalls"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
)
|
|
|
|
// RegistrationsVerb is the seat verb a part takes its first view of the registrations by, as its manifest's invokes
|
|
// names it.
|
|
const RegistrationsVerb = "seat:" + seat.Seat + "." + seat.VerbRegistrations
|
|
|
|
// Call asks one seat verb over the bus and answers what the holder said, whatever wraps it (process.Conn.Call, or
|
|
// stdio.Ask through the tool runner).
|
|
type Call func(key string, body any) (json.RawMessage, error)
|
|
|
|
// registrationsCreated stands for the picture's Created in its follower: the registrations are followed by the seat's
|
|
// event, never by an owner's views family, so one fixed time, never zero.
|
|
var registrationsCreated = time.Unix(0, 0).UTC()
|
|
|
|
// Registrations is a part's typed follower of the registrations (design 47 G §353): a first view asked of the seat's verb
|
|
// `registrations`, then each `registration-changed` heard, built on go/views' Follower, so the runtime's tools
|
|
// `<module>_first_views` and `<module>_first_view_again` read and steer it as every first view. Its picture is keyed by
|
|
// module, each value the module's newest registration. Ready is closed at the first view: a part that switches on its
|
|
// first registrations waits for it.
|
|
type Registrations struct {
|
|
Follower *views.Follower
|
|
|
|
call Call
|
|
}
|
|
|
|
// NewRegistrations is a Registrations asking the seat through call. Nothing is asked before Start.
|
|
func NewRegistrations(call Call) *Registrations {
|
|
r := &Registrations{call: call}
|
|
r.Follower = &views.Follower{Owner: seat.Seat, Tool: RegistrationsVerb, State: seat.StateRegistrations, Ask: r.ask}
|
|
return r
|
|
}
|
|
|
|
// WireRegistrations is a Registrations adopted by a stdio part's views.Reader: the reader writes its conditions, reads it
|
|
// as `mesh-catalog.registrations`, lists and asks it again in its first-view tools, and at its Start subscribes the
|
|
// seat's event registration-changed (the part's manifest consumes it) into Hear and asks the first view. changed, when
|
|
// not nil, is told of each module whose registration the picture changes, after the reader's own. The part declares the
|
|
// seat's verb registrations in invokes.
|
|
func WireRegistrations(r *views.Reader, call Call, changed func(reg seat.Registration, gone bool)) (*Registrations, error) {
|
|
g := NewRegistrations(call)
|
|
g.Follower.Changed = typedChanged(changed)
|
|
if err := r.Adopt(g.Follower, seat.Seat, seat.EventRegistrationChanged, g.Hear); err != nil {
|
|
return nil, err
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
// WireRegistrationsProcess is WireRegistrations for a process's views.ProcessReader: its picture
|
|
// (`mesh-catalog_registrations`) reads the registrations as a read-only bucket, and the seat's event
|
|
// registration-changed on the module's durable consumer goes to Hear.
|
|
func WireRegistrationsProcess(r *views.ProcessReader, call Call, changed func(reg seat.Registration, gone bool)) (*Registrations, *views.Picture, error) {
|
|
g := NewRegistrations(call)
|
|
g.Follower.Changed = typedChanged(changed)
|
|
p, err := r.Adopt(g.Follower, seat.Seat, seat.EventRegistrationChanged, g.Hear)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return g, p, nil
|
|
}
|
|
|
|
func typedChanged(changed func(seat.Registration, bool)) func(string, json.RawMessage, bool, time.Time) {
|
|
if changed == nil {
|
|
return nil
|
|
}
|
|
return func(module string, value json.RawMessage, gone bool, _ time.Time) {
|
|
reg := seat.Registration{Module: module}
|
|
if !gone {
|
|
_ = json.Unmarshal(value, ®)
|
|
}
|
|
changed(reg, gone)
|
|
}
|
|
}
|
|
|
|
// Start asks the first view until it is answered or ctx ends.
|
|
func (r *Registrations) Start(ctx context.Context) error { return r.Follower.Start(ctx) }
|
|
|
|
// Ready is closed once the first view is answered.
|
|
func (r *Registrations) Ready() <-chan struct{} { return r.Follower.Ready() }
|
|
|
|
// Registration is one module's newest registration in the picture.
|
|
func (r *Registrations) Registration(module string) (seat.Registration, bool) {
|
|
raw, ok := r.Follower.Get(module)
|
|
if !ok {
|
|
return seat.Registration{}, false
|
|
}
|
|
var reg seat.Registration
|
|
if err := json.Unmarshal(raw, ®); err != nil {
|
|
return seat.Registration{}, false
|
|
}
|
|
return reg, true
|
|
}
|
|
|
|
// All is every registration in the picture, by module name.
|
|
func (r *Registrations) All() []seat.Registration {
|
|
values := r.Follower.Values()
|
|
out := make([]seat.Registration, 0, len(values))
|
|
for _, raw := range values {
|
|
var reg seat.Registration
|
|
if json.Unmarshal(raw, ®) == nil {
|
|
out = append(out, reg)
|
|
}
|
|
}
|
|
slices.SortFunc(out, func(a, b seat.Registration) int {
|
|
if a.Module < b.Module {
|
|
return -1
|
|
}
|
|
if a.Module > b.Module {
|
|
return 1
|
|
}
|
|
return 0
|
|
})
|
|
return out
|
|
}
|
|
|
|
// Hear takes one `registration-changed` event, as the part's durable consumer of the seat's events hands it. A version
|
|
// the picture holds, or one older, is passed over; a newer one asks the first view again on its own goroutine, so the
|
|
// consumer is never held while the holder is away, and again until the picture holds that version or ctx ends (an ask
|
|
// already running, the first view among them, may have been answered before the change was written). Data that is no such event is an error.
|
|
func (r *Registrations) Hear(ctx context.Context, data []byte) error {
|
|
var ev seat.RegistrationChangedEvent
|
|
if err := json.Unmarshal(data, &ev); err != nil || ev.Module == "" || ev.Version <= 0 {
|
|
return fmt.Errorf("meshcatalogclient: not a %s event: %s", seat.EventRegistrationChanged, clipBytes(data))
|
|
}
|
|
if r.holds(ev.Module, ev.Version) {
|
|
return nil
|
|
}
|
|
go r.catchUp(ctx, ev.Module, ev.Version)
|
|
return nil
|
|
}
|
|
|
|
func (r *Registrations) holds(module string, version int64) bool {
|
|
reg, ok := r.Registration(module)
|
|
return ok && reg.Version >= version
|
|
}
|
|
|
|
func (r *Registrations) catchUp(ctx context.Context, module string, version int64) {
|
|
catchUp(ctx, r.Follower, func() bool { return r.holds(module, version) }, fmt.Sprintf("registration of %s at version %d said", module, version))
|
|
}
|
|
|
|
// ask is the Follower's Ask: every registration, whole in one answer.
|
|
func (r *Registrations) ask(_ context.Context, _ string) (views.Answer, error) {
|
|
raw, err := r.call(RegistrationsVerb, seat.RegistrationsInput{})
|
|
if err != nil {
|
|
return views.Answer{}, err
|
|
}
|
|
answer, said, ok := buscalls.Unwrap(raw)
|
|
if !ok {
|
|
return views.Answer{}, fmt.Errorf("%s answered: %s", RegistrationsVerb, said)
|
|
}
|
|
var a seat.RegistrationsAnswer
|
|
if err := json.Unmarshal(answer, &a); err != nil || a.Registrations == nil {
|
|
return views.Answer{}, fmt.Errorf("%s answered no registrations: %s", RegistrationsVerb, clipBytes(answer))
|
|
}
|
|
values := make(map[string]json.RawMessage, len(a.Registrations))
|
|
for _, reg := range a.Registrations {
|
|
v, _ := json.Marshal(reg)
|
|
values[reg.Module] = v
|
|
}
|
|
return views.Answer{V: 1, Owner: seat.Seat, State: seat.StateRegistrations, Created: registrationsCreated, Values: values, At: time.Now()}, nil
|
|
}
|
|
|
|
func clipBytes(b []byte) string {
|
|
if len(b) > 200 {
|
|
return string(b[:200]) + "…"
|
|
}
|
|
return string(b)
|
|
}
|