Files
mesh-sdk/go/process/pullonce.go
T
jschoubben becaf8096a H-A-196: one SDK function claims, pulls and wraps a durable's loop
The tool runner would otherwise copy the claim's wrapper (review round 1, P7):
ConsumeOnce claims the durable, pulls it and answers the loop, and HearEvents
uses it; Conn.Consume claims through the same guard. A drain keeps its claim
until the loop ended, and the loop's Closed is closed only once the claim is
released, so a bind during a drain is refused and one after it is never.

Builds: design 47 H H-A-196, H-A-197
2026-11-20 22:18:43 +01:00

100 lines
3.8 KiB
Go

package process
import (
"fmt"
"runtime"
"strings"
"sync"
"time"
"github.com/nats-io/nats.go/jetstream"
)
// A process pulls a durable consumer once (design 47 H, H-A-196): a second pull loop on a durable the same process
// already pulls takes every other event from the first, so each event reaches one loop only and nothing says so. The SDK
// refuses it, loud at bind time, the error naming the durable and both binders. A durable is told by its stream, its name
// and the time the bus made it, so the same names on two buses (two embedded buses in one test process) are two durables.
var pulled = struct {
sync.Mutex
by map[string]string
}{by: map[string]string{}}
// ConsumeOnce is the one way a part of a module pulls a durable consumer: it claims cons for binder (a short label; empty
// is the calling function's name) and pulls it with handle and opts, as cons.Consume does. A durable this process pulls
// already is refused, the error naming it and both binders. The claim is released when the loop is stopped, when a drain
// has ended, or when the bus ends the loop: the context's Closed is closed only once the claim is released, so a part that
// binds again on it is never refused. A consumer that is not durable is no claim.
func ConsumeOnce(cons jetstream.Consumer, binder string, handle jetstream.MessageHandler, opts ...jetstream.PullConsumeOpt) (jetstream.ConsumeContext, error) {
if binder == "" {
binder = callerOutside()
}
release, err := pullOnce(cons, binder)
if err != nil {
return nil, err
}
cc, err := cons.Consume(handle, opts...)
if err != nil {
release()
return nil, err
}
closed := make(chan struct{})
go func() { <-cc.Closed(); release(); close(closed) }()
return pulledOnce{ConsumeContext: cc, release: release, closed: closed}, nil
}
// pullOnce claims the pull of cons, a durable consumer, for binder: it answers the release, or an error when this process
// already pulls that durable, naming it and both binders.
func pullOnce(cons jetstream.Consumer, binder string) (release func(), err error) {
info := cons.CachedInfo()
if info == nil || info.Config.Durable == "" {
return func() {}, nil
}
key := info.Stream + "/" + info.Name + "@" + info.Created.UTC().Format(time.RFC3339Nano)
pulled.Lock()
defer pulled.Unlock()
if first, ok := pulled.by[key]; ok {
return nil, fmt.Errorf("the durable consumer %s on %s is pulled in this process already, by %s: a second pull loop, by %s, "+
"would take every other event from it, so it is refused (design 47 H, H-A-196)", info.Name, info.Stream, first, binder)
}
pulled.by[key] = binder
var once sync.Once
return func() {
once.Do(func() {
pulled.Lock()
delete(pulled.by, key)
pulled.Unlock()
})
}, nil
}
// ownPackage is this package's path as a function's name begins with it.
const ownPackage = "git.novox.be/novox/mesh-sdk/go/process."
// callerOutside is the name of the first function on the stack outside this package: the part that asked the pull.
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, ownPackage) {
return f.Function
}
if !more {
return f.Function
}
}
}
// pulledOnce is a pull loop whose claim is released as it is stopped, before Stop returns (so a re-serve that stops its old
// loop binds the new one at once), and once a drain or the bus ended the loop, never at the drain's start, so a bind
// during a drain cannot run two loops; its Closed is closed after the release.
type pulledOnce struct {
jetstream.ConsumeContext
release func()
closed chan struct{}
}
func (p pulledOnce) Stop() { p.ConsumeContext.Stop(); p.release() }
func (p pulledOnce) Closed() <-chan struct{} { return p.closed }