Documentation
¶
Index ¶
- func ServeSubscribe(bus *Bus, logger *slog.Logger) http.HandlerFunc
- type Bus
- func (b *Bus) History(ctx context.Context, last int, topicPattern string) ([]Event, error)
- func (b *Bus) HistoryByTimeRange(ctx context.Context, from, to time.Time, source, topicPattern string, ...) ([]Event, error)
- func (b *Bus) Prune()
- func (b *Bus) Publish(ctx context.Context, topic string, data json.RawMessage, source string) (*Event, error)
- func (b *Bus) StartPruning(interval time.Duration)
- func (b *Bus) Stop()
- func (b *Bus) Subscribe(pattern string) *Subscriber
- func (b *Bus) Unsubscribe(sub *Subscriber)
- type Event
- type Subscriber
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 (*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 ¶
StartPruning launches a background goroutine that periodically removes events beyond maxHistory. Call Stop() to shut it down.
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 ¶
Subscriber receives events matching a pattern.