pglisten

package
v0.4.0 Latest Latest
Warning

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

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

Documentation

Overview

Package pglisten is the PostgreSQL LISTEN/NOTIFY transport the engine's cross-node fan-outs are built on: one connection parked on the channels a process listens to, one issuing pg_notify, and the reconnect loop that keeps both alive.

It carries strings and knows nothing about what they mean. What belongs in a payload, how a backlog is coalesced and what a lost notification costs are questions for the package above — engine/notify/pgnotify for pokes, engine/presence/pgpresence for the ephemeral plane.

Sharing one Conn

Both of those open their own transport when handed a DSN. A deployment running both opens one Conn instead and hands it to each, which is the difference between two connections per node and four:

link, err := pglisten.Open(ctx, dsn)
if err != nil {
	return err
}
defer link.Close()

notifier, err := pgnotify.New(ctx, link)
...
bus, err := pgpresence.New(ctx, link)

Each registers its own channel and receives only that channel's traffic, so sharing the connection does not mix the two streams. Closing the Conn stops both; closing either of them leaves the Conn and the other one running.

Connections

Two, and neither is pooled: a listener held for as long as the Conn is open, and a publisher dialled on the first Publish and redialled whenever it is found closed. A node's fan-out costs those two however many channels it registers.

Delivery

Best-effort. Postgres buffers nothing across a dropped LISTEN, so everything published while the listener is reconnecting is lost, and Listener.Reconnected is where a caller learns a gap happened.

Index

Constants

View Source
const MaxPayload = 7900

MaxPayload is the largest payload Publish carries, a margin under Postgres's 8000-byte NOTIFY limit. Publish refuses anything longer, so a caller that has to report the failure to whoever produced the payload can size it against this first.

Variables

View Source
var ErrClosed = errors.New("pglisten: connection is closed")

ErrClosed is returned by Listen and Publish on a Conn that has been closed.

Functions

func ValidateChannel

func ValidateChannel(name string) error

ValidateChannel returns a non-nil error if name is not a channel LISTEN accepts unquoted, meaning it does not match [a-z_][a-z0-9_]*.

Listen applies the same check, so this is only needed to reject a configured channel before opening a Conn costs a connection.

Types

type Conn

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

Conn is a shared LISTEN/NOTIFY transport. It must come from Open — the zero value is not usable — and every method is safe for concurrent use.

func Open

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

Open connects to the Postgres named by dsn (a libpq URI or key/value string) and starts listening.

The listener connection is established before returning, so a bad DSN or an unreachable database fails here rather than degrading silently into a transport that never delivers. ctx bounds the dial only: the Conn outlives it and holds its connections until the caller Closes it.

func (*Conn) Close

func (c *Conn) Close() error

Close stops the listener and releases both connections. It is idempotent, and unblocks any Listen still waiting for its subscription.

func (*Conn) Listen

func (c *Conn) Listen(ctx context.Context, channel string, l Listener) error

Listen subscribes to channel and routes its notifications to l. It returns once the subscription is live on the database.

The channel must match [a-z_][a-z0-9_]* and must not already be registered. ctx bounds the wait for the subscription to take effect — which spans a reconnect if the database is down — and abandoning it unregisters the channel. Returns ErrClosed on a closed Conn.

Registering while other channels carry traffic does not disturb them: the listener goroutine is woken over a private control channel with its session — and so its subscriptions — intact. Only when that wake cannot be sent does Listen fall back to breaking the wait, which cycles the connection and costs every channel the same gap a reconnect leaves.

func (*Conn) Publish

func (c *Conn) Publish(ctx context.Context, channel, payload string) error

Publish sends payload on channel, blocking until Postgres has accepted it or ctx is done.

Publishes on one Conn are serialized, so a caller that must not wait on another's round trip queues and publishes from a goroutine of its own. Payloads over MaxPayload bytes are refused without being sent. Returns ErrClosed on a closed Conn.

type Listener

type Listener interface {
	// Notification handles one payload, byte for byte as Publish was given it.
	Notification(payload string)
	// Reconnected reports that the listener connection was replaced, leaving a
	// gap in the stream that nothing replays. A caller whose notifications can
	// be resynthesized re-sends them here; one carrying state that cannot has
	// nothing to do.
	Reconnected()
}

Listener receives one channel's traffic.

Calls are serialized: a Conn invokes at most one Listener method at a time, across every channel and every Listener it carries. Neither method may block — anything slow here stalls delivery on all of them.

type Option

type Option func(*config)

Option configures Open.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger installs the logger used for the transport's own trouble — reconnects, failed publishes. Defaults to slog.Default.

Jump to

Keyboard shortcuts

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