Files
mesh-catalog/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/react.go
T
jschoubben 4f10af81b0 GATE-6: the watchdog holds a head no merge call delivered and raises head-untaken
The gate consumes the forge bridge's head-moved, keeps in its template
state heads each head source.<branch> does not name, and raises
head-untaken (scope delivery, id <repository>.<branch>, warning, the
operator its resolver) once untaken-bound passes, on its one deadline
timer; a later source revision naming the head lets it go and clears the
finding. The stalled conditions, their state and the operator-channel
use go: the watchman's record-open is the one signal.

Builds: design 47 H §740; H-A-27 (head-moved's form, forgemsg), H-A-48 (the stalled conditions go; untaken-bound on the gate's one timer), H-A-81 (warning, resolver operator, no owner)

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 03:17:42 +01:00

88 lines
2.8 KiB
Go

package main
import (
"sync"
"time"
)
// How this owner keeps time (novox/hq ADR 0406 §1, issue 975). It has no tick, no forge poll and no reconcile loop:
// a look (React) is taken after each event that can change what a step needs, and a step that waits on the clock — a
// grace, a bound, a retry after a failure — says when it must be looked at again (lookAt). The one timer below is
// armed for the earliest of those and fires one more look; it is never a tick, since a look that finds nothing
// waiting arms nothing.
// askAgainAfter is how long after a failed ask of the controller, the forge or the operator it is asked again: the
// failure is the reason, and it is said in the log when it happens.
const askAgainAfter = 30 * time.Second
// lookWanted is the earliest time a look must be taken again, and the bounded step that wants it.
type lookWanted struct {
mu sync.Mutex
at time.Time
step string
working bool
timer *time.Timer
}
// lookAt says a bounded step, waiting on the clock, must be looked at again at a time. Safe to call with or without
// the holder's lock.
func (h *Holder) lookAt(at time.Time, step string) {
h.wanted.mu.Lock()
defer h.wanted.mu.Unlock()
if h.wanted.at.IsZero() || at.Before(h.wanted.at) {
h.wanted.at, h.wanted.step = at, step
}
}
// again says an ask that failed is asked again after askAgainAfter: a bounded step retried by the look's timer.
func (h *Holder) again(step string) { h.lookAt(h.Now().Add(askAgainAfter), step) }
// Poke asks for a look; any number of pokes before it starts are one look.
func (h *Holder) Poke() {
select {
case h.poke <- struct{}{}:
default:
}
}
// Work takes a look each time it is poked, for ever: the one goroutine that acts on events.
func (h *Holder) Work() {
h.wanted.mu.Lock()
h.wanted.working = true
h.wanted.mu.Unlock()
for range h.poke {
h.React()
}
}
// React takes every step the facts allow, after an event that can change what they are: the held heads past untaken-bound
// (heads.go), and then the time the next look is wanted at, if any step waits on the clock. Never concurrent with itself.
func (h *Holder) React() {
h.reactMu.Lock()
defer h.reactMu.Unlock()
h.wanted.mu.Lock()
h.wanted.at, h.wanted.step = time.Time{}, ""
h.wanted.mu.Unlock()
h.lookAtHeads()
h.arm()
}
// arm sets the timer for the earliest time a step wants a look at, or stops it when none does.
func (h *Holder) arm() {
h.wanted.mu.Lock()
defer h.wanted.mu.Unlock()
if !h.wanted.working {
return
}
if h.wanted.timer != nil {
h.wanted.timer.Stop()
h.wanted.timer = nil
}
if h.wanted.at.IsZero() {
return
}
wait := max(h.wanted.at.Sub(h.Now()), 100*time.Millisecond)
// retries: the time-bound step the last look left waiting (a grace, a bound or an ask that failed), at its time
h.wanted.timer = time.AfterFunc(wait, h.Poke)
}