Files
mesh-catalog/modules/networkmanager/cmd/uplink-tools/publish.go
T
jochen f6646c7254 M5 networkmanager: the bundle publishes the kept events
The bundle publishes what the watcher kept, in order and under each event's own
message id, deleting each once the stream acknowledged it and keeping a refused one
for the next moment: its start, a file event on pending/ and its bus connection's
return. The watcher now names the events by the seat's generated contract.

Builds: design 08 sign-in-page §1, §20, §22
2026-11-24 21:22:01 +01:00

66 lines
2.2 KiB
Go

package main
// The bundle publishes the kept events (design 08 sign-in-page §20): in sequence order, on the seat's subject, each
// with its message id as the bus's message id (the runtime prefixes it with the module), awaiting the stream's
// acknowledgement, and deletes each file once acknowledged. A publish refused or unacknowledged stays in pending/, is
// written to the journal and counted, and is published again at the next moment: the bundle's start, a file event of
// the kernel on pending/, its bus connection coming back, or uplink_publish_again.
import (
"encoding/json"
"sync"
)
// publisher publishes the kept events, one round at a time.
type publisher struct {
folder Folder
// publish says one event on the seat, de-duplicated by id, and answers once the stream acknowledged it.
publish func(subject string, body json.RawMessage, id string) error
mu sync.Mutex
refused int
}
// publishResult is one round: how many were published and how many still wait.
type publishResult struct {
Published int `json:"published"`
Waiting int `json:"waiting"`
Refused string `json:"refused,omitempty"`
}
// publishKept publishes every kept event in order; the first refused stops the round, so none is published before an
// older one.
func (p *publisher) publishKept(moment string) publishResult {
p.mu.Lock()
defer p.mu.Unlock()
var out publishResult
kept, err := p.folder.pending()
if err != nil {
journal("reading the kept events at %s failed: %v", moment, err)
out.Refused = err.Error()
return out
}
for i, k := range kept {
if err := p.publish(k.Subject, k.Body, k.MessageID); err != nil {
p.refused++
out.Waiting, out.Refused = len(kept)-i, err.Error()
journal("publishing %s at %s was refused or not acknowledged: %v; %d left waiting", k.MessageID, moment, err,
out.Waiting)
return out
}
journal("published %s on %s", k.MessageID, k.Subject)
out.Published++
if err := p.folder.drop(k); err != nil {
journal("deleting the published %s failed: %v", k.MessageID, err)
}
}
return out
}
// refusedSince is the publishes refused since the bundle's start.
func (p *publisher) refusedSince() int {
p.mu.Lock()
defer p.mu.Unlock()
return p.refused
}