Tool runner bus: pulled workers belong to their connection; two tests wait for what they assert

A worker bound for a pull was kept for the process, so a later connection's pull
used a worker bound on a closed connection and answered "connection closed"
(every run of TestAHolderPullsOneMessageWhenItHasRoom after the first). The
bound workers and the pulled messages are now the Conn's.

The two tests that failed main.165 and main.115 asserted on bus figures before
the bus had delivered them: the worker's pending count right after a publish, a
no-wait pull right after a publish, a callback counter read when a flush
returned, a 300 ms delay and a 2 s ack wait a loaded machine overruns. They now
count binds on a channel the watcher's reader fills, pull with a wait that ends
at the delivery, prove "nothing fetched ahead" by the second message's first
delivery, and use bounds a loaded machine stays within.

Builds: design 47 H H-A-363 (fix-a-bug-met-twice; whole-module tests under load, H-A-327)
This commit is contained in:
2026-11-21 23:39:25 +01:00
parent 02675a43ea
commit a75d86236b
5 changed files with 55 additions and 37 deletions
+26 -20
View File
@@ -33,8 +33,10 @@ type Pulled struct {
Delivered uint64 `json:"delivered"`
}
// pulls are the messages modules pulled and have not settled, by id; each kept with the module it was pulled for, so
// one module never settles another's.
// pulls are the messages modules pulled on one connection and have not settled, by id, each kept with the module it was
// pulled for, so one module never settles another's; and the workers bound on that connection. They are the Conn's, not
// the process's: a worker bound on a connection since closed answered every later pull "connection closed" (design
// 47 H, H-A-363: state outliving the connection it was made on).
type pulls struct {
mu sync.Mutex
held map[string]pulledMsg
@@ -46,8 +48,6 @@ type pulledMsg struct {
msg jetstream.Msg
}
var seatPulls = &pulls{held: map[string]pulledMsg{}, cons: map[string]jetstream.Consumer{}}
func pullID() string {
var b [8]byte
_, _ = rand.Read(b[:])
@@ -111,17 +111,20 @@ func (c *Conn) SeatPull(module, consumer string, wait time.Duration) (*Pulled, e
}
p := &Pulled{ID: pullID(), Worker: w.Consumer, Subject: msg.Subject(), Body: json.RawMessage(msg.Data()),
Headers: headers, Delivered: delivered}
seatPulls.mu.Lock()
seatPulls.held[p.ID] = pulledMsg{module: module, msg: msg}
seatPulls.mu.Unlock()
c.pulls.mu.Lock()
if c.pulls.held == nil {
c.pulls.held = map[string]pulledMsg{}
}
c.pulls.held[p.ID] = pulledMsg{module: module, msg: msg}
c.pulls.mu.Unlock()
return p, nil
}
func (c *Conn) pullConsumer(module string, w SeatWorker) (jetstream.Consumer, error) {
key := module + " " + w.Consumer
seatPulls.mu.Lock()
bound, ok := seatPulls.cons[key]
seatPulls.mu.Unlock()
c.pulls.mu.Lock()
bound, ok := c.pulls.cons[key]
c.pulls.mu.Unlock()
if ok {
return bound, nil
}
@@ -135,9 +138,12 @@ func (c *Conn) pullConsumer(module string, w SeatWorker) (jetstream.Consumer, er
if err != nil {
return nil, fmt.Errorf("binding %s's worker %s on %s: %w", module, w.Consumer, w.Stream, err)
}
seatPulls.mu.Lock()
seatPulls.cons[key] = bound
seatPulls.mu.Unlock()
c.pulls.mu.Lock()
if c.pulls.cons == nil {
c.pulls.cons = map[string]jetstream.Consumer{}
}
c.pulls.cons[key] = bound
c.pulls.mu.Unlock()
return bound, nil
}
@@ -168,15 +174,15 @@ func (c *Conn) SeatSettleAfter(module, id string, delay time.Duration) error {
}
func (c *Conn) settle(module, id, fate string, delay time.Duration) error {
seatPulls.mu.Lock()
p, ok := seatPulls.held[id]
c.pulls.mu.Lock()
p, ok := c.pulls.held[id]
if ok && p.module != module {
ok = false
}
if ok && fate != SettleProgress {
delete(seatPulls.held, id)
delete(c.pulls.held, id)
}
seatPulls.mu.Unlock()
c.pulls.mu.Unlock()
if !ok {
return fmt.Errorf("%s holds no pulled message %s", module, id)
}
@@ -192,8 +198,8 @@ func (c *Conn) settle(module, id, fate string, delay time.Duration) error {
case SettleNakAfter:
return p.msg.NakWithDelay(delay)
}
seatPulls.mu.Lock()
seatPulls.held[id] = p // an unknown fate settles nothing
seatPulls.mu.Unlock()
c.pulls.mu.Lock()
c.pulls.held[id] = p // an unknown fate settles nothing
c.pulls.mu.Unlock()
return fmt.Errorf("a pulled message's fate is ack, nak, nak-after, term or progress, not %q", fate)
}