Pull small batches in the event hub and keep slow handlers alive (H-A-281)

The hub pulled the bus client's default 500 messages per pull and handled
them one by one, so buffered messages outlived the 30 s ack wait and were
given again and handled twice (met live on the builder: 37 of 38
outstanding redelivered). HearEvents, the one pull of the hub and of
HearStream, now pulls at most twenty messages per pull and says in
progress for a message whose handler runs past a third of the consumer's
ack wait, read once from the bound consumer's information.

Builds: design 47 H H-A-281
This commit is contained in:
2026-11-21 16:48:05 +01:00
parent dfcf2147bc
commit fedf9d2e7b
2 changed files with 135 additions and 3 deletions
+54 -3
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"strings"
"time"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
@@ -106,8 +107,58 @@ 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. A durable this process pulls
// already is refused (ConsumeOnce, design 47 H, H-A-196).
//
// It pulls at most hubPullMessages messages per pull, and says in progress to the bus for a message whose handler runs
// past a third of the consumer's ack wait, again at each further third until the handler returns (design 47 H, H-A-281):
// handlers run one by one, so a large pull's buffered messages would outlive the ack wait and be given again. The ack
// wait is read once, from the consumer's information as the bind read it.
func HearEvents(cons jetstream.Consumer, handle func(jetstream.Msg), logf func(string, ...any)) (jetstream.ConsumeContext, error) {
return ConsumeOnce(cons, callerOutside(), handle, jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) {
logf("hearing the bus: %v", err)
}))
return hearEvents(cons, callerOutside(), handle, logf, realTicker)
}
// hubPullMessages is how many messages the event hub pulls at most per pull (design 47 H, H-A-281).
const hubPullMessages = 20
// ticker answers a channel ticking every d and its stop.
type ticker func(d time.Duration) (<-chan time.Time, func())
func realTicker(d time.Duration) (<-chan time.Time, func()) {
t := time.NewTicker(d)
return t.C, t.Stop
}
func hearEvents(cons jetstream.Consumer, binder string, handle func(jetstream.Msg), logf func(string, ...any), tick ticker) (jetstream.ConsumeContext, error) {
var ackWait time.Duration
if info := cons.CachedInfo(); info != nil {
ackWait = info.Config.AckWait
}
return ConsumeOnce(cons, binder, keptInProgress(handle, ackWait/3, tick), jetstream.PullMaxMessages(hubPullMessages),
jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) {
logf("hearing the bus: %v", err)
}))
}
// keptInProgress is handle that says in progress for its message at every tick of every while the handler runs, the
// ticker stopped at the handler's return (design 47 H, H-A-281); with no every it is handle as it is.
func keptInProgress(handle func(jetstream.Msg), every time.Duration, tick ticker) jetstream.MessageHandler {
if every <= 0 {
return handle
}
return func(m jetstream.Msg) {
ticks, stop := tick(every)
done, ended := make(chan struct{}), make(chan struct{})
go func() {
defer close(ended)
for {
select {
case <-done:
return
case <-ticks:
_ = m.InProgress()
}
}
}()
defer func() { close(done); stop(); <-ended }()
handle(m)
}
}