Files
mesh-sdk/go/process/event_hub.go
T
2026-11-25 17:13:22 +01:00

353 lines
12 KiB
Go

package process
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-sdk/go/seatdef"
)
// EventHub is the one pull of a module's durable consumer on the shared events stream (novox/hq ADR 0635 §5): a module
// has one durable, `<node>_<module>`, so two loops pulling it each take messages the other needs. Every part of the
// module that hears events registers a handler by subject pattern, and the hub pulls once and hands each message to the
// handler whose pattern it matches. A message no handler takes is acknowledged and said, never left to be redelivered.
// The hub never makes the stream or the consumer: they are the controller's, and a bus without them is an error of Start.
//
// Register every handler, then Start once: a message heard before its handler is registered would be taken by none.
type EventHub struct {
logf func(string, ...any)
mu sync.RWMutex
routes []hubRoute
cons jetstream.ConsumeContext
bound []func(created time.Time)
// unhandled are told each subject no handler took (OnUnhandled).
unhandled []func(subject string)
// kept are the module's durables on the kept seat events' streams it handles (StartKept).
kept []jetstream.ConsumeContext
// held is every message the hub took and has not seen end, across its durables (event_hold.go); feeds are the durables'
// tickers. tick is the clock of the tickers, a test's to replace.
held *eventHold
feeds []*eventFeed
tick ticker
}
type hubRoute struct {
pattern string
handle func(jetstream.Msg)
key EventKey
}
// NewEventHub is a hub saying through logf (nil says nothing).
func NewEventHub(logf func(string, ...any)) *EventHub {
if logf == nil {
logf = func(string, ...any) {}
}
h := &EventHub{logf: logf, tick: realTicker}
h.held = newEventHold(logf, func(d time.Duration) (<-chan time.Time, func()) { return h.tick(d) })
return h
}
// Handle registers handle for the subjects matching pattern (`*` one token, `>` the rest). The handler acknowledges
// the message itself. A pattern that overlaps one registered (one matches every subject of the other) is refused,
// so a message always has one taker.
//
// key, when given, is the key of the pattern's events (design 47 H2 H-A-683 (1)): one key's events are handled one at a
// time in the order heard and the events of different keys side by side, at most `events-at-once` keys at once; every pattern
// given none hands its events under one shared key, so a caller that gives none is handled one at a time as before.
func (h *EventHub) Handle(pattern string, handle func(jetstream.Msg), key ...EventKey) error {
if pattern == "" || handle == nil {
return fmt.Errorf("an event handler needs a subject pattern and a handler")
}
if len(key) > 1 {
return fmt.Errorf("the events %q are given more than one key function", pattern)
}
var keyOf EventKey
if len(key) == 1 {
keyOf = key[0]
}
h.mu.Lock()
defer h.mu.Unlock()
for _, r := range h.routes {
if subjectsOverlap(r.pattern, pattern) {
return fmt.Errorf("the events %q overlap %q, already handled: a message has one taker", pattern, r.pattern)
}
}
h.routes = append(h.routes, hubRoute{pattern, handle, keyOf})
return nil
}
// OnBound registers f, told each time the hub binds the module's consumer, with when the bus made it. A consumer is made
// from now (ADR 0443), so an event said before it was made is not delivered to it: a part that took a picture before
// that must ask its owner again (views.Follower.ConsumerMade). So every handler of an event that ends a kept call's wait
// (a callee's "my state changed") also registers here the call it makes on that event, made again once at each bind
// (novox/hq ADR 0668; issue 1966). Register before Start.
func (h *EventHub) OnBound(f func(created time.Time)) {
h.mu.Lock()
h.bound = append(h.bound, f)
h.mu.Unlock()
}
// OnUnhandled registers f, told the subject of each message no handler takes, after the hub said it in the log and
// acknowledged it: the module's process reader counts them in its finding `event-unhandled` (design 47 H, H-A-166,
// H-A-169).
func (h *EventHub) OnUnhandled(f func(subject string)) {
h.mu.Lock()
h.unhandled = append(h.unhandled, f)
h.mu.Unlock()
}
// Patterns are the registered subject patterns, in order.
func (h *EventHub) Patterns() []string {
h.mu.RLock()
defer h.mu.RUnlock()
out := make([]string, len(h.routes))
for i, r := range h.routes {
out[i] = r.pattern
}
return out
}
// Start binds the module's durable on the shared events stream and pulls it; a call after a start that took does
// nothing. It makes no stream and no consumer: a consumer the bus does not hold yet is the error of this call, and a
// later call (the module's next change of membership) binds it, so a part is not lost for the bus's order.
func (h *EventHub) Start(ctx context.Context, js jetstream.JetStream, cred Account) error {
h.mu.Lock()
defer h.mu.Unlock()
if h.cons != nil {
return nil
}
cons, err := cred.EventConsumer(ctx, js, EventsStream)
if err == nil {
err = h.held.settle()
}
if err == nil {
feed := h.newFeed()
if info := cons.CachedInfo(); info != nil {
feed.start(info.Config.AckWait)
}
h.cons, err = hearHeld(cons, callerOutside(), h.hearOn(feed), h.logf)
if err != nil {
feed.end()
}
}
if err != nil {
return fmt.Errorf("the consumer %s on %s is not heard: %w", cred.EventConsumerName(), EventsStream, err)
}
if info, ierr := cons.Info(ctx); ierr != nil {
h.logf("events: the consumer %s on %s is heard, but its information is not read (%v): a first view taken before it was made is not asked again", cred.EventConsumerName(), EventsStream, ierr)
} else {
h.logf("events: the consumer %s on %s is heard; the bus made it at %s", cred.EventConsumerName(), EventsStream, info.Created.UTC().Format(time.RFC3339))
for _, f := range h.bound {
f(info.Created.UTC())
}
}
return nil
}
// StartUntilBound is Start that does not give up: when the module's consumer is not there yet it returns that error at
// once, and tries again on a goroutine of its own, after each wait, until it binds or ctx ends; then live is called (nil
// calls nothing), so a part that needs the events path live does what it does only then. wait returns false to stop
// trying (nil waits five seconds or for ctx). The bus makes the consumer when the module's account is composed, which
// says nothing a module can hear before the consumer exists, so the retry is a wait on that, bounded by ctx.
func (h *EventHub) StartUntilBound(ctx context.Context, js jetstream.JetStream, cred Account, wait func(context.Context) bool, live func()) error {
err := h.Start(ctx, js, cred)
if err == nil {
if live != nil {
live()
}
return nil
}
if wait == nil {
wait = func(ctx context.Context) bool {
t := time.NewTimer(5 * time.Second)
defer t.Stop()
select {
case <-ctx.Done():
return false
case <-t.C:
return true
}
}
}
go func() {
for wait(ctx) && ctx.Err() == nil {
if h.Start(ctx, js, cred) == nil {
if live != nil {
live()
}
return
}
}
}()
return err
}
// Stop ends the pull.
func (h *EventHub) Stop() {
h.mu.Lock()
defer h.mu.Unlock()
if h.cons != nil {
h.cons.Stop()
h.cons = nil
}
for _, cc := range h.kept {
cc.Stop()
}
h.kept = nil
for _, f := range h.feeds {
f.end()
}
h.feeds = nil
h.held.stop()
}
// newFeed makes the ticker of one durable the hub pulls (event_hold.go).
func (h *EventHub) newFeed() *eventFeed {
f := newEventFeed(h.held)
h.feeds = append(h.feeds, f)
return f
}
// StartKept binds, beside EVENTS, the module's durable on each kept seat event's stream (`SEAT_<seat>_<event>`) whose event a
// registered pattern takes: a kept event is never on EVENTS, and the tool runner reads its own twin of that durable (lead
// answer A-397), so the program hears it only here. Each stream bound is said, and each it cannot bind (no durable composed
// for this module there, or the bus refuses) is said and passed over. It returns how many it bound.
func (h *EventHub) StartKept(ctx context.Context, js jetstream.JetStream, cred Account) int {
h.mu.RLock()
var patterns []string
for _, r := range h.routes {
patterns = append(patterns, r.pattern)
}
h.mu.RUnlock()
names := js.StreamNames(ctx)
var streams []string
for name := range names.Name() {
streams = append(streams, name)
}
if err := names.Err(); err != nil {
h.logf("events: the kept seat events' streams are not listed (%v): none of them is heard", err)
}
bound := 0
for _, stream := range streams {
seat, event, ok := keptSeatEvent(stream)
if !ok {
continue
}
subject := "mesh.seat." + seat + ".event." + event
taken := false
for _, p := range patterns {
if subjectMatches(p, subject) || subjectMatches(p, subject+".x") {
taken = true
break
}
}
if !taken {
continue
}
h.mu.Lock()
feed := h.newFeed()
h.mu.Unlock()
cc, err := cred.HearSeatEvents(ctx, js, stream, h.hearOn(feed), h.logf, heldBy(feed))
if err != nil {
feed.end()
h.logf("events: the kept event %s.%s is not heard on %s: %v", seat, event, stream, err)
continue
}
h.mu.Lock()
h.kept = append(h.kept, cc)
h.mu.Unlock()
bound++
h.logf("events: the consumer %s on %s is heard (the kept event %s.%s)", cred.EventConsumerName(), stream, seat, event)
}
return bound
}
// keptSeatEvent is the seat and the event a kept seat event's stream carries, from its name `SEAT_<seat>_<event>` (seatdef.KeptStream):
// a seat's and an event's names hold no underscore.
func keptSeatEvent(stream string) (seat, event string, ok bool) {
rest, isKept := strings.CutPrefix(stream, seatdef.KeptStreamPrefix)
if !isKept {
return "", "", false
}
seat, event, ok = strings.Cut(rest, "_")
if !ok || seat == "" || event == "" || strings.Contains(event, "_") {
return "", "", false
}
return seat, event, true
}
// hear is hearOn for a message with no durable of its own to say in progress on.
func (h *EventHub) hear(m jetstream.Msg) { h.hearOn(nil)(m) }
// hearOn is the handler of one durable's messages: it finds the message's taker and key, and holds it for them (event_hold.go),
// so the pull goes on while a handler runs and every message held is said in progress by the durable's ticker. A message no
// handler takes is acknowledged and said, never held.
func (h *EventHub) hearOn(feed *eventFeed) func(jetstream.Msg) {
return func(m jetstream.Msg) {
h.mu.RLock()
var route *hubRoute
for i := range h.routes {
if subjectMatches(h.routes[i].pattern, m.Subject()) {
route = &h.routes[i]
break
}
}
told := h.unhandled
h.mu.RUnlock()
if route == nil {
h.logf("events: %s is taken by no handler of this module, acknowledged", m.Subject())
_ = m.Ack()
for _, f := range told {
f(m.Subject())
}
return
}
key := sharedEventKey
if route.key != nil {
key = keyedEventKeyPrefix + route.key(m.Subject(), m.Data())
}
id := ""
if md, err := m.Metadata(); err == nil {
id = md.Stream + "/" + strconv.FormatUint(md.Sequence.Stream, 10)
}
h.held.take(&heldEvent{msg: m, handle: route.handle, feed: feed, id: id, key: key, subject: m.Subject()})
}
}
// subjectMatches says whether subject is one the pattern (`*` one token, `>` the rest) names.
func subjectMatches(pattern, subject string) bool {
pt, st := strings.Split(pattern, "."), strings.Split(subject, ".")
for i, t := range pt {
if t == ">" {
return len(st) > i
}
if i >= len(st) || (t != "*" && t != st[i]) {
return false
}
}
return len(pt) == len(st)
}
// subjectsOverlap says whether some subject matches both patterns.
func subjectsOverlap(a, b string) bool {
at, bt := strings.Split(a, "."), strings.Split(b, ".")
for i := 0; ; i++ {
if i >= len(at) || i >= len(bt) {
return len(at) == len(bt)
}
if at[i] == ">" || bt[i] == ">" {
return true
}
if at[i] != "*" && bt[i] != "*" && at[i] != bt[i] {
return false
}
}
}