notify

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package notify wakes long-polling leases when jobs are enqueued. One Listener per server holds a dedicated Postgres connection that LISTENs on pgstore.NotifyChannel and fans each notification out to the subscribers of its queue.

The payload is "<queue> <sender>": the queue name, a space, and the sending server's random token. Queue names cannot contain a space, and a NOTIFY payload cannot contain a NUL byte, so a space is the separator. A server skips notifications carrying its own token: Publisher has already woken its leases in memory. A payload without a space (servers before the tag) wakes the queue as before. Older servers read a tagged payload as an unknown queue name and wake no one; their leases find the jobs at the next 1 s re-check, so a rolling upgrade costs at most that.

Index

Constants

View Source
const ApplicationName = "tasklane-notify"

ApplicationName identifies the listener's connection in pg_stat_activity.

View Source
const ConnectTimeout = 10 * time.Second

ConnectTimeout bounds each connection attempt when the DSN sets no connect_timeout, so a reconnect to an unreachable database fails and backs off instead of hanging.

View Source
const DefaultCoalesceWindow = 5 * time.Millisecond

DefaultCoalesceWindow is how long a Publisher gathers publishes for one queue before it sends one NOTIFY for all of them.

Variables

This section is empty.

Functions

This section is empty.

Types

type Execer

type Execer interface {
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
}

Execer runs one statement; *pgxpool.Pool satisfies it.

type Listener

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

func New

func New(connString string, log *slog.Logger) (*Listener, error)

New parses connString once, so a malformed database URL is reported here rather than on every reconnect attempt.

func (*Listener) Listening

func (l *Listener) Listening() bool

Listening reports whether the LISTEN connection is currently up.

func (*Listener) Run

func (l *Listener) Run(ctx context.Context) error

Run listens until ctx is cancelled, reconnecting with backoff whenever the connection fails. It always returns nil once ctx is done.

func (*Listener) Subscribe

func (l *Listener) Subscribe(queue string) (<-chan struct{}, func())

Subscribe returns a channel that receives a value after jobs are enqueued to queue, and a function that ends the subscription. The channel holds at most one pending wake-up: several notifications before the subscriber reads collapse into one, which is all a "go and re-check" signal needs.

type Publisher

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

Publisher tells waiting leases that a queue has new runnable jobs, after the transaction that made them runnable has committed.

Postgres serializes every transaction that calls NOTIFY behind one database-wide lock, held until its commit is flushed to the WAL, so a NOTIFY inside each enqueue's transaction made enqueue commits wait in line (M5a: Lock:object was 30-55% of backend samples). Sending after commit, once per queue per window, takes NOTIFY out of the enqueue's commit.

It stays correct because:

  • A rolled-back enqueue never reaches Publish.
  • A crash between commit and Publish, or a failed NOTIFY, leaves waiting leases to find the job at their next periodic re-check (1 s), as for any lost notification.
  • This server's own waiting leases are woken in memory at once, without a round trip. Its own NOTIFY carries its sender token, so its Listener skips it instead of waking them a second time.
  • Other servers hear the NOTIFY at most one window later.

func NewPublisher

func NewPublisher(l *Listener, db Execer, window time.Duration, log *slog.Logger) *Publisher

NewPublisher wakes l's subscribers directly and sends NOTIFY through db, at most once per queue per window.

func (*Publisher) Close

func (p *Publisher) Close()

Close stops scheduling sends, sends the scheduled ones now instead of at the end of their window, and waits for every send in flight, so it must run before the pool closes. Calling it again is harmless.

func (*Publisher) Publish

func (p *Publisher) Publish(queue string)

Publish wakes this server's waiting leases for queue at once and schedules a NOTIFY for the other servers, unless one is already scheduled. It never blocks. After Close it only wakes local subscribers.

func (*Publisher) Subscribe

func (p *Publisher) Subscribe(queue string) (<-chan struct{}, func())

Subscribe delegates to the Listener, so the API server needs only the Publisher.

Jump to

Keyboard shortcuts

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