Builder: the publish dispatch pulls the live consumer itself, then the again

The dispatch read the live consumer's pending count against items the
pull's callback had not yet handed it, so at one processor an again item
was sent before a live one already delivered. It now fetches the live
consumer without waiting and pulls the again consumer only when that
returns nothing; an item is received when its fetch returns it.

Builds: design 47 H2 §793; H-A-473
This commit is contained in:
2026-11-22 14:14:15 +01:00
parent b914974b1d
commit 1f33a4c04f
@@ -643,38 +643,28 @@ func (pq *publishQueue) pull(ctx context.Context, holder string) error {
if err != nil { if err != nil {
return err return err
} }
chans := map[string]chan jetstream.Msg{} cons := map[string]jetstream.Consumer{}
var live jetstream.Consumer for _, class := range []string{versions.PublishClassStart, versions.PublishClassLive} {
for _, class := range []string{versions.PublishClassStart, versions.PublishClassLive} { // the again first: the live is first by the dispatch, not by luck
c, err := pq.js.CreateOrUpdateConsumer(ctx, b.Stream, jetstream.ConsumerConfig{Durable: publishesConsumer(holder, class), c, err := pq.js.CreateOrUpdateConsumer(ctx, b.Stream, jetstream.ConsumerConfig{Durable: publishesConsumer(holder, class),
FilterSubject: b.Subject + ".publish." + versions.EscapeToken(holder) + "." + class + ".>", AckPolicy: jetstream.AckExplicitPolicy, FilterSubject: b.Subject + ".publish." + versions.EscapeToken(holder) + "." + class + ".>", AckPolicy: jetstream.AckExplicitPolicy,
AckWait: pq.ackWait, MaxAckPending: publishesMaxAckPending, MaxDeliver: publishesMaxDeliver}) AckWait: pq.ackWait, MaxAckPending: publishesMaxAckPending, MaxDeliver: publishesMaxDeliver})
if err != nil { if err != nil {
return fmt.Errorf("the consumer %s: %w", publishesConsumer(holder, class), err) return fmt.Errorf("the consumer %s: %w", publishesConsumer(holder, class), err)
} }
ch := make(chan jetstream.Msg, publishesMaxAckPending) cons[class] = c
cc, err := c.Consume(func(m jetstream.Msg) { ch <- m }, jetstream.PullMaxMessages(publishesMaxAckPending))
if err != nil {
return fmt.Errorf("the consumer %s is not pulled: %w", publishesConsumer(holder, class), err)
}
go func() { <-ctx.Done(); cc.Stop() }()
chans[class] = ch
if class == versions.PublishClassLive {
live = c
}
}
liveWaits := func() bool {
info, err := live.Info(ctx)
return err == nil && info.NumPending > 0
} }
pq.logf("pulling the publishes of %s, the live first", holder) pq.logf("pulling the publishes of %s, the live first", holder)
go pq.dispatch(ctx, chans[versions.PublishClassLive], chans[versions.PublishClassStart], liveWaits) go pq.dispatch(ctx, holder, cons[versions.PublishClassLive], cons[versions.PublishClassStart])
return nil return nil
} }
// dispatch runs the items of one holder, at most four at once, every live item before an again one: an again item pulled while // dispatch runs the items of one holder, at most four at once, every live item before an again one (design 47 H2 §793,
// a live one waits on the queue (liveWaits) runs after it. // H-A-473): it pulls the live consumer itself, a fetch of up to its free slots that does not wait, and pulls the again consumer
func (pq *publishQueue) dispatch(ctx context.Context, live, again <-chan jetstream.Msg, liveWaits func() bool) { // only when that fetch returns nothing; an item is received the moment its fetch returns it, so no live item is delivered and
// not yet received while an again one is chosen. When neither holds an item, a pull of one item waits on each, and what it
// receives is run, the live first. A pull the bus refuses ends the dispatch, said; the holder is pulled again when an item of
// it is put.
func (pq *publishQueue) dispatch(ctx context.Context, holder string, live, again jetstream.Consumer) {
sem := make(chan struct{}, versions.PublishSendsAtOnce) sem := make(chan struct{}, versions.PublishSendsAtOnce)
run := func(m jetstream.Msg) { run := func(m jetstream.Msg) {
go func() { go func() {
@@ -682,48 +672,142 @@ func (pq *publishQueue) dispatch(ctx context.Context, live, again <-chan jetstre
pq.take(ctx, m) pq.take(ctx, m)
}() }()
} }
var spare jetstream.Msg // an again item pulled while a live one waited failed := func(err error) {
pq.logf("the publishes of %s are not pulled: %v; they are pulled when an item of it is put", holder, err)
pq.mu.Lock()
delete(pq.pulling, holder)
pq.mu.Unlock()
}
refused := func(err error) bool {
return err != nil && !errors.Is(err, jetstream.ErrNoMessages) && !errors.Is(err, nats.ErrTimeout) && ctx.Err() == nil
}
now := func(c jetstream.Consumer, n int) ([]jetstream.Msg, error) {
b, err := c.FetchNoWait(n)
if err != nil {
return nil, err
}
var got []jetstream.Msg
for m := range b.Messages() {
got = append(got, m)
}
return got, b.Error()
}
type waited struct {
m jetstream.Msg
err error
}
liveDone, againDone := make(chan waited, 1), make(chan waited, 1)
wait := func(c jetstream.Consumer, done chan<- waited) {
go func() {
var w waited
b, err := c.Fetch(1)
if err == nil {
for m := range b.Messages() {
w.m = m
}
err = b.Error()
}
w.err = err
done <- w
}()
}
var heldLive, heldAgain jetstream.Msg // an item a waiting pull received
var liveWaiting, againWaiting bool
heard := func(w waited, held *jetstream.Msg, waiting *bool) bool {
*waiting = false
if refused(w.err) {
failed(w.err)
return false
}
if w.m != nil {
*held = w.m
}
return true
}
for { for {
select { select {
case sem <- struct{}{}: case sem <- struct{}{}:
case <-ctx.Done(): case <-ctx.Done():
return return
} }
select { n := 1
case m := <-live: slots:
run(m) for n < cap(sem) {
continue
default:
}
m := spare
spare = nil
if m == nil {
select { select {
case lm := <-live: case sem <- struct{}{}:
run(lm) n++
continue default:
case m = <-again: break slots
case <-ctx.Done():
return
} }
} }
if liveWaits() { select {
spare = m case w := <-liveDone:
select { if !heard(w, &heldLive, &liveWaiting) {
case lm := <-live:
run(lm)
case <-ctx.Done():
return return
} }
continue default:
} }
select { select {
case lm := <-live: // came while the queue was asked case w := <-againDone:
spare = m if !heard(w, &heldAgain, &againWaiting) {
run(lm) return
}
default: default:
}
var got []jetstream.Msg
switch {
case heldLive != nil:
got, heldLive = []jetstream.Msg{heldLive}, nil
case !liveWaiting:
ms, err := now(live, n)
if refused(err) {
failed(err)
return
}
got = ms
}
if len(got) == 0 {
switch {
case heldAgain != nil:
got, heldAgain = []jetstream.Msg{heldAgain}, nil
case !againWaiting:
ms, err := now(again, n)
if refused(err) {
failed(err)
return
}
got = ms
}
}
for _, m := range got {
run(m) run(m)
} }
for range n - len(got) {
<-sem
}
if len(got) > 0 {
continue
}
if !liveWaiting {
liveWaiting = true
wait(live, liveDone)
}
if !againWaiting {
againWaiting = true
wait(again, againDone)
}
select {
case w := <-liveDone:
if !heard(w, &heldLive, &liveWaiting) {
return
}
case w := <-againDone:
if !heard(w, &heldAgain, &againWaiting) {
return
}
case <-ctx.Done():
return
}
} }
} }