Files
archie/internal/platform/hub/hub.go
T
sam 34482c8b0c fix(dashboard): re-read on the live stream instead of polling
Every view listing runs or approvals polled on a timer: the home page made
four requests every five seconds per open tab, two of them the same run list,
and the needs-attention card asked for 200 runs to render five.

A run or an approval request moving is now published on the hub and framed on
the dashboard stream, so those views re-read on the change. A reconnect still
re-reads everything, and the hub drops a reader that falls behind, which ends
its stream and reconnects it.
2026-11-27 19:44:21 +11:00

66 lines
1.6 KiB
Go

// Package hub fans notifications out to live subscribers, such as a
// dashboard's stream. A note carries only what changed, never work: a
// subscriber that falls behind is cut off and resyncs from its cursor, so
// nothing depends on a note arriving.
package hub
import "sync"
// buffer is how many notes a subscriber may fall behind before it is cut off.
const buffer = 32
// WorkTopic carries a note whenever a run or an approval request moves, so a
// mounted view re-reads what is running or waiting on a person.
const WorkTopic = "work"
// Note says something on a topic changed. Seq is the stream position a
// subscriber reads from to catch up.
type Note struct {
Topic string `json:"topic"`
Seq int64 `json:"seq"`
}
// Hub holds the live subscribers.
type Hub struct {
mu sync.Mutex
subs map[chan Note]string
}
// New returns an empty hub.
func New() *Hub { return &Hub{subs: map[chan Note]string{}} }
// Publish sends n to every subscriber of its topic. A subscriber whose buffer
// is full is closed rather than waited for.
func (h *Hub) Publish(n Note) {
h.mu.Lock()
defer h.mu.Unlock()
for ch, topic := range h.subs {
if topic != n.Topic {
continue
}
select {
case ch <- n:
default:
delete(h.subs, ch)
close(ch)
}
}
}
// Subscribe returns a channel of notes on topic, closed if the subscriber
// falls behind, and a function that ends the subscription.
func (h *Hub) Subscribe(topic string) (<-chan Note, func()) {
ch := make(chan Note, buffer)
h.mu.Lock()
h.subs[ch] = topic
h.mu.Unlock()
return ch, func() {
h.mu.Lock()
defer h.mu.Unlock()
if _, ok := h.subs[ch]; ok {
delete(h.subs, ch)
close(ch)
}
}
}