events

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 8 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 events provides a typed event bus with in-memory pub/sub via Go channels, crash-recoverable JSONL persistence, TTL pruning, and topic-based subscription. It replaces the shell's file-based pub/sub in event-bus.sh.

Index

Constants

View Source
const DefaultLimit = 50

DefaultLimit is the default maximum number of events returned by queries. Matches the shell _EVENT_BUS_DEFAULT_LIMIT=50.

View Source
const DefaultTTL = 30

DefaultTTL is the default time-to-live for events in days. Matches the shell _EVENT_BUS_DEFAULT_TTL=30.

Variables

This section is empty.

Functions

func ComputeExpiry

func ComputeExpiry(createdAt time.Time, ttlDays int) time.Time

ComputeExpiry calculates the expiration timestamp from a creation time and TTL in days.

func FormatTimestamp

func FormatTimestamp(t time.Time) string

FormatTimestamp formats a time as ISO-8601 UTC matching the shell output:

date -u +%Y-%m-%dT%H:%M:%SZ

func GenerateEventID

func GenerateEventID(now time.Time, randomBytes []byte) string

GenerateEventID creates an event ID matching the shell format:

evt_{unix_timestamp}_{4_hex_chars}

Shell: evt_$(date +%s)_$(head -c 2 /dev/urandom | od -An -tx1 | tr -d ' \n')

func TopicMatch

func TopicMatch(pattern, topic string) bool

TopicMatch checks if a topic matches a pattern. Patterns ending with "*" are treated as prefix matches (e.g., "learning.*" matches "learning.observe", "learning.promote"). All other patterns are exact matches.

Types

type Bus

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

Bus provides in-memory pub/sub with JSONL persistence and TTL-based pruning.

func NewBus

func NewBus(store *storage.Store, config Config) *Bus

NewBus creates a new event bus backed by the given store.

func (*Bus) Cleanup

func (b *Bus) Cleanup(ctx context.Context, dryRun bool) (removed int, remaining int, err error)

Cleanup removes expired events from the JSONL file. If dryRun is true, it reports counts without modifying the file. Returns the number of events removed and remaining.

func (*Bus) Close

func (b *Bus) Close()

Close marks the bus as closed and closes all subscriber channels. No further publishes or subscribes are accepted after Close.

func (*Bus) LoadAndReplay

func (b *Bus) LoadAndReplay(ctx context.Context) error

LoadAndReplay replays all non-expired events from JSONL to matching subscribers. This is the crash recovery mechanism -- call it on startup to ensure no events are lost after an unclean shutdown.

func (*Bus) Publish

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

Publish emits a typed event to the given topic. The event is persisted to JSONL and broadcast to all active subscribers whose patterns match the topic.

func (*Bus) Query

func (b *Bus) Query(ctx context.Context, topicPattern string, since time.Time, limit int) ([]Event, error)

Query reads events from JSONL matching the topic pattern, not expired, and optionally since the given time. Returns up to limit events. Returns an empty slice (not error) if the file does not exist.

func (*Bus) Replay

func (b *Bus) Replay(ctx context.Context, topic string, since time.Time, limit int) ([]Event, error)

Replay reads events from JSONL matching the exact topic, not expired, and since the given time. Events are sorted by timestamp ascending. Returns up to limit events. Returns an empty slice (not error) if the file does not exist.

func (*Bus) Subscribe

func (b *Bus) Subscribe(topicPattern string) (<-chan Event, error)

Subscribe registers a subscriber for events matching the given topic pattern. Returns a read-only channel that receives matching events. The channel is buffered (capacity 256) to avoid blocking publishers.

func (*Bus) Unsubscribe

func (b *Bus) Unsubscribe(topicPattern string, ch <-chan Event)

Unsubscribe removes a subscriber channel and closes it. The publisher is never blocked by unsubscribe operations.

type Config

type Config struct {
	// DefaultTTL is the TTL in days for events when not specified. Defaults to 30.
	DefaultTTL int
	// DefaultLimit is the maximum number of events returned by Query/Replay. Defaults to 50.
	DefaultLimit int
	// JSONLFile is the filename for event persistence, relative to the store's base path.
	// Defaults to "event-bus.jsonl".
	JSONLFile string
}

Config holds bus configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults matching the shell behavior.

type Event

type Event struct {
	ID        string          `json:"id"`
	Topic     string          `json:"topic"`
	Payload   json.RawMessage `json:"payload"`
	Source    string          `json:"source"`
	Timestamp string          `json:"timestamp"`
	TTLDays   int             `json:"ttl_days"`
	ExpiresAt string          `json:"expires_at"`
}

Event represents a single event on the bus. JSON field names match the shell event-bus.sh output exactly.

Jump to

Keyboard shortcuts

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