Files
mesh-tools/node-tools/internal/alive/alive.go
T
jschoubben 3850eb8eac H1 integration bound: the heartbeat and the counter tests hold under load
TestTheRuntimeSaysItIsThereEveryInterval slept 70 ms and wanted three
beats at 20 ms: a loaded machine that did not schedule the heartbeat in
time failed it (main.20's integration step on the build node); and its
stop returned before the heartbeat's goroutine ended, so a beat could be
said after it ("said after it was stopped"). The ticks are now the
caller's (keep, beside Keep's own ticker), the test drives them and waits
on each beat, and stop returns once nothing more is said. Pinned to one
processor at idle priority beside six busy loops, the old test failed 13
of 100 runs; the new passes 100 of 100.

TestTwoWritersIncrementingOneCounterOnTheBusLoseNoUpdate bounded a
writer's tries at 1000, a guess: on one processor the scheduler lets one
writer win every round and the other lost its 1000 tries every time. A
write is lost only to the other writer's write, so the bound is the
other's count plus one.

Builds: design 47 H §774; principle 11
2026-11-20 17:23:59 +01:00

91 lines
2.8 KiB
Go

// Package alive is the node tools saying they are there (novox/hq to-be 45 §3, S11).
//
// **A machine heard and its runtime gone is a machine nobody can ask anything.** The node-engine says
// every minute that the machine is there; nothing said whether the runtime serving every module's
// tools and every held seat's verbs was. Now it says so too, on its own subject, every interval, with
// the interval — the mesh's watchdog raises `tools-silent` after three missed — and its build.
// Core NATS, kept by nobody, as the host's heartbeat: a lost one is the next one.
package alive
import (
"encoding/json"
"runtime/debug"
"sync"
"time"
)
// Every is how often the runtime says it is there.
const Every = 60 * time.Second
// Subject is where one machine's node tools say it.
func Subject(node string) string { return "mesh.control." + node + ".tools-alive" }
// Beat is what is said: the machine, the interval, and the build.
type Beat struct {
Node string `json:"node"`
IntervalSeconds int `json:"interval_seconds"`
Version string `json:"version,omitempty"`
}
// Sayer publishes one message, kept by nobody.
type Sayer interface {
Say(subject string, body []byte) error
}
// Version is this build's commit, as the toolchain stamped it; empty when it did not.
func Version() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
for _, s := range info.Settings {
if s.Key == "vcs.revision" {
return s.Value
}
}
return ""
}
// Keep says the runtime is there now and every interval until the returned function is called. A
// heartbeat that cannot be said is logged when that starts and when it stops — not every minute.
func Keep(bus Sayer, node string, every time.Duration, logf func(string, ...any)) func() {
tick := time.NewTicker(every)
stop := keep(bus, node, every, tick.C, logf)
return func() { stop(); tick.Stop() }
}
// keep is Keep saying it again on every tick: the interval is told in the beat, the ticks are the caller's.
// Its stop returns once nothing more is said.
func keep(bus Sayer, node string, every time.Duration, ticks <-chan time.Time, logf func(string, ...any)) func() {
body, _ := json.Marshal(Beat{Node: node, IntervalSeconds: int(every / time.Second), Version: Version()})
done, ended := make(chan struct{}), make(chan struct{})
var once sync.Once
go func() {
defer close(ended)
failing := ""
for {
why := ""
if err := bus.Say(Subject(node), body); err != nil {
why = err.Error()
}
if why != failing {
if why != "" {
logf("node-tools: could not say this runtime is there — the mesh will call it silent: %s", why)
} else {
logf("node-tools: says again that this runtime is there")
}
failing = why
}
select {
case <-done:
return
case <-ticks:
}
}
}()
return func() {
once.Do(func() { close(done) })
<-ended
}
}