diff --git a/node-tools/internal/bus/bus.go b/node-tools/internal/bus/bus.go index 24eae81..f7a4737 100644 --- a/node-tools/internal/bus/bus.go +++ b/node-tools/internal/bus/bus.go @@ -427,29 +427,33 @@ func (c *Conn) issue(module string, m Membership) { // right after its boot, a timeout), is said once and read again, with a growing wait, until it can be or the // connection closes: Follow runs once per module, so nothing else would ever read it again. func (c *Conn) followGrants(module string) { - if c.watchGrants(module, true) { + // Read here, not in the loop: the loop outlives Follow, and a test sets them back as it ends (design 47 H, H-A-363). + patience, timeout := GrantsPatience, StateTimeout + if c.watchGrants(module, true, patience, timeout) { return } + wait, most := GrantsRetry, GrantsRetryMost go func() { - wait := GrantsRetry for !c.nc.IsClosed() { time.Sleep(wait) if c.nc.IsClosed() { return } - if c.watchGrants(module, false) { + if c.watchGrants(module, false, patience, timeout) { c.Logf("[mesh-tools] the grants part's memberships are read again; %s on %s follows its key", module, c.node) return } - if wait *= 2; wait > GrantsRetryMost { - wait = GrantsRetryMost + if wait *= 2; wait > most { + wait = most } } }() } -// watchGrants is one attempt of followGrants: true once the key is watched. A failure is said when say is true. -func (c *Conn) watchGrants(module string, say bool) bool { +// watchGrants is one attempt of followGrants: true once the key is watched. A failure is said when say is true. Its +// bounds, patience for the key's direct get and timeout for the bucket and its current value, are GrantsPatience and +// StateTimeout as they stood when followGrants began. +func (c *Conn) watchGrants(module string, say bool, patience, timeout time.Duration) bool { logf := func(format string, a ...any) { if say { c.Logf(format, a...) @@ -459,7 +463,7 @@ func (c *Conn) watchGrants(module string, say bool) bool { // First the key's direct get, which the account is granted alone: a bus that refuses it, or a state not there yet, // answers at once, where binding the bucket would wait out its bound on a refusal. bucket := membership.GrantsBucket - probe, err := c.nc.Request("$JS.API.DIRECT.GET.KV_"+bucket+".$KV."+bucket+"."+key, nil, GrantsPatience) + probe, err := c.nc.Request("$JS.API.DIRECT.GET.KV_"+bucket+".$KV."+bucket+"."+key, nil, patience) if err != nil || probe.Header.Get("Status") == "503" { why := "it did not answer" if err != nil { @@ -473,7 +477,7 @@ func (c *Conn) watchGrants(module string, say bool) bool { if err != nil { return false } - ctx, cancel := context.WithTimeout(context.Background(), StateTimeout) + ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() kv, err := js.KeyValue(ctx, bucket) if err != nil { @@ -506,7 +510,7 @@ func (c *Conn) watchGrants(module string, say bool) bool { } c.issue(module, m) } - timer := time.NewTimer(StateTimeout) + timer := time.NewTimer(timeout) defer timer.Stop() for current := true; current; { select { @@ -522,7 +526,7 @@ func (c *Conn) watchGrants(module string, say bool) bool { case <-timer.C: _ = w.Stop() logf("[mesh-tools] %s's key %s of the grants part's memberships did not answer in %s; %s has no membership "+ - "until it can be", module, key, StateTimeout, module) + "until it can be", module, key, timeout, module) return false } } diff --git a/node-tools/internal/bus/seat.go b/node-tools/internal/bus/seat.go index f703d16..2a9f8c8 100644 --- a/node-tools/internal/bus/seat.go +++ b/node-tools/internal/bus/seat.go @@ -279,6 +279,8 @@ func (c *Conn) SeatTake(module, consumer string, deliver func(Work) error) (func if err != nil { return nil, fmt.Errorf("binding %s's worker %s on %s: %w", module, w.Consumer, w.Stream, err) } + // Read once, as the take starts: its loop outlives SeatTake, and a test sets it back as it ends (design 47 H, H-A-363). + heartbeat := WorkHeartbeat reading, err := bound.Consume(func(msg jetstream.Msg) { if !json.Valid(msg.Data()) { _ = msg.Term() @@ -292,7 +294,7 @@ func (c *Conn) SeatTake(module, consumer string, deliver func(Work) error) (func // that died, not one that is slow. done := make(chan struct{}) go func() { - tick := time.NewTicker(WorkHeartbeat) + tick := time.NewTicker(heartbeat) defer tick.Stop() for { select { diff --git a/node-tools/internal/bus/streams.go b/node-tools/internal/bus/streams.go index b9b70aa..ddd0e34 100644 --- a/node-tools/internal/bus/streams.go +++ b/node-tools/internal/bus/streams.go @@ -106,12 +106,18 @@ type hearing struct { kick chan struct{} done chan struct{} unhear func() + + // rebind, nakDelay and heartbeat are RebindEvery, NakDelay and EventHeartbeat as they stood when the hearing was + // made: its loops outlive the call that started them, so they never read a setting again, which a test may set + // back as it ends (design 47 H, H-A-363: a goroutine outliving its settings). + rebind, nakDelay, heartbeat time.Duration } func newHearing(c *Conn, js jetstream.JetStream, module string, deliver func(Envelope) error) *hearing { return &hearing{c: c, js: js, module: module, deliver: deliver, bound: map[HeardIssued]jetstream.ConsumeContext{}, made: map[HeardIssued]time.Time{}, said: map[HeardIssued]bool{}, why: map[HeardIssued]error{}, - kick: make(chan struct{}, 1), done: make(chan struct{})} + kick: make(chan struct{}, 1), done: make(chan struct{}), + rebind: RebindEvery, nakDelay: NakDelay, heartbeat: EventHeartbeat} } // bind reads one durable consumer by name, and on failure says what it was doing. Errors the bus gives @@ -158,7 +164,7 @@ func (h *hearing) handle(msg jetstream.Msg) { done, stopped := make(chan struct{}), make(chan struct{}) go func() { defer close(stopped) - tick := time.NewTicker(EventHeartbeat) + tick := time.NewTicker(h.heartbeat) defer tick.Stop() for { select { @@ -186,7 +192,7 @@ func (h *hearing) handle(msg jetstream.Msg) { return } if err != nil { - _ = msg.NakWithDelay(NakDelay) + _ = msg.NakWithDelay(h.nakDelay) return } _ = msg.Ack() @@ -203,7 +209,7 @@ func (h *hearing) start() { h.nudge() }) go func() { - tick := time.NewTicker(RebindEvery) + tick := time.NewTicker(h.rebind) defer tick.Stop() for { select { @@ -353,7 +359,7 @@ func (h *hearing) reconcile() { // Said once, not at every attempt; said again when it is bound. if !h.said[heard] { h.c.Logf("[mesh-tools] %s does not hear %s yet: %s its consumer %s failed: %v; tried again every %s", - h.module, heard.Stream, doing, heard.Consumer, err, RebindEvery) + h.module, heard.Stream, doing, heard.Consumer, err, h.rebind) h.said[heard] = true } continue