pubsub

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package pubsub provides change-notification fan-out. Notifier is the seam a shared implementation plugs into for multi-node deployments; pubsubtest.Conformance is the behavior suite such an implementation must pass, and engine/notify/pgnotify is one over Postgres LISTEN/NOTIFY.

Local is strictly single-node: a notification published on one server process never reaches subscribers on another, so clients connected elsewhere fall back to their polling interval. Multi-node deployments must install a shared Notifier via server.WithNotifier — which should also implement the Activity extension, or background work stays as node-local as the pokes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SubscribeActivity

func SubscribeActivity(n Notifier) (ids <-chan string, cancel func(), ok bool)

SubscribeActivity subscribes to workspace activity through n, unwrapping wrappers (Coalescing) on the way down, and reports ok=false when nothing in the chain implements Activity — which leaves periodic sweeping the only way to find work.

Types

type Activity

type Activity interface {
	// SubscribeActivity returns a channel of ids of workspaces that saw a
	// notify, and a cancel func. The channel is closed after cancel.
	//
	// Ids REPEAT: this is a change signal, not a one-time announcement, so a
	// workspace appears again every time it is notified. What the stream must
	// not do is grow with the write rate — a subscriber that is not keeping up
	// sees one id per workspace with a notify outstanding, never one per notify
	// — which is the same coalescing Subscribe gives, spread over every
	// workspace instead of one.
	//
	// Delivery is best-effort in the same way pokes are: it cuts latency and
	// carries no guarantee, so a consumer keeps a periodic sweep as its
	// backstop rather than treating this as complete.
	SubscribeActivity() (<-chan string, func())
}

Activity is the optional extension a Notifier implements when it can report every workspace that sees a notify through ONE subscription, without the subscriber naming those workspaces first.

Subscribe answers "did this workspace change" for a workspace you already know of, which is the right shape for a client stream and the wrong one for background work: a process reacting to changes across a whole store would hold a subscription — a channel, a map entry, and something selecting on it — for every workspace ever written, and could not learn of a workspace created after it started at all. One activity stream carries all of them at fixed cost, which is what keeps a per-workspace consumer's resident footprint independent of how many workspaces exist.

Reach it through SubscribeActivity rather than a bare type assertion, so a wrapped notifier is unwrapped rather than mistaken for one that cannot.

type Coalescing

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

Coalescing wraps a Notifier and caps the notify rate per workspace. The first change in a quiet workspace passes through immediately, then a cooldown of one window starts: changes landing during a cooldown collapse into a single trailing notify at its end (which starts the next cooldown). Steady-state, a write-heavy workspace emits at most one notify per window, while a quiet one keeps zero added latency.

The wrap sits on the send side, so a shared Notifier (e.g. NATS-backed) also carries less traffic on the bus.

A Coalescing owns timers, so it must be Closed when the component that installed it shuts down.

func (*Coalescing) Close

func (c *Coalescing) Close() error

Close stops the pending cooldown timers. A trailing notify owed at Close time is dropped rather than delivered afterwards: past Close the wrapper never touches the inner notifier on its own again. That matters for a shared (NATS/Redis) notifier, which would otherwise see publishes from a process that has already shut down.

It does not close the inner notifier — the caller that supplied it owns its lifecycle. Notify still works after Close (passed through uncapped), so a write racing shutdown loses no signal. Close is idempotent.

func (*Coalescing) Notify

func (c *Coalescing) Notify(workspaceID string)

func (*Coalescing) Subscribe

func (c *Coalescing) Subscribe(workspaceID string) (<-chan struct{}, func())

func (*Coalescing) SubscribeSet

func (c *Coalescing) SubscribeSet(workspaceIDs []string) Set

func (*Coalescing) Unwrap

func (c *Coalescing) Unwrap() Notifier

Unwrap exposes the wrapped notifier so optional extensions (Activity) are found on it. Coalescing deliberately does not forward those itself: a forwarding method would satisfy the extension interface even when the inner notifier cannot, turning "not supported" into a subscription that never fires.

type Local

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

Local is an in-process Notifier.

func NewLocal

func NewLocal() *Local

func (*Local) Notify

func (l *Local) Notify(workspaceID string)

func (*Local) Subscribe

func (l *Local) Subscribe(workspaceID string) (<-chan struct{}, func())

func (*Local) SubscribeActivity

func (l *Local) SubscribeActivity() (<-chan string, func())

func (*Local) SubscribeSet

func (l *Local) SubscribeSet(workspaceIDs []string) Set

type Notifier

type Notifier interface {
	Notify(workspaceID string)
	// Subscribe returns a signal channel and a cancel func. The channel is
	// closed after cancel.
	Subscribe(workspaceID string) (<-chan struct{}, func())
	// SubscribeSet subscribes to every workspace in workspaceIDs at once.
	// Duplicates fold into one membership. Membership is fixed for the Set's
	// life; a different set of workspaces is a new subscription.
	SubscribeSet(workspaceIDs []string) Set
}

Notifier broadcasts "something changed" signals per workspace. Signals are content-free and coalesce: a slow subscriber sees at least one signal after any notify, not one per notify.

func NewCoalescing

func NewCoalescing(inner Notifier, window time.Duration) Notifier

NewCoalescing wraps inner with per-workspace coalescing. A window of zero or less returns inner unchanged — the caller gets no wrapper, and so has nothing to Close.

func Prefixed

func Prefixed(n Notifier, prefix string) Notifier

Prefixed returns a view of n scoped to one prefix of the workspace-id space. The view prepends prefix to every workspace id it passes to n, and removes it from every id n hands back: the members a Set reports and the ids on the activity stream. An id from n without the prefix belongs to another partition, and the view drops it. An empty prefix returns n unchanged.

A host that serves several applications from one notifier gives each application's server its own view, so two applications with the same workspace id do not wake each other's subscribers. The prefix must end in a separator that no workspace id can contain, or two partitions overlap.

The view implements Activity exactly when n does, as SubscribeActivity resolves it. It has no Unwrap: unwrapping it would reach ids that still carry the prefix.

type Set

type Set interface {
	// Wake signals that a member may have been notified. The channel has a
	// one-token buffer and is never closed. A token can outlive the drain that
	// took its members, so an empty Changed after a wake is expected.
	Wake() <-chan struct{}
	// Changed returns the members notified since the previous call and clears
	// them, in no particular order; nil when there are none.
	Changed() []string
	// Close releases the subscription. Idempotent.
	Close()
}

Set is one SubscribeSet subscription: a shared wake channel plus the record of which members were notified since the consumer last drained.

One Set replaces a Subscribe per workspace for a consumer of many. Per-workspace channels put the merge on the consumer — a goroutine and a channel per member — where a Set keeps it inside the notifier's fan-out: Notify marks the member dirty and pokes the wake slot, the consumer selects on one channel and drains with Changed. Nothing blocks and nothing is lost; a notify racing a drain either joins the Changed being taken or leaves a wake token behind, so the worst case is one wake with an empty drain.

Directories

Path Synopsis
Package pubsubtest provides a shared Ginkgo behavior suite every pubsub.Notifier implementation must pass.
Package pubsubtest provides a shared Ginkgo behavior suite every pubsub.Notifier implementation must pass.

Jump to

Keyboard shortcuts

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