H-A-197: the runner pulls through the SDK's ConsumeOnce, keeping no copy

Review round 1 (P7): the runner's bind copied the SDK's claim wrapper and its
caller lookup. It now calls process.ConsumeOnce, which claims, pulls and
releases (at a stop, or once a drain or the bus ended the loop, before its
Closed is closed), naming the claim for the module whose hearing it is.

Builds: design 47 H H-A-197, H-A-196
This commit is contained in:
2026-11-20 22:20:07 +01:00
parent c952386737
commit 876adfbf05
+6 -46
View File
@@ -3,8 +3,6 @@ package bus
import (
"context"
"errors"
"fmt"
"runtime"
"sort"
"strings"
"sync"
@@ -88,9 +86,6 @@ type hearing struct {
js jetstream.JetStream
module string
deliver func(Envelope) error
// by is the part that asked this hearing (the first caller outside this package), named in the claim of each durable
// it pulls (design 47 H, H-A-196, H-A-197).
by string
// serial hands the module one event at a time across its consumers (novox/hq issue 387).
serial sync.Mutex
@@ -114,7 +109,7 @@ type hearing struct {
}
func newHearing(c *Conn, js jetstream.JetStream, module string, deliver func(Envelope) error) *hearing {
return &hearing{c: c, js: js, module: module, deliver: deliver, by: callerOutside(),
return &hearing{c: c, js: js, module: module, deliver: deliver,
bound: map[HeardIssued]jetstream.ConsumeContext{}, made: map[HeardIssued]time.Time{}, said: map[HeardIssued]bool{}, why: map[HeardIssued]error{},
kick: make(chan struct{}, 1), done: make(chan struct{})}
}
@@ -136,48 +131,14 @@ func (h *hearing) bind(stream, durable string, onErr func(error)) (jetstream.Con
if onErr != nil {
opts = append(opts, jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) { onErr(err) }))
}
// Claimed through the SDK's one guard before it is pulled (design 47 H, H-A-196, H-A-197): a second pull loop on a
// durable this process pulls already would take every other event from the first, so it is refused here, loud.
release, err := process.PullOnce(consumer, fmt.Sprintf("the tool runner's hearing of %s, asked by %s", h.module, h.by))
// Claimed through the SDK's one guard as it is pulled (design 47 H, H-A-196, H-A-197): a second pull loop on a durable
// this process pulls already would take every other event from the first, so it is refused here, loud. The claim is
// released as the loop is stopped, or once the bus ended it, before its Closed is closed.
reading, err := process.ConsumeOnce(consumer, "the tool runner's hearing of "+h.module, h.handle, opts...)
if err != nil {
return nil, time.Time{}, "claiming", err
}
reading, err := consumer.Consume(h.handle, opts...)
if err != nil {
release()
return nil, time.Time{}, "reading", err
}
go func() { <-reading.Closed(); release() }()
return pulled{ConsumeContext: reading, release: release}, created, "", nil
}
// pulled is a pull loop whose claim (process.PullOnce) is released as it is stopped or drained, before the call returns,
// so a re-serve that stops its old loop binds the new one without a refusal; a loop the bus ended is released by release.
type pulled struct {
jetstream.ConsumeContext
release func()
}
func (p pulled) Stop() { p.ConsumeContext.Stop(); p.release() }
func (p pulled) Drain() { p.ConsumeContext.Drain(); p.release() }
// released releases the claim of a loop the bus ended, before it is bound again.
func released(reading jetstream.ConsumeContext) {
if p, ok := reading.(pulled); ok {
p.release()
}
}
// callerOutside is the first function on the stack outside this package: the part that asked the hearing.
func callerOutside() string {
pcs := make([]uintptr, 16)
frames := runtime.CallersFrames(pcs[:runtime.Callers(3, pcs)])
for {
f, more := frames.Next()
if !strings.HasPrefix(f.Function, "github.com/novox/mesh-tools/node-tools/internal/bus.") || !more {
return f.Function
}
}
return reading, created, "", nil
}
// handle hands one delivered event to the module: acknowledged when it took it, offered again after NakDelay
@@ -359,7 +320,6 @@ func (h *hearing) reconcile() {
for heard, reading := range h.bound {
select {
case <-reading.Closed():
released(reading)
delete(h.bound, heard)
delete(h.made, heard)
h.whyMu.Lock()