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
- Variables
- type Hint
- type Hub
- func (h *Hub) CheckSessions(ctx context.Context)
- func (h *Hub) Close()
- func (h *Hub) Listening(restored bool)
- func (h *Hub) Receive(n db.Hint)
- func (h *Hub) Run(ctx context.Context, ticks <-chan time.Time)
- func (h *Hub) Send(hint Hint, to func(Subscriber) bool)
- func (h *Hub) Stream(ctx context.Context, w io.Writer, flush func() error, sub *Subscription) error
- func (h *Hub) Streams() int
- func (h *Hub) Subscribe(s Subscriber) (*Subscription, error)
- func (h *Hub) Unsubscribe(sub *Subscription)
- type Notices
- type Subscriber
- type Subscription
Constants ¶
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.
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 )
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 ¶
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 ¶
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 ¶
NewHub returns the Hub of the Organization orgID; live tells which sessions are still usable.
func (*Hub) CheckSessions ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) 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 ¶
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.
type Subscriber ¶
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.