broadcaster

package
v1.4.8 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package broadcaster listens for the database's notifications, on a channel, so the server's modules, and its clients, can be told of changes, with one connection which listens, rather than one each.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	pg.PoolConn
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, pool pg.PoolConn, opts ...Opt) (_ *Manager, err error)

New creates a new manager, which listens for the notifications on its channel when it's run

func (*Manager) Channel

func (self *Manager) Channel() string

Channel returns the channel the manager listens for notifications on

func (*Manager) Listening

func (self *Manager) Listening() bool

Listening reports whether the manager is listening on its channel, so the events published to it are sent to the subscribers

func (*Manager) Publish

func (self *Manager) Publish(ctx context.Context, event schema.Event) (err error)

Publish publishes the event to the manager's channel, as JSON, so every server listening on it is told of it. Returns ErrBadParameter when the event is larger than a notification can be.

func (*Manager) Run

func (self *Manager) Run(ctx context.Context, logger *slog.Logger) error

Run listens for the notifications on the manager's channel, and sends each, as an event, to the subscribers, logging with the logger, until the context is cancelled. A notification which isn't an event is logged, and skipped. When the connection which listens closes, such as when it fails, it listens again, and the notifications in between are missed. When it can't listen again, such as while the database restarts, it logs that, and tries again, after a delay which doubles each time, until it can, or the context is cancelled. When it can't listen at first, it returns the error. When it stops, every subscription ends. Only one can run at a time.

func (*Manager) Running

func (self *Manager) Running() bool

Running reports whether the manager is running

func (*Manager) Schema

func (self *Manager) Schema() string

Schema returns the database schema of the manager

func (*Manager) Subscribe

func (self *Manager) Subscribe(ctx context.Context, filter schema.EventFilter, ch chan<- schema.Event) (<-chan error, error)

Subscribe sends the events published to the manager's channel which match the filter to ch, from when it returns, as it's subscribed before it does, until the context is cancelled, or the manager stops running, as there will be no more events. It returns a channel which is sent nil when the context is cancelled, or ErrNotAvailable when the manager stops, or has stopped, once the subscription has ended, after which no more events are sent to ch, so it can be closed. A subscription made before the manager first runs lasts until it stops, and one made once it's stopped isn't made, and ErrNotAvailable is returned, so the server, as it stops, doesn't accept them. A channel is subscribed once at a time: subscribing it while it's subscribed returns ErrConflict. The channel ch isn't closed. An event isn't waited for a subscriber to receive, so a subscriber whose channel is full misses it, which coalesces events into the one in the channel, or which, when it needs every event, should buffer its channel.

func (*Manager) Subscribers

func (self *Manager) Subscribers() int

Subscribers returns how many subscriptions there are

func (*Manager) WaitListening

func (self *Manager) WaitListening(ctx context.Context) error

WaitListening waits until the manager is listening on its channel, so the changes made from then are sent to the subscribers, or returns the context's error when it's cancelled first

type Opt

type Opt func(*opt) error

Opt is a functional option for the broadcaster manager's configuration

func WithChannel

func WithChannel(channel string) Opt

WithChannel sets the channel the notifications are listened for on, which must be an identifier

func WithSchema

func WithSchema(name string) Opt

WithSchema sets the database schema, which must be an identifier

func WithTracer

func WithTracer(tracer trace.Tracer) Opt

WithTracer sets the tracer used for tracing operations

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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