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 ¶
const ApplicationName = "tasklane-notify"
ApplicationName identifies the listener's connection in pg_stat_activity.
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.
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 ¶
New parses connString once, so a malformed database URL is reported here rather than on every reconnect attempt.
func (*Listener) Run ¶
Run listens until ctx is cancelled, reconnecting with backoff whenever the connection fails. It always returns nil once ctx is done.
func (*Listener) Subscribe ¶
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 ¶
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.