Files
mesh-tools/node-tools/cmd/node-tools/ownconsumer.go
T
jschoubben fb4fe282c7 H1 one consumer loop: serve's first views wiring in one tested function
Review round 1: no test failed when serve went back to firstview.Start.
startViews starts the follower and answers the options runtime.Run is
given; its test holds that on a module's own account the follower binds
nothing and the runtime is given the hearer, and on the machine's
account the follower binds its own.

Builds: design 47 H §752, H-A-113, H-A-132, H-A-133

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 11:33:19 +01:00

69 lines
2.5 KiB
Go

package main
import (
"context"
"sync"
"github.com/novox/mesh-tools/node-tools/internal/bus"
"github.com/novox/mesh-tools/node-tools/internal/firstview"
"github.com/novox/mesh-tools/node-tools/internal/runtime"
)
// ownHearer is the branch follower's hearing in a module's own runtime: the runtime binds the module's consumer once, for
// its bundle, and hands each event to it after the bundle took it (runtime.WithHearer), so no second pull loop reads the
// same durable and each event reaches both (design 47 H §752, H-A-113, H-A-132).
type ownHearer struct {
mu sync.Mutex
deliver func(bus.Envelope) error
}
// consume is the follower's Consume: it binds nothing, and holds deliver until the stop is called.
func (h *ownHearer) consume(deliver func(bus.Envelope) error) (func(), error) {
h.mu.Lock()
h.deliver = deliver
h.mu.Unlock()
return func() {
h.mu.Lock()
h.deliver = nil
h.mu.Unlock()
}, nil
}
// hear hands one event of the module's consumer to the follower, while it hears.
func (h *ownHearer) hear(env bus.Envelope) error {
h.mu.Lock()
deliver := h.deliver
h.mu.Unlock()
if deliver == nil {
return nil
}
return deliver(env)
}
// followerOnOwnConsumer is the first views' reach in a runtime that serves its own account's module: the follower hears on
// the consumer the runtime binds for the module's bundle, through the hearer answered, which the runtime is given
// (runtime.WithHearer). A runtime that serves another module's account (the machine's runtime) keeps the follower's own
// binding, and answers no hearer.
func followerOnOwnConsumer(d firstview.Deps, served []runtime.Served, own string) (firstview.Deps, *ownHearer) {
for _, s := range served {
if s.Module == own {
h := &ownHearer{}
d.Consume = h.consume
return d, h
}
}
return d, nil
}
// startViews is serve's first views: it starts the follower over d and answers the options runtime.Run is given, so that on a
// module's own account the follower binds nothing and hears on the module's one consumer (runtime.WithHearer), and on the
// machine's account the follower binds its own (design 47 H §752, H-A-132, H-A-133).
func startViews(ctx context.Context, d firstview.Deps, served []runtime.Served, own string, logf func(string, ...any)) (*firstview.Views, func(), []runtime.Option) {
followed, hearer := followerOnOwnConsumer(d, served, own)
source, stop := firstview.StartWith(ctx, followed, logf)
if hearer == nil {
return source, stop, nil
}
return source, stop, []runtime.Option{runtime.WithHearer(own, hearer.hear)}
}