Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/filledread.go
T
jschoubben 218d5cb67f Builder: read the last versions-filled at start from its kept stream, not EVENTS
The seat event's own stream is heard from its first message through the SDK's HearSeatEvents with FromFirst; nothing of EVENTS is read and no new grant is needed.

Builds: design 47 G A-411 (replaces A-408's read of EVENTS)
2026-11-14 23:39:06 +01:00

88 lines
3.1 KiB
Go

package main
import (
"context"
"errors"
"sync/atomic"
"time"
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/process"
)
// lastKept reads whether the stream keeps a message on a subject: found false when the stream or the message is not there.
type lastKept func(ctx context.Context, subject string) (found bool, err error)
// lastKeptOf reads the seat event's own kept stream (the contract's StreamVersionsFilled, kept by age) the way any kept event
// is heard (design 47 G A-411, A-233): the SDK's hearing of it from its first message, whose start read hands every message
// kept to the handler; the reading is stopped once the read ended, the hub's own hearing following the durable. Nothing of
// EVENTS is read.
func lastKeptOf(js jetstream.JetStream, acct process.Account, logf func(string, ...any)) lastKept {
return func(ctx context.Context, subject string) (bool, error) {
var found atomic.Bool
cc, err := acct.HearSeatEvents(ctx, js, meshcatalog.StreamVersionsFilled, func(m jetstream.Msg) {
if m.Subject() == subject {
found.Store(true)
}
_ = m.Ack() // a message of the durable is taken as the hub takes it; an ordered read's has nothing to acknowledge
}, logf, process.FromFirst())
if err != nil {
if errors.Is(err, jetstream.ErrStreamNotFound) {
return false, nil
}
return false, err
}
cc.Stop()
return found.Load(), nil
}
}
// readFilled reads at the builder's start the last versions-filled the stream keeps (design 47 G §369, §372, A-411) and takes it as heard; the event heard later sets it as before. Nothing found leaves it unset, said; a
// read that failed is said and tried again in the background with backoff, never holding the start.
func (rv *reservations) readFilled(ctx context.Context, read lastKept, wait func(context.Context, time.Duration) bool) {
const first, most = time.Second, time.Minute
try := func() (done bool, err error) {
found, err := read(ctx, meshcatalog.SubjectVersionsFilled)
if err != nil {
return false, err
}
if !found {
rv.logf("the stream keeps no versions-filled yet (read at start): versions are reserved once the catalog says it")
return true, nil
}
rv.filled.Store(true)
rv.logf("the catalog said versions-filled (read at start): versions are reserved from now on")
if rv.wake != nil {
rv.wake(ctx)
}
return true, nil
}
if done, err := try(); done {
return
} else {
rv.logf("the last versions-filled is not read at start: %v; read again in the background", err)
}
go func() {
for d := first; ; d = min(d*2, most) {
if !wait(ctx, d) {
return
}
done, err := try()
if done {
return
}
rv.logf("the last versions-filled is not read: %v; read again in %s", err, min(d*2, most))
}
}()
}
// sleeps waits d, as a context's bound (ADR 0437 §8: the builder holds no timer); false when ctx ended first.
func sleeps(ctx context.Context, d time.Duration) bool {
bound, cancel := context.WithTimeout(ctx, d)
defer cancel()
<-bound.Done()
return ctx.Err() == nil
}