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 ¶
- type Manager
- func (self *Manager) Channel() string
- func (self *Manager) Listening() bool
- func (self *Manager) Publish(ctx context.Context, event schema.Event) (err error)
- func (self *Manager) Run(ctx context.Context, logger *slog.Logger) error
- func (self *Manager) Running() bool
- func (self *Manager) Schema() string
- func (self *Manager) Subscribe(ctx context.Context, filter schema.EventFilter, ch chan<- schema.Event) (<-chan error, error)
- func (self *Manager) Subscribers() int
- func (self *Manager) WaitListening(ctx context.Context) error
- type Opt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
func New ¶
New creates a new manager, which listens for the notifications on its channel when it's run
func (*Manager) Listening ¶
Listening reports whether the manager is listening on its channel, so the events published to it are sent to the subscribers
func (*Manager) Publish ¶
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 ¶
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) 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 ¶
Subscribers returns how many subscriptions there are
type Opt ¶
type Opt func(*opt) error
Opt is a functional option for the broadcaster manager's configuration
func WithChannel ¶
WithChannel sets the channel the notifications are listened for on, which must be an identifier
func WithSchema ¶
WithSchema sets the database schema, which must be an identifier
func WithTracer ¶
WithTracer sets the tracer used for tracing operations