H1 one consumer loop: the follower hears on the module's one consumer
In a module's own runtime the branch follower (first views) and the bundles each bound the module's durable <node>_<module>_tools with a pull loop of their own, so each event reached one of the two: the catalog revision of main.1 reached the follower and never the indexer's bundle, whose revision verb answered none-yet. The runtime now binds the consumer once, for the bundles, and hands each event to the follower after the bundles took it (runtime.WithHearer); the machine's runtime keeps the follower's own binding. Builds: design 47 H §752, H-A-113, H-A-132 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
This commit is contained in:
@@ -89,7 +89,13 @@ func serve(ctx context.Context) error {
|
||||
// The indexer's and the assigner's states the console indexes the mesh by, and the gate reads its marks from, come to it as
|
||||
// first views, asked of the owners' own tools and followed on their views families, never read from their state (novox/hq
|
||||
// ADR 0615 §1; design 46 7-approval-mark §5).
|
||||
source, stopViews := firstview.Start(context.Background(), conn, log.Printf)
|
||||
// In a module's own runtime the follower hears on the module's one consumer, bound by the runtime for its bundle (design 47
|
||||
// H §752, H-A-132).
|
||||
followed, hearer := followerOnOwnConsumer(firstview.DepsOf(conn, log.Printf), served, cred.Module)
|
||||
if hearer != nil {
|
||||
opts = append(opts, runtime.WithHearer(cred.Module, hearer.hear))
|
||||
}
|
||||
source, stopViews := firstview.StartWith(context.Background(), followed, log.Printf)
|
||||
defer stopViews()
|
||||
approvals := approvalsState{conn: conn, module: cred.Module}
|
||||
gate := approval.New(approval.Deps{Node: conn.Node(), Runner: cred.Module, Catalog: source.ApprovalCatalog(definitionsOf(seatList)), Wait: source.WaitCatalog,
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"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
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// In a module's own runtime the branch follower binds no consumer of its own and hears each event the runtime hands it from the
|
||||
// module's one consumer; a runtime that is not the module's own keeps the follower's own binding (design 47 H §752, H-A-132).
|
||||
func TestTheFollowerHearsOnTheOwnModulesOneConsumer(t *testing.T) {
|
||||
for _, row := range []struct {
|
||||
name string
|
||||
served []runtime.Served
|
||||
own string
|
||||
binds bool
|
||||
}{
|
||||
{"a module's own runtime", []runtime.Served{{Module: "mesh-delivery-indexer"}}, "mesh-delivery-indexer", false},
|
||||
{"the machine's runtime", []runtime.Served{{Module: "mesh-delivery-indexer"}}, runtime.NodeRuntimeModule, true},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
bound := 0
|
||||
d := firstview.Deps{Consume: func(func(bus.Envelope) error) (func(), error) { bound++; return func() {}, nil }}
|
||||
got, hearer := followerOnOwnConsumer(d, row.served, row.own)
|
||||
var heard []string
|
||||
stop, err := got.Consume(func(env bus.Envelope) error { heard = append(heard, env.Key); return nil })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (bound == 1) != row.binds || (hearer == nil) != row.binds {
|
||||
t.Fatalf("bound its own consumer %d time(s), hearer given to the runtime %v; want its own binding %v", bound, hearer != nil, row.binds)
|
||||
}
|
||||
if row.binds {
|
||||
return
|
||||
}
|
||||
env := bus.Envelope{Key: "mesh-catalog.catalog-revision.main", Body: json.RawMessage(`{}`)}
|
||||
if err := hearer.hear(env); err != nil || len(heard) != 1 || heard[0] != env.Key {
|
||||
t.Fatalf("the event handed by the runtime: err %v, the follower heard %q", err, heard)
|
||||
}
|
||||
stop()
|
||||
if err := hearer.hear(env); err != nil || len(heard) != 1 {
|
||||
t.Fatalf("after the follower stopped: err %v, heard %q", err, heard)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user