Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3bc7da52e4 | ||
|
|
bc138366af | ||
|
|
4a5eb26f4e | ||
|
|
173a8c120d | ||
|
|
becaf8096a | ||
|
|
c1c369f50c |
@@ -5,6 +5,8 @@
|
||||
// allow. It answers its seat's verbs through AnswerSeat alone (design 47 G, lead answer A-107). A sibling of stdio,
|
||||
// which it imports only for stdio's pure types (ErrChanged, CodeChanged, StateEntry, EmitOptions); it never
|
||||
// touches stdio's current, channel or live(), which stay the bundle's own way to the bus.
|
||||
//
|
||||
// Design 47 H1, proof 2: the SDK and its importer delivered in one merge call, 2026-11-21.
|
||||
package process
|
||||
|
||||
import (
|
||||
|
||||
@@ -289,4 +289,88 @@ func TestTheHubBindsALateConsumerAndSaysItIsLive(t *testing.T) {
|
||||
t.Run("AModuleHearsASeatEventKeptOnItsOwnStream", subAModuleHearsASeatEventKeptOnItsOwnStream)
|
||||
t.Run("AModuleHearsAnotherModulesStreamKeptLastPerSubject", subAModuleHearsAnotherModulesStreamKeptLastPerSubject)
|
||||
t.Run("AModuleHearsAStreamKeptByAgeFromItsFirstMessage", subAModuleHearsAStreamKeptByAgeFromItsFirstMessage)
|
||||
t.Run("AProcessPullsADurableOnce", subAProcessPullsADurableOnce)
|
||||
}
|
||||
|
||||
// A second pull loop on a durable this process already pulls is refused at bind time, naming the durable and both
|
||||
// binders, and the durable is pulled again once the first loop stopped (design 47 H, H-A-196).
|
||||
func subAProcessPullsADurableOnce(t *testing.T) {
|
||||
nc, err := nats.Connect(testbus.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
js, err := jetstream.New(nc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: process.EventsStream, Subjects: []string{"mesh.mod.*.event.>"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cred := process.Account{Node: "n1", Module: "twice"}
|
||||
if _, err := js.CreateOrUpdateConsumer(ctx, process.EventsStream, jetstream.ConsumerConfig{Durable: cred.EventConsumerName(), AckPolicy: jetstream.AckExplicitPolicy}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cons, err := cred.EventConsumer(ctx, js, process.EventsStream)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := process.HearEvents(cons, func(m jetstream.Msg) { _ = m.Ack() }, t.Logf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = process.ConsumeOnce(cons, "the follower", func(jetstream.Msg) {})
|
||||
if err == nil {
|
||||
t.Fatal("a second pull loop on the durable the process pulls was taken")
|
||||
}
|
||||
for _, want := range []string{cred.EventConsumerName(), process.EventsStream, "subAProcessPullsADurableOnce", "the follower"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Fatalf("the refusal %q does not name %q", err, want)
|
||||
}
|
||||
}
|
||||
if _, err := process.HearEvents(cons, func(jetstream.Msg) {}, t.Logf); err == nil {
|
||||
t.Fatal("a second HearEvents on the durable the process pulls was taken")
|
||||
}
|
||||
first.Stop()
|
||||
again, err := process.HearEvents(cons, func(m jetstream.Msg) { _ = m.Ack() }, t.Logf)
|
||||
if err != nil {
|
||||
t.Fatalf("the durable is not pulled again after its loop stopped: %v", err)
|
||||
}
|
||||
// A drain keeps the claim until the loop ended: a bind during it is refused, one after it (Closed) is taken.
|
||||
held := make(chan struct{})
|
||||
slow, err := cred.EventConsumer(ctx, js, process.EventsStream)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again.Stop()
|
||||
handed := make(chan struct{}, 2)
|
||||
draining, err := process.ConsumeOnce(slow, "the drained loop", func(m jetstream.Msg) { handed <- struct{}{}; <-held; _ = m.Ack() })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := js.Publish(ctx, "mesh.mod.alpha.event.happened", []byte(`{}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := js.Publish(ctx, "mesh.mod.alpha.event.happened", []byte(`{}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-handed:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("the drained loop was handed no message")
|
||||
}
|
||||
draining.Drain()
|
||||
if _, err := process.ConsumeOnce(slow, "a bind during the drain", func(jetstream.Msg) {}); err == nil ||
|
||||
!strings.Contains(err.Error(), "the drained loop") {
|
||||
t.Fatalf("a bind while the drain runs was not refused naming the draining loop: %v", err)
|
||||
}
|
||||
close(held)
|
||||
<-draining.Closed()
|
||||
after, err := process.ConsumeOnce(slow, "a bind after the drain", func(m jetstream.Msg) { _ = m.Ack() })
|
||||
if err != nil {
|
||||
t.Fatalf("a bind after the drain ended was refused: %v", err)
|
||||
}
|
||||
after.Stop()
|
||||
}
|
||||
|
||||
@@ -104,9 +104,10 @@ func (a Account) namedEventConsumerName(suffix string) string {
|
||||
}
|
||||
|
||||
// HearEvents pulls from cons, handing each message to handle, and says every error the bus reports while
|
||||
// hearing through logf ("hearing the bus: ..."). The caller stops the returned context.
|
||||
// hearing through logf ("hearing the bus: ..."). The caller stops the returned context. A durable this process pulls
|
||||
// already is refused (ConsumeOnce, design 47 H, H-A-196).
|
||||
func HearEvents(cons jetstream.Consumer, handle func(jetstream.Msg), logf func(string, ...any)) (jetstream.ConsumeContext, error) {
|
||||
return cons.Consume(handle, jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) {
|
||||
return ConsumeOnce(cons, callerOutside(), handle, jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) {
|
||||
logf("hearing the bus: %v", err)
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
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 }
|
||||
@@ -71,6 +71,11 @@ func (c *Conn) Consume(ctx context.Context, stream string, handle func(*Message)
|
||||
if err != nil {
|
||||
return fmt.Errorf("binding %s's queue consumer %s on %s: %w", c.account.Module, name, stream, err)
|
||||
}
|
||||
release, err := pullOnce(cons, callerOutside())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer release()
|
||||
return runQueue(ctx, fetchOne(cons), handle)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -90,15 +91,23 @@ func TestConsumeAckRemovesTheMessageFromTheStream(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
got := make(chan *Message, 1)
|
||||
got, second := make(chan *Message, 1), make(chan error, 1)
|
||||
go func() {
|
||||
_ = conn.Consume(ctx, "MAIL_SENDS", func(m *Message) {
|
||||
// A second pull loop on the durable this process pulls is refused at bind time (design 47 H, H-A-196).
|
||||
short, stop := context.WithTimeout(context.Background(), time.Second)
|
||||
second <- conn.Consume(short, "MAIL_SENDS", func(*Message) {})
|
||||
stop()
|
||||
got <- m
|
||||
cancel()
|
||||
})
|
||||
}()
|
||||
msg := recv(t, got, 8*time.Second, "the message")
|
||||
<-ctx.Done()
|
||||
if err := <-second; err == nil || !strings.Contains(err.Error(), "pulled in this process already") ||
|
||||
!strings.Contains(err.Error(), account.QueueConsumerName()) {
|
||||
t.Fatalf("a second Consume of the queue the process pulls was not refused naming its durable: %v", err)
|
||||
}
|
||||
if err := msg.Ack(); err != nil {
|
||||
t.Fatalf("Ack: %v", err)
|
||||
}
|
||||
|
||||
@@ -154,7 +154,9 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) {
|
||||
hub := process.NewEventHub(t.Logf)
|
||||
got := map[string]chan string{"a": make(chan string, 8), "b": make(chan string, 8)}
|
||||
for name, pattern := range map[string]string{"a": "mesh.mod.p.event.a", "b": "mesh.mod.q.event.b.*"} {
|
||||
if err := hub.Handle(pattern, func(m jetstream.Msg) { _ = m.Ack(); got[name] <- m.Subject() }); err != nil {
|
||||
// The handlers' acknowledgement waits for the bus's answer, so a message a handler handed on is acknowledged
|
||||
// on the bus, and every one the hub handed before it too (acks of one consumer are taken in order).
|
||||
if err := hub.Handle(pattern, func(m jetstream.Msg) { _ = m.DoubleAck(ctx); got[name] <- m.Subject() }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -230,12 +232,25 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) {
|
||||
if len(got["a"])+len(got["b"])+len(seatHeard) != 0 {
|
||||
t.Fatal("a handler heard a message that was another's, or one twice")
|
||||
}
|
||||
// The reader acknowledges an event after it took it into its picture and the adopted handler returned, so the
|
||||
// picture and the handler say nothing of its ack: one more event, handed after them in order and acknowledged with
|
||||
// the bus's answer, is the mark that every ack before it has landed (the build node, main.54, read one pending). An
|
||||
// event said after the mark (the owner's own mark of its burst) may still wait for its ack, so the floor is read.
|
||||
mark, err := js.Publish(ctx, "mesh.mod.p.event.a", []byte(`"the mark"`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-got["a"]:
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("the mark after the handled events was not heard")
|
||||
}
|
||||
cons, err := js.Consumer(ctx, process.EventsStream, cred.EventConsumerName())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info, err := cons.Info(ctx); err != nil || info.NumAckPending != 0 {
|
||||
t.Fatalf("a message was left unacknowledged: %+v %v", info, err)
|
||||
if info, err := cons.Info(ctx); err != nil || info.AckFloor.Stream < mark.Sequence {
|
||||
t.Fatalf("a message up to the mark %d was left unacknowledged: %+v %v", mark.Sequence, info, err)
|
||||
}
|
||||
// H-A-169: the one event no handler takes is the module's finding event-unhandled, counted once, naming its subject,
|
||||
// though the reader started twice.
|
||||
|
||||
Reference in New Issue
Block a user