package main import ( "flag" "fmt" "io" "os" "sync/atomic" "github.com/novox/mesh-controller/internal/broker" ) // The serving controller's own connection, lent to whatever it does for a moment (novox/hq issue 327). // // Every place that needed the bus for a moment dialled it: a verb's own process, and in the serving // controller a watchdog tick reading whether the build seat paused, a walk's step reading readiness, a // queue read. Each paid a connection, a TLS handshake and a login on the control node, and hundreds an // hour hid in the server's connection total the one thing it would show: a client reconnecting in a loop. // The serving controller is on the bus already; what it does is done on that connection. // servingBus is the serving controller's connection, set when it starts serving; nil in every other // process, which dials its own. var servingBus atomic.Pointer[broker.JetStream] // aBus is a connection for something done for a moment: the serving controller's own, lent — so its // Close closes nothing — when this process is it, and otherwise one dialled for it, named for this // process (broker.ConnectionName), which its Close closes. func aBus() (*broker.JetStream, error) { if serving := servingBus.Load(); serving != nil { return broker.Borrow(serving), nil } address, err := broker.BusAddress() if err != nil { return nil, err } js, err := broker.Dial(address) if err != nil { return nil, fmt.Errorf("cannot reach the bus: %w", err) } return js, nil } // usageTo sends a command's flag errors and usage to where its answer goes when that is not this process's // output: a verb answered in the serving controller says them in its answer, not in the controller's log. func usageTo(set *flag.FlagSet, w io.Writer) { if w != os.Stdout { set.SetOutput(w) } }