Documentation
¶
Overview ¶
Package notify multiplexes Postgres LISTEN/NOTIFY over one connection per process.
A Listener hijacks a single connection from the pool and LISTENs on every channel that has at least one Subscription. Delivery is best effort: when a Subscription's buffer is full, or the connection was lost and re-established, the Subscription receives a Notification with Gap set and must assume it missed notifications.
PgBouncer in transaction mode is not supported, because LISTEN needs a real session.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidChannel is returned for a channel name that is empty, longer // than 63 bytes or contains a NUL byte. ErrInvalidChannel = errors.New("notify: invalid channel name") // ErrRunning is returned by Run while another Run of the same Listener is // active. ErrRunning = errors.New("notify: listener already running") )
Functions ¶
func CheckChannel ¶
CheckChannel reports whether channel can be used with LISTEN. A valid name is non-empty, at most 63 bytes long and contains no NUL byte. Any other character is allowed because names are always quoted.
Types ¶
type Config ¶
type Config struct {
// Buffer is the capacity of each Subscription's channel. Values below 1
// use the default of 64.
Buffer int
// Logger receives connection errors. Default slog.Default().
Logger *slog.Logger
}
Config configures a Listener.
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener owns the process's single LISTEN connection and fans incoming notifications out to Subscriptions.
func NewListener ¶
NewListener returns a Listener that hijacks its connection from pool. It does nothing until Run is called.
func (*Listener) Run ¶
Run holds the LISTEN connection until ctx ends and then returns nil. A lost connection is replaced by a newly hijacked one after a backoff, and every Subscription then receives a Gap. Run returns ErrRunning while another Run of the same Listener is active.
func (*Listener) Subscribe ¶
func (l *Listener) Subscribe(channel string) (*Subscription, error)
Subscribe returns a Subscription for channel. The first Subscription of a channel makes the running Listener LISTEN on it; notifications sent before that LISTEN took effect are not delivered. Callers must Close the Subscription when done.
type Notification ¶
type Notification struct {
// Payload is the NOTIFY payload. It is empty for a Gap.
Payload string
// Gap reports that notifications may have been lost, because the
// Subscription's buffer was full or the connection was re-established.
Gap bool
}
Notification is one delivery to a Subscription.
type Option ¶
Option configures optional Listener dependencies.
func WithMeterProvider ¶
func WithMeterProvider(mp metric.MeterProvider) Option
WithMeterProvider records the notify.reconnects and notify.gaps counters with mp instead of the global meter provider.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription receives the notifications of one channel.
func (*Subscription) C ¶
func (s *Subscription) C() <-chan Notification
C returns the channel notifications are delivered on. It is closed by Close.
func (*Subscription) Close ¶
func (s *Subscription) Close()
Close ends the Subscription and closes C. The last Close of a channel makes the Listener UNLISTEN it. Close is idempotent.