Documentation
¶
Overview ¶
Package pushhub is the in-process fan-out behind the browser's single /v1/ws connection (Q2). Publishers (job persist, the event tap, decision/session writes, worker presence) only ever call the non-blocking Notify* methods; the hub turns those into frames on per-connection bounded queues. A queue that fills drops the increment and flags the connection for a `resync`, so a slow browser can never stall recordEvent or persist.
The package is a leaf: it knows topics, frames and queues, nothing about jobs, stores or HTTP (G022).
Index ¶
- Constants
- Variables
- func ErrorFrame(msg string, topics []string) []byte
- func JobTopic(id string) string
- func PongFrame() []byte
- func ValidTopic(topic string) bool
- type BackfillFunc
- type Conn
- func (c *Conn) Caller() string
- func (c *Conn) Close()
- func (c *Conn) Next(ctx context.Context) ([]byte, error)
- func (c *Conn) Push(b []byte)
- func (c *Conn) Subscribe(topics []string, sinceSeq map[string]int64) (rejected []string)
- func (c *Conn) Topics() []string
- func (c *Conn) Unsubscribe(topics []string)
- type Hub
- func (h *Hub) CloseAll()
- func (h *Hub) ConnCount() int
- func (h *Hub) NoteEvent(scope string, ev JobEvent)
- func (h *Hub) Notify(topic string)
- func (h *Hub) NotifyJob(id, status string)
- func (h *Hub) PublishJobEvent(id string, ev JobEvent)
- func (h *Hub) PublishJobKind(id, kind string)
- func (h *Hub) Register(caller string) (*Conn, error)
- func (h *Hub) SetBackfill(fn BackfillFunc)
- func (h *Hub) SetSnapshot(topic string, fn SnapFunc)
- func (h *Hub) Stats() (dropped, resyncs int64)
- func (h *Hub) Subscribers(topic string) int
- type JobEvent
- type Options
- type SnapFunc
Constants ¶
const ( TopicStats = "stats" TopicPending = "pending" TopicJobs = "jobs" TopicSessions = "sessions" TopicRunners = "runners" TopicMeta = "meta" TopicPlans = "plans" TopicWorkflows = "workflows" TopicSchedules = "schedules" TopicWork = "work" JobTopicPrefix = "job:" )
Topics. A `job:<id>` topic carries one job's increments; the rest are global.
const ( DefaultQueueSize = 64 DefaultMaxConnsPerCaller = 16 // DefaultStatsInterval: stats is heavy (two 200ms-budget passes), so it is // coalesced to at most one computation per interval, and only with subscribers. DefaultStatsInterval = 2 * time.Second // DefaultSessionsInterval: session churn (turn counters) is throttled the same way. DefaultSessionsInterval = 2 * time.Second // DefaultInvalInterval spaces the cheap invalidations (jobs, runners, ...) so a burst // of writes reaches the browser as one refetch trigger. DefaultInvalInterval = 300 * time.Millisecond // DefaultPendingInterval keeps the bell snappy while still merging a burst. DefaultPendingInterval = 200 * time.Millisecond )
Defaults (the design's numbers).
Variables ¶
var ErrClosed = errors.New("pushhub: connection closed")
ErrClosed is returned by Conn.Next once the connection or hub is closed.
var ErrTooManyConns = errors.New("pushhub: too many connections for this caller")
ErrTooManyConns is Register's refusal once a caller already holds the maximum.
Functions ¶
func ErrorFrame ¶
ErrorFrame tells a client one of its frames was refused.
func ValidTopic ¶
ValidTopic reports whether a client may subscribe to topic.
Types ¶
type BackfillFunc ¶
BackfillFunc returns the events of one job after a sequence cursor.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is one browser connection: a bounded outbound queue plus its subscriptions.
func (*Conn) Close ¶
func (c *Conn) Close()
Close unregisters the connection and unblocks Next. Idempotent.
func (*Conn) Next ¶
Next blocks for the next outbound frame. A pending overflow is reported first, as a `resync` frame, ahead of whatever is still queued.
func (*Conn) Push ¶
Push queues an arbitrary pre-built frame (hello/pong style) for this connection.
func (*Conn) Subscribe ¶
Subscribe adds topics (invalid ones are returned as rejected). sinceSeq carries the per-`job:<id>` resume cursors. Snapshot topics are answered with their current snapshot, a cursor-carrying job topic with the events it missed.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub is the subscription table plus the coalescers that feed it.
func (*Hub) CloseAll ¶
func (h *Hub) CloseAll()
CloseAll drops every connection and refuses new ones (server shutdown).
func (*Hub) NoteEvent ¶
NoteEvent routes one recorded event (any scope) onto the topics it affects. seq is the event's row sequence, scope the job id or a synthetic `plan:<id>`-style scope.
func (*Hub) Notify ¶
Notify marks a global topic changed. Snapshot topics (stats, pending) are recomputed, the rest get an `inval`; either way bursts are coalesced and the work happens on a hub goroutine, so Notify is safe on any hot path.
func (*Hub) NotifyJob ¶
NotifyJob reports that job id changed to status: it feeds the `jobs` list invalidation (with the id), the `job:<id>` topic, and stats.
func (*Hub) PublishJobEvent ¶
PublishJobEvent forwards one recorded event to the job's subscribers. It is a no-op (and allocation-free) when nobody watches the job.
func (*Hub) PublishJobKind ¶
PublishJobKind sends a typed hint (e.g. "interaction") to a job's subscribers.
func (*Hub) Register ¶
Register admits a new connection for caller. The hello frame is already queued.
func (*Hub) SetBackfill ¶
func (h *Hub) SetBackfill(fn BackfillFunc)
SetBackfill installs the `job:<id>` since_seq source.
func (*Hub) SetSnapshot ¶
SetSnapshot installs the payload source of a snapshot topic (stats, pending).
func (*Hub) Subscribers ¶
Subscribers is the number of connections subscribed to topic.
type JobEvent ¶
type JobEvent struct {
Seq int64 `json:"seq"`
Type string `json:"type"`
Detail string `json:"detail,omitempty"`
At int64 `json:"at"`
}
JobEvent is one lifecycle event as backfilled for a `job:<id>` subscription.
type Options ¶
type Options struct {
QueueSize int
MaxConnsPerCaller int
StatsInterval time.Duration
SessionsInterval time.Duration
InvalInterval time.Duration
PendingInterval time.Duration
// Version is stamped on the hello frame; a changed value tells the page a new build
// is running.
Version func() string
Now func() time.Time
}
Options tunes a Hub; zero values take the defaults above.