events

package
v0.1.0-beta Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServeSubscribe

func ServeSubscribe(bus *Bus, logger *slog.Logger) http.HandlerFunc

ServeSubscribe handles WebSocket subscription connections. Query params:

  • pattern: glob pattern for topic filtering (default: "*")

Types

type Bus

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

Bus provides pub/sub event distribution with SQLite-backed history.

func New

func New(db *sql.DB, maxHistory int) *Bus

New creates a new event Bus.

func (*Bus) History

func (b *Bus) History(ctx context.Context, last int, topicPattern string) ([]Event, error)

History returns the last N events, optionally filtered by topic pattern.

func (*Bus) HistoryByTimeRange

func (b *Bus) HistoryByTimeRange(ctx context.Context, from, to time.Time, source, topicPattern string, limit int) ([]Event, error)

HistoryByTimeRange returns events filtered by time range, source, and topic pattern. All filter parameters are optional — pass zero time or empty strings to skip.

func (*Bus) Prune

func (b *Bus) Prune()

Prune removes events beyond maxHistory. Called automatically by StartPruning, but can also be invoked manually.

func (*Bus) Publish

func (b *Bus) Publish(ctx context.Context, topic string, data json.RawMessage, source string) (*Event, error)

Publish writes an event to SQLite history, prunes old events, and fans out to matching subscribers.

func (*Bus) StartPruning

func (b *Bus) StartPruning(interval time.Duration)

StartPruning launches a background goroutine that periodically removes events beyond maxHistory. Call Stop() to shut it down.

func (*Bus) Stop

func (b *Bus) Stop()

Stop shuts down the background pruning goroutine.

func (*Bus) Subscribe

func (b *Bus) Subscribe(pattern string) *Subscriber

Subscribe registers a subscriber for events matching pattern. Pattern uses path.Match glob syntax on dot-separated topics.

func (*Bus) Unsubscribe

func (b *Bus) Unsubscribe(sub *Subscriber)

Unsubscribe removes a subscriber and closes its channel.

type Event

type Event struct {
	ID        int64           `json:"id"`
	Topic     string          `json:"topic"`
	Data      json.RawMessage `json:"data"`
	Source    string          `json:"source"`
	CreatedAt time.Time       `json:"created_at"`
}

Event represents a published event.

type Subscriber

type Subscriber struct {
	Pattern string
	Ch      chan Event
}

Subscriber receives events matching a pattern.

Jump to

Keyboard shortcuts

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