Documentation
¶
Overview ¶
Package events provides the in-process broadcast hub behind GET /api/v1/events. One publisher fans an event out to every connected browser tab; nothing outside this process ever sees it, and nothing in this process depends on delivery succeeding.
The asymmetry is deliberate: a publish happens on an apply's critical path, while a subscriber is a browser tab that may have stopped reading at any moment. So publishing never blocks, never returns an error, and never waits on a reader -- a tab that falls behind loses events and repairs itself by resuming from Last-Event-ID.
Index ¶
Constants ¶
const ( Heartbeat = "heartbeat" ApplyCompleted = "apply.completed" PlanInvalidated = "plan.invalidated" StatusChanged = "status.changed" SeriesChanged = "series.changed" )
Event names. These are the wire values in the SSE `event:` field and the strings the client's bus switches on, so they are part of the contract -- see api/openapi.yaml's /events description.
history.appended from the design spec's catalog is deliberately absent: it would fire at the same instant, from the same place, carrying the same run_id as ApplyCompleted, and two events for one fact is a catalog that lies about its own granularity. If history ever grows a path that appends outside an apply, it earns its own event then.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Event ¶
Event is one broadcast. Data is marshaled to JSON by the HTTP handler rather than here: the hub stays transport-agnostic, and a marshaling failure belongs to the one connection that hit it instead of to every subscriber.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub fans events out to subscribers. The zero value is not usable; call NewHub.
func NewHub ¶
NewHub returns a hub whose subscribers each get a channel buffered to buffer events. A non-positive buffer becomes 1, since an unbuffered channel would make every Publish drop for every subscriber that isn't blocked in a receive at that exact instant.
func (*Hub) LastID ¶
LastID returns the id of the most recent event, or 0 when none has been published.
func (*Hub) Publish ¶
Publish broadcasts an event to every current subscriber and records it in the resume ring. It never blocks and never fails: a subscriber whose buffer is full simply misses this event.
Callers publish AFTER the write they are announcing has committed. A tab that refetches on an event must not be able to outrun the data the event describes.
func (*Hub) Subscribe ¶
Subscribe registers a new subscriber and replays whatever the ring still holds past lastID, so a reconnecting client picks up where it left off. Pass lastID 0 for a fresh subscription.
The returned function unsubscribes and closes the channel; it is idempotent and safe to call from several goroutines. Canceling ctx does the same thing, so a dropped HTTP connection cleans itself up without the handler having to notice.