pgnotify

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package pgnotify implements the engine's change-notification fan-out over PostgreSQL LISTEN/NOTIFY, so pokes reach clients connected to any node rather than only the one that took the write.

Install it with server.WithNotifier. It is independent of which storage adapter is in use — a Postgres notifier in front of a SQLite store is a perfectly good single-database-plus-many-readers deployment — and a deployment reaching for a second node usually already has a Postgres, which is what makes this cheaper than standing up NATS or Redis for the same job.

What it does and does not guarantee

Delivery is BEST-EFFORT, and that is a property of the seam rather than a shortcut taken here: pokes cut latency, they do not carry correctness. Clients converge by polling regardless, so a dropped notification costs one poll interval. Three places lean on that licence — a full publish queue drops rather than blocks, a notification published while the listener is reconnecting is gone (Postgres buffers nothing across a dropped LISTEN), and a payload over the NOTIFY size limit is skipped.

Shape

Notify does two things: it fans out LOCALLY first, then queues a pg_notify. The local half is what keeps same-node latency at zero and off the database round trip; the remote half is what reaches the other nodes. The publishing node also receives its own notification back, which costs one redundant wakeup and nothing else — signals are content-free and coalesce, so no node-id de-duplication is needed.

The transport underneath is engine/pglisten: one connection parked on the channel, one issuing pg_notify, and the reconnect loop over both. What arrives feeds a pubsub.Local, which already implements the per-workspace fan-out, the coalescing send buffer and the Activity extension — so this package adds a wire format and inherits the semantics.

Open dials its own transport. A deployment that also runs pgpresence passes both a shared one instead, halving the connections either would open alone:

link, err := pglisten.Open(ctx, dsn)
...
defer link.Close()
notifier, err := pgnotify.New(ctx, link)

Hosting several applications

One notifier serves every app a server.Manager hosts. Each app's server prefixes the workspace ids it publishes and subscribes to with its own share of the id space, so two apps that both have a workspace "team" do not wake each other's clients and this package needs no notion of an app:

n, err := pgnotify.Open(ctx, dsn)
...
server.NewManager(ctx, root, server.WithServerOptions(server.WithNotifier(n)))

Composition

pubsub.NewCoalescing wraps the SEND side, so the server's notify window caps pg_notify traffic exactly as it caps in-process traffic — the bus carries less, not just the goroutines.

Index

Constants

View Source
const DefaultChannel = "syncplane_poke"

DefaultChannel is the LISTEN/NOTIFY channel used unless WithChannel says otherwise. One channel carries every workspace of every app on it.

Variables

This section is empty.

Functions

This section is empty.

Types

type Notifier

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

Notifier is a pubsub.Notifier backed by LISTEN/NOTIFY. It holds the transport, the publisher goroutine and the node-local fan-out.

func New

func New(ctx context.Context, link *pglisten.Conn, opts ...Option) (*Notifier, error)

New builds a notifier on an existing transport, so a process also running pgpresence opens one pair of connections rather than two.

New blocks until the channel is subscribed, which spans a reconnect if the database is down; ctx bounds that wait. link must outlive the notifier — Close stops publishing and delivery but leaves the transport running.

func Open

func Open(ctx context.Context, dsn string, opts ...Option) (*Notifier, error)

Open connects to the Postgres named by dsn and starts listening, on a transport of its own that Close takes down with it.

The listener connection is established before returning, so a bad DSN or an unreachable database fails here rather than degrading silently into a notifier that never delivers.

func (*Notifier) Close

func (n *Notifier) Close() error

Close stops publishing and delivery. It is idempotent. A notifier from Open closes its transport; one from New leaves the caller's alone.

func (*Notifier) Notify

func (n *Notifier) Notify(workspaceID string)

func (*Notifier) Subscribe

func (n *Notifier) Subscribe(workspaceID string) (<-chan struct{}, func())

func (*Notifier) SubscribeActivity

func (n *Notifier) SubscribeActivity() (<-chan string, func())

SubscribeActivity reports every workspace that sees a notify, local or remote. It is the same stream pubsub.Local serves, which is what makes workspace discovery work across nodes: a workspace first written on another node arrives here as a notification rather than waiting for a consumer's next sweep.

Reconnects are the gap. A workspace written while the listener was down is not announced — nothing re-derives what was missed — so a consumer keeps its periodic sweep as the backstop the contract already asks for.

func (*Notifier) SubscribeSet

func (n *Notifier) SubscribeSet(workspaceIDs []string) pubsub.Set

type Option

type Option func(*config)

Option configures Open and New.

func WithChannel

func WithChannel(name string) Option

WithChannel sets the LISTEN/NOTIFY channel. Give separate deployments that share one database separate channels, or each will wake the other's clients for workspaces they do not have. Must match [a-z_][a-z0-9_]*.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger installs the logger used for this package's own trouble — undecodable notifications, dropped ones, failed publishes. Defaults to slog.Default. Open passes it on to the transport it dials; a transport handed to New keeps whatever logger it was opened with.

Jump to

Keyboard shortcuts

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