S6 event-hub-by-key: the pull takes one message at a time and only while the hub has room

Builds: design 47 H2 H-A-695 (2), H-A-733
This commit is contained in:
2026-11-25 17:19:38 +01:00
parent 065e73e1dd
commit 7ff8e07d2a
5 changed files with 104 additions and 8 deletions
+41 -6
View File
@@ -139,14 +139,49 @@ func hearEvents(cons jetstream.Consumer, binder string, handle func(jetstream.Ms
}
// hearHeld is HearEvents for a handler that holds each message and returns (the event hub's, event_hold.go): the pull says
// nothing in progress itself, since the hub's ticker of the durable does for every message held.
func hearHeld(cons jetstream.Consumer, binder string, handle func(jetstream.Msg), logf func(string, ...any)) (jetstream.ConsumeContext, error) {
return ConsumeOnce(cons, binder, handle, jetstream.PullMaxMessages(hubPullMessages),
jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) {
logf("hearing the bus: %v", err)
}))
// nothing in progress itself, since the hub's ticker of the durable does for every message held. It pulls one message at a
// time and only while the hub holds fewer than events-waiting, so no delivered message is left outside the hold: the rest
// wait on the bus, not yet given (design 47 H2 H-A-695 (2), H-A-733).
func hearHeld(cons jetstream.Consumer, binder string, hold *eventHold, handle func(jetstream.Msg), logf func(string, ...any)) (jetstream.ConsumeContext, error) {
release, err := pullOnce(cons, binder)
if err != nil {
return nil, err
}
it, err := cons.Messages(jetstream.PullMaxMessages(1))
if err != nil {
release()
return nil, err
}
pulled := &heldPull{it: it, closed: make(chan struct{})}
go func() {
defer func() { release(); close(pulled.closed) }()
for {
hold.waitRoom()
m, err := it.Next()
if err != nil {
if errors.Is(err, jetstream.ErrMsgIteratorClosed) {
return
}
logf("hearing the bus: %v", err)
time.Sleep(time.Second)
continue
}
handle(m)
}
}()
return pulled, nil
}
// heldPull is the pull of hearHeld as a ConsumeContext.
type heldPull struct {
it jetstream.MessagesContext
closed chan struct{}
}
func (p *heldPull) Stop() { p.it.Stop() }
func (p *heldPull) Drain() { p.it.Drain() }
func (p *heldPull) Closed() <-chan struct{} { return p.closed }
// 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 {