eventbus

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package eventbus is a minimal in-process publish/subscribe channel for server-side change events (migration_added, schema_changed, etc.).

Bus is concurrency-safe. Each subscription owns a buffered channel; slow consumers that fill the buffer have new events dropped (with a metrics counter, not a panic) rather than backpressuring publishers. Producers must remain non-blocking — the bus sits in the hot path of migration apply.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

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

Bus is a fan-out pub/sub channel. Zero value is not usable; call New.

func New

func New() *Bus

New returns an empty bus.

func (*Bus) Dropped

func (b *Bus) Dropped() uint64

Dropped returns the cumulative count of events the bus dropped due to full subscriber buffers since startup. Exposed for /readyz diagnostics.

func (*Bus) Publish

func (b *Bus) Publish(ev Event)

Publish broadcasts ev to every current subscriber. Caller should set ev.Kind and ev.Payload; ID and At are assigned by the bus.

func (*Bus) Subscribe

func (b *Bus) Subscribe(buffer int) *Subscription

Subscribe returns a new subscription with the given channel buffer. A buffer of 64–256 is typical; pick higher for clients expected to be slow.

type Event

type Event struct {
	ID      uint64    `json:"event_id"`
	Kind    string    `json:"kind"`
	At      time.Time `json:"at"`
	Payload any       `json:"payload,omitempty"`
}

Event is a single notification posted to the bus. ID is monotonic per-bus and lets Subscription consumers resume after a disconnect (via the /events?since= parameter in the HTTP/IPC adapters).

type Subscription

type Subscription struct {
	Ch chan Event
	// contains filtered or unexported fields
}

Subscription is one consumer's view of the bus. Read events from Ch and call Close when done. The buffer size is fixed at construction; when full, the bus drops further events for this subscription rather than blocking publishers.

func (*Subscription) Close

func (s *Subscription) Close()

Close removes the subscription from the bus and closes Ch. Safe to call multiple times.

Jump to

Keyboard shortcuts

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