Documentation
¶
Overview ¶
Package bus is the in-process fan-out for live frames: every stored event, session change, or alert is published once and delivered to every subscriber (WebSocket clients, the loop detector, tests). Slow subscribers are dropped rather than allowed to stall the writer — the UI can always catch up via REST.
Every published frame is numbered (Seq) and kept in a replay ring, so a /v1/live client that reconnects gets exactly what it missed (.ai/21-app.md § Live replay).
Index ¶
Constants ¶
const ( // ReplayFrames is the most frames the replay ring holds. ReplayFrames = 2000 // ReplayAge is how long a frame stays in the replay ring. ReplayAge = 10 * time.Minute )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus fans frames out to subscribers.
func (*Bus) Publish ¶
Publish numbers a frame, keeps it for replay, and delivers it to every subscriber without blocking. Numbering and delivery share one lock, so every subscriber receives frames in seq order.
func (*Bus) Resume ¶ added in v0.76.0
func (b *Bus) Resume(buffer int, since *uint64) (s *Subscriber, missed []Frame, seq uint64, ok bool)
Resume subscribes and, in the same step, reports where the subscriber starts. With since nil it starts at the newest frame (seq). With since set, missed is every frame after since, in order, and ok is false when the ring no longer holds them (or never did) — the client must refetch. Frames on the subscriber's channel always come after seq.
func (*Bus) Since ¶ added in v0.76.0
Since returns the frames after since, in order, and the newest seq; ok is false when the ring does not hold all of them.
func (*Bus) Subscribe ¶
func (b *Bus) Subscribe(buffer int) *Subscriber
Subscribe registers a subscriber with the given buffer size.
func (*Bus) Subscribers ¶
Subscribers returns the current subscriber count.
type Frame ¶
type Frame struct {
Type FrameType `json:"type"`
Seq uint64 `json:"seq,omitempty"`
Data any `json:"data"`
}
Frame is what goes over /v1/live. Seq is set by Publish: one more than the frame before it.
type FrameType ¶
type FrameType string
FrameType matches the WS contract: event | session | alert.
const ( FrameEvent FrameType = "event" FrameSession FrameType = "session" FrameAlert FrameType = "alert" FrameStats FrameType = "stats" // per-session stats snapshot; UI convenience // FramePermission says an owned session started or stopped waiting on a // permission prompt: {session_id, permission}, permission null when it // stopped. FramePermission FrameType = "permission" // FrameNotify is a notification for the app (alerts.Notification): what // Telegram would say, as plain text, with the prompt's answers when there // is one Caprock can give. FrameNotify FrameType = "notify" // FrameResources is the resource meter's sample (daemon.ResourceReport), // every few seconds while a dashboard is connected. FrameResources FrameType = "resources" // FrameQueue is an owned session's message queue after any change // (ADR-053): {session_id, messages, waiting}. FrameQueue FrameType = "queue" // FrameInbox says the Inbox changed (03-contracts.md § Inbox): {id, // deleted}, so a screen showing it reads it again. FrameInbox FrameType = "inbox" )
type Subscriber ¶
type Subscriber struct {
C chan Frame
// contains filtered or unexported fields
}
Subscriber receives frames on C. Close it with Unsubscribe.
func (*Subscriber) Lagged ¶ added in v0.76.0
func (s *Subscriber) Lagged() <-chan struct{}
Lagged is signalled when a frame was dropped because C was full, so a subscriber that can replay (Since) catches up even when no later frame arrives to show the gap.
func (*Subscriber) Unsubscribe ¶
func (s *Subscriber) Unsubscribe()
Unsubscribe removes the subscriber and closes its channel.