notifier

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package notifier multiplexes a single Postgres LISTEN connection to the producers, the cancel handler and the elector, and reconnects with jittered backoff when the connection is lost.

n, err := notifier.New(notifier.Config{Listener: l, Clock: clk, OnConnect: wakeAll})
n.Subscribe("foreman_insert", func(queue string) { wake(queue) })
go n.Run(ctx)

Index

Constants

View Source
const (
	DefaultMinBackoff = 100 * time.Millisecond
	DefaultMaxBackoff = 5 * time.Second
	// DefaultIdleTimeout is how long the connection may stay quiet before it
	// is pinged; a connection that died silently is found within twice this.
	DefaultIdleTimeout = 30 * time.Second
)

Defaults for reconnect backoff.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Listener job.Listener
	Clock    clock.Clock
	Logger   *slog.Logger
	// MinBackoff and MaxBackoff bound the jittered reconnect delay.
	MinBackoff time.Duration
	MaxBackoff time.Duration
	// IdleTimeout is how long to wait for a notification before pinging the
	// connection.
	IdleTimeout time.Duration
	// OnConnect runs after every successful (re)connection; notifications
	// sent while disconnected are lost, so callers re-check state here.
	OnConnect func()
	Rand      func() float64
}

Config configures a Notifier.

type Notifier

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

Notifier multiplexes one LISTEN connection to many subscribers and reconnects with jittered exponential backoff when the connection drops.

func New

func New(cfg Config) (*Notifier, error)

New validates cfg.

func (*Notifier) Connected

func (n *Notifier) Connected() bool

Connected reports whether the LISTEN connection is up.

func (*Notifier) Run

func (n *Notifier) Run(ctx context.Context) error

Run listens until ctx is cancelled, reconnecting after failures.

func (*Notifier) Subscribe

func (n *Notifier) Subscribe(channel string, fn func(payload string))

Subscribe registers fn for channel. fn runs on the notifier goroutine and must not block. Subscriptions must be made before Run.

Jump to

Keyboard shortcuts

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