From 6dd3141c5ebedc6ed73aa4b0de78ef34f23aa264 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 18:50:23 +0100 Subject: [PATCH] hearHeld paces its pull at the kept-call pace and says each kind of error once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 47 H2 §775, H-A-695 (2), H-A-733 --- go/process/consumers_test.go | 23 ++++++++++++++++++++++ go/process/module.go | 37 ++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/go/process/consumers_test.go b/go/process/consumers_test.go index 2044289d..329bc58c 100644 --- a/go/process/consumers_test.go +++ b/go/process/consumers_test.go @@ -3,9 +3,11 @@ package process import ( "context" "encoding/json" + "errors" "testing" "time" + "git.novox.be/novox/mesh-sdk/go/busretry" "github.com/nats-io/nats.go/jetstream" ) @@ -65,3 +67,24 @@ func TestConsumerLedgerKeepsTheFactsAfterEachMessage(t *testing.T) { t.Errorf("after both applied: %+v, want sequence 8 and none unacknowledged", f) } } + +// Design 47 H2 §775: the held pull waits after an error at the kept-call pace, doubling to its most, and says each kind of +// error once until a message arrives. +func TestHeldPullPacesAtTheKeptCallPaceAndSaysEachKindOnce(t *testing.T) { + pace := busretry.Pace{First: time.Second, Max: 3 * time.Second} + var f pullFaults + a, b := errors.New("a"), errors.New("b") + for i, row := range []struct { + err error + say bool + wait time.Duration + }{{a, true, time.Second}, {a, false, 2 * time.Second}, {b, true, 3 * time.Second}, {a, false, 3 * time.Second}} { + if say, wait := f.next(row.err, pace); say != row.say || wait != row.wait { + t.Errorf("error %d: said %v, waited %v; want %v, %v", i, say, wait, row.say, row.wait) + } + } + f.ended() + if say, wait := f.next(a, pace); !say || wait != time.Second { + t.Errorf("after a message: said %v, waited %v", say, wait) + } +} diff --git a/go/process/module.go b/go/process/module.go index 5c767720..18931643 100644 --- a/go/process/module.go +++ b/go/process/module.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "git.novox.be/novox/mesh-sdk/go/busretry" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" ) @@ -155,6 +156,7 @@ func hearHeld(cons jetstream.Consumer, binder string, hold *eventHold, handle fu pulled := &heldPull{it: it, closed: make(chan struct{})} go func() { defer func() { release(); close(pulled.closed) }() + var faults pullFaults for { hold.waitRoom() m, err := it.Next() @@ -162,16 +164,47 @@ func hearHeld(cons jetstream.Consumer, binder string, hold *eventHold, handle fu if errors.Is(err, jetstream.ErrMsgIteratorClosed) { return } - logf("hearing the bus: %v", err) - time.Sleep(time.Second) + say, wait := faults.next(err, busretry.KeptPace()) + if say { + logf("hearing the bus: %v", err) + } + time.Sleep(wait) continue } + faults.ended() handle(m) } }() return pulled, nil } +// pullFaults paces hearHeld's pull after an error at the kept-call pace (busretry.KeptPace: the settings +// kept-call-pace-first and kept-call-pace-max), doubling from the first to the most, and says each kind of error once +// until a message arrives (design 47 H2 §775, principle 11). +type pullFaults struct { + wait time.Duration + said map[string]bool +} + +// next is the wait after err and whether err is of a kind not yet said. +func (f *pullFaults) next(err error, pace busretry.Pace) (bool, time.Duration) { + if f.wait <= 0 { + f.wait = pace.First + } else { + f.wait = min(f.wait*2, pace.Max) + } + if f.said == nil { + f.said = map[string]bool{} + } + kind := err.Error() + say := !f.said[kind] + f.said[kind] = true + return say, f.wait +} + +// ended is a message pulled: the pace and what was said start again. +func (f *pullFaults) ended() { *f = pullFaults{} } + // heldPull is the pull of hearHeld as a ConsumeContext. type heldPull struct { it jetstream.MessagesContext