live

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package live is the stream of live-update hints (ADR-0009; C-09.FR-25, C-03.FR-18): each replica runs one Hub that takes the hints of LISTEN/NOTIFY — writers send them in the transaction of their change — and of its own notice watcher, and fans them out to its server-sent events streams. A hint carries no data, so a client re-reads through the API and authorization stays in one place. A stream ends when its session ends, so that the reconnect gets 401.

Index

Constants

View Source
const (
	HintOrganization  = "organization"
	HintSystemNotices = "system-notices"
	// HintAlertGroup names an Alert Group that changed; HintAlertGroups, without an id, says that new Alert Groups
	// may match a list (C-09.FR-25).
	HintAlertGroup  = "alert-group"
	HintAlertGroups = "alert-groups"
	// HintAccountLinks says that an Account link of the stream's User appeared or disappeared (C-18); it goes only to
	// the streams of that User.
	HintAccountLinks = "account-links"
	// HintSystemStatus says that the System status changed, such as the result of the outgoing heartbeat (C-19); it
	// goes only to the streams whose identity reads it (system-status:read), like the notices for Admins.
	HintSystemStatus = "system-status"
)

The hint types of this release; later capabilities add theirs and list them in resyncTypes.

View Source
const (
	// MaxStreams is live.max_streams: the streams one replica serves at once.
	MaxStreams = 1000
	// MaxStreamsPerSession is live.max_streams_per_session: the streams of one session, one per open tab.
	MaxStreamsPerSession = 10
	// CheckInterval is how often a replica checks that the sessions of its streams are still usable, and evaluates
	// the Organization-wide notices.
	CheckInterval = 5 * time.Second
)
View Source
const (
	// RetryMillis is the reconnect delay the stream gives the browser in its first line.
	RetryMillis = 3000
	// KeepaliveInterval is how often a stream without hints sends a comment, so that proxies keep it open.
	KeepaliveInterval = 25 * time.Second
)

Variables

View Source
var ErrTooManyStreams = errors.New("too many live-updates streams")

ErrTooManyStreams refuses a stream beyond MaxStreams or MaxStreamsPerSession.

Functions

This section is empty.

Types

type Hint

type Hint struct {
	Type string
	ID   string
}

Hint is one live-update hint: its type and the public_id it names, empty for a whole collection.

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub fans the hints of one Organization out to the streams of this replica.

func NewHub

func NewHub(orgID int64, live func(ctx context.Context, ids []int64) ([]int64, error)) *Hub

NewHub returns the Hub of the Organization orgID; live tells which sessions are still usable.

func (*Hub) CheckSessions

func (h *Hub) CheckSessions(ctx context.Context)

CheckSessions closes the streams whose session ended, expired or lost its user, in one query for the replica. When the check fails, as when the database is unavailable, every stream stays open until the next check.

func (*Hub) Close

func (h *Hub) Close()

Close ends every stream and refuses new ones; the server closes the Hub when it shuts down, so that the streams do not hold up the drain.

func (*Hub) Listening

func (h *Hub) Listening(restored bool)

Listening is called each time the replica listens for hints; after a loss it sends every stream the hints of everything, because the hints sent meanwhile are gone.

func (*Hub) Receive

func (h *Hub) Receive(n db.Hint)

Receive takes a hint of LISTEN/NOTIFY and sends it to every stream that may get it when it belongs to the Hub's Organization; a hint that names a User goes only to the streams of that User.

func (*Hub) Run

func (h *Hub) Run(ctx context.Context, ticks <-chan time.Time)

Run checks the sessions of the streams at every tick until ctx ends, then closes the Hub.

func (*Hub) Send

func (h *Hub) Send(hint Hint, to func(Subscriber) bool)

Send sends hint to the streams whose subscriber to accepts, every stream when to is nil, the hints about Alert Groups only to those who read them. A stream whose hints are not read fast enough is closed instead.

func (*Hub) Stream

func (h *Hub) Stream(ctx context.Context, w io.Writer, flush func() error, sub *Subscription) error

Stream writes the server-sent events of sub to w, calling flush after each, until ctx ends, the subscription is closed — its session ended, the Hub closed or the stream fell behind — or a write fails. It starts with "retry: 3000", sends ": keepalive" every KeepaliveInterval and each hint as an event named hint with an id line, the count of the stream's events, and the data {"type": …, "id": …}. The caller unsubscribes afterwards.

func (*Hub) Streams

func (h *Hub) Streams() int

Streams is how many streams the Hub serves.

func (*Hub) Subscribe

func (h *Hub) Subscribe(s Subscriber) (*Subscription, error)

Subscribe opens a stream's subscription; beyond MaxStreams on the replica or MaxStreamsPerSession for the session it is ErrTooManyStreams, and once the Hub is closed it is a subscription that is already done.

func (*Hub) Unsubscribe

func (h *Hub) Unsubscribe(sub *Subscription)

Unsubscribe ends a subscription; a stream calls it when it stops.

type Notices

type Notices struct {
	// contains filtered or unexported fields
}

Notices watches the Organization-wide notices of C-02.FR-24 for the streams of one replica. The notices change with time as well as with the writes of the Leader, so each replica evaluates them every CheckInterval on the business clock and sends a system-notices hint when the set a stream may see changed: the notices for everyone to every stream, the notices for Admins only to the streams of identities that hold system-status:read.

func NewNotices

func NewNotices(read func(ctx context.Context, now time.Time) ([]organization.Notice, error), business clock.Clock,
	hub *Hub, log *logging.Logger) *Notices

NewNotices returns the watcher of the notices that read computes at a business time, sending hints through hub.

func (*Notices) Check

func (n *Notices) Check(ctx context.Context)

Check evaluates the notices once and sends the hints of a change. The first successful check only records the sets: a client reads the notices when it starts. A failed check is logged once, until a check succeeds again.

func (*Notices) Run

func (n *Notices) Run(ctx context.Context, ticks <-chan time.Time)

Run checks the notices at every tick until ctx ends.

func (*Notices) Visible

func (n *Notices) Visible(ctx context.Context, admin bool) ([]organization.Notice, error)

Visible returns the active notices the caller may see: the notices for everyone, and those for Admins when admin (the caller holds system-status:read).

type Subscriber

type Subscriber struct {
	SessionID   int64
	User        string
	Admin       bool
	AlertGroups bool
}

Subscriber is who reads a stream: the session, the public_id of its User, whether its identity sees the notices for Admins (system-status:read) and whether it reads Alert Groups (alert-groups:read).

type Subscription

type Subscription struct {
	Subscriber
	// contains filtered or unexported fields
}

Subscription is one stream's share of the Hub.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL