Files
mesh-delivery/modules/mesh-delivery-teller/cmd/mesh-delivery-teller/decided.go
T
jschoubben bce3c96acb Join 9: the indexer, teller and pipeline name no seat list verb in their operatorask contract (A-126)
operatorask finds the holder through seatlist.HolderOf, so Contract.Lists and ListBody are gone.

Builds: design 47 G A-126

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 01:51:54 +01:00

121 lines
4.9 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
"strings"
"github.com/nats-io/nats.go/jetstream"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/operatorask"
)
// The teller's three bus adapters for mesh-sdk go/asker, which in a bundle reaches the bus through the runtime: here the
// teller is a process on its own credential. What each may do is the controller's composed grant for a module that uses
// the seat operator-channel (mesh-control internal/broker seattraffic.go SeatTrafficOf): publish its own ask, cancel and
// condition, hear its own decided event, and read the router's record of its own asks.
// publishVia publishes an accept of the seat, de-duplicated by id: the same id twice is one message.
func publishVia(ctx context.Context, js jetstream.JetStream) func(subject string, body any, id string) error {
return func(subject string, body any, id string) error {
data, err := json.Marshal(body)
if err != nil {
return err
}
var opts []jetstream.PublishOpt
if id != "" {
opts = append(opts, jetstream.WithMsgID(id))
}
_, err = js.Publish(ctx, subject, data, opts...)
return err
}
}
// recordVia reads the router's record of one of this module's asks under the module's own name: the key `<module>.<id>` of the
// state asks of the seat operator-channel, in the bucket of the seat's holder on the seat list (mesh-sdk's operatorask, design
// 47 G A-101, A-74); no bucket's name is in the program. The record is {state, warrant}; an ask the router holds no record
// of is nil. call asks the seat list's verb (process.Conn.Call) and ask is the get of the record (process.Conn.Ask), which the
// controller grants for a module that uses the seat.
func recordVia(call operatorask.Caller, ask func(bucket, id string) (*stdio.StateEntry, error)) func(id string) (*stdio.StateEntry, error) {
contract := operatorask.Contract{Seat: oc.Seat, StateAsks: oc.StateAsks}
return func(id string) (*stdio.StateEntry, error) {
bucket, err := operatorask.Bucket(call, contract)
if err != nil {
return nil, err
}
return ask(bucket, id)
}
}
// decidedVia hears the warrants the router says to this module alone: its own `decided` event, a seat event on the seat's
// subject (`mesh.seat.<seat>.event.<event>.<module>`) that only the router publishes (the grant to subscribe it names the
// module). They are handled on the module's one hub over its durable consumer on EVENTS (made with its grant for the seat
// it uses), so an answer the router published while the teller was down is delivered when it is back, never lost (issue
// 1126). hear registers the handler of the subject on the hub; the message is acknowledged once handled. The handler is the
// asker's: pattern, `<seat>.<event>.<module>`, is the key it expects.
func decidedVia(hear func(subject string, handle func(jetstream.Msg)) error, logf func(string, ...any)) func(pattern string, handle func(stdio.Envelope) error) error {
return func(pattern string, handle func(stdio.Envelope) error) error {
seat, event, _ := strings.Cut(pattern, ".")
return hear("mesh.seat."+seat+".event."+event, func(m jetstream.Msg) {
if err := handle(stdio.Envelope{Key: pattern, Body: m.Data()}); err != nil {
logf("a warrant could not be handled and is read again from the router's record at the next start: %v", err)
}
if err := m.Ack(); err != nil {
logf("a warrant was handled but could not be acknowledged: %v", err)
}
})
}
}
// sayVia publishes a condition on the seat's one subject for conditions, under the module's own name.
func sayVia(ctx context.Context, js jetstream.JetStream, module string) func(c oc.Condition) error {
return func(c oc.Condition) error {
subject, err := occ.ConditionSubject(module)
if err != nil {
return err
}
data, err := occ.EncodeCondition(c)
if err != nil {
return err
}
_, err = js.Publish(ctx, subject, data)
return err
}
}
// watchAsks hands each key of the gate's bucket asks, with its revision, to put, and each key taken away to gone, until
// ctx ends or the watch fails. current is called once, when the watch has handed over every key the bucket held at its
// start (the watch's nil mark).
func watchAsks(ctx context.Context, kv jetstream.KeyValue, put func(key string, rev uint64, value []byte), gone func(key string), current func()) error {
w, err := kv.Watch(ctx, ">")
if err != nil {
return err
}
defer w.Stop()
for {
select {
case <-ctx.Done():
return nil
case e, ok := <-w.Updates():
if !ok {
return errors.New("the watch ended")
}
switch {
case e == nil:
if current != nil {
current()
current = nil
}
case e.Operation() == jetstream.KeyValuePut:
put(e.Key(), e.Revision(), e.Value())
default:
gone(e.Key())
}
}
}
}