Files
mesh-tools/node-tools/internal/bus/queue.go
T
jschoubben a6a16c897b T4: a waiting read lists an ask handed out and not acknowledged, marked again
mesh/queue.waiting reads from each worker's acknowledgement floor, so an ask a worker holds or
handed back is listed with again: true; the holder tells the two apart by its own state taken.

Builds: design 47 H2 §777, H-A-410 (second addendum)
2026-11-22 01:01:59 +01:00

104 lines
3.5 KiB
Go

package bus
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/nats-io/nats.go/jetstream"
)
// QueueWaitingMost is the most a waiting read holds, in its asks' ids and modules (design 47 H2 H-A-410).
const QueueWaitingMost = 768 << 10
// WaitingAsk is one ask that waits on a seat's work queue: its id (the publisher's own, as it published it), the module that
// put it and how long it has waited.
type WaitingAsk struct {
ID string `json:"id"`
Module string `json:"module"`
AgeSeconds int64 `json:"age_seconds"`
// Again says the ask was handed to a worker at least once and not acknowledged: held now, or handed back to wait again;
// the holder tells the two apart by its own state taken (H-A-410, second addendum).
Again bool `json:"again,omitempty"`
}
// QueueWaiting is what waits on a seat's work queue, oldest first, and whether the read stopped at QueueWaitingMost.
type QueueWaiting struct {
Asks []WaitingAsk `json:"asks"`
Capped bool `json:"capped,omitempty"`
}
// seatStream is the work-queue stream a seat's asks are kept on.
func seatStream(seat string) string {
return "SEAT_" + strings.ToUpper(strings.ReplaceAll(seat, "-", "_"))
}
// QueueWaitingOf reads, without consuming, the asks that wait on the seat's work queue for module, the holder whose
// membership lists the queue's workers: its own store (ADR 0596). For each of its workers on the seat's stream, every message
// after the worker's acknowledgement floor is read by a direct get by subject, never by a consumer of its own; one at or
// below the worker's delivered sequence is marked again (design 47 H2 H-A-410 and its addenda).
func (c *Conn) QueueWaitingOf(module, seat string) (QueueWaiting, error) {
t, err := c.trafficOf(module)
if err != nil {
return QueueWaiting{}, err
}
stream := seatStream(seat)
var workers []SeatWorker
for _, w := range t.Workers {
if w.Stream == stream {
workers = append(workers, w)
}
}
if len(workers) == 0 {
return QueueWaiting{}, fmt.Errorf("%s holds no work queue of the seat %s: its membership lists no worker on %s "+
"(novox/hq ADR 0596: a holder reads its own queue)", module, seat, stream)
}
js, err := jetstream.New(c.nc)
if err != nil {
return QueueWaiting{}, err
}
ctx, cancel := context.WithTimeout(context.Background(), StateTimeout)
defer cancel()
kept, err := js.Stream(ctx, stream)
if err != nil {
return QueueWaiting{}, fmt.Errorf("reading %s's queue %s: %w", module, stream, err)
}
out := QueueWaiting{Asks: []WaitingAsk{}}
size, now := 0, time.Now()
for _, w := range workers {
bound, err := c.pullConsumer(module, w)
if err != nil {
return QueueWaiting{}, err
}
info, err := bound.Info(ctx)
if err != nil {
return QueueWaiting{}, fmt.Errorf("reading %s's worker %s: %w", module, w.Consumer, err)
}
filter := w.Filter
if filter == "" {
filter = ">"
}
for seq := info.AckFloor.Stream + 1; ; {
m, err := kept.GetMsg(ctx, seq, jetstream.WithGetMsgSubject(filter))
if errors.Is(err, jetstream.ErrMsgNotFound) {
break
}
if err != nil {
return QueueWaiting{}, fmt.Errorf("reading %s's queue %s: %w", module, stream, err)
}
seq = m.Sequence + 1
ask := WaitingAsk{Module: m.Header.Get("x-source"), AgeSeconds: int64(now.Sub(m.Time) / time.Second),
Again: m.Sequence <= info.Delivered.Stream}
ask.ID = strings.TrimPrefix(m.Header.Get("x-event-id"), ask.Module+".")
if size += len(ask.ID) + len(ask.Module) + 48; size > QueueWaitingMost {
out.Capped = true
return out, nil
}
out.Asks = append(out.Asks, ask)
}
}
return out, nil
}