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
- func ComputeExpiry(createdAt time.Time, ttlDays int) time.Time
- func FormatTimestamp(t time.Time) string
- func GenerateEventID(now time.Time, randomBytes []byte) string
- func TopicMatch(pattern, topic string) bool
- type Bus
- func (b *Bus) Cleanup(ctx context.Context, dryRun bool) (removed int, remaining int, err error)
- func (b *Bus) Close()
- func (b *Bus) LoadAndReplay(ctx context.Context) error
- func (b *Bus) Publish(ctx context.Context, topic string, payload json.RawMessage, source string) (*Event, error)
- func (b *Bus) Query(ctx context.Context, topicPattern string, since time.Time, limit int) ([]Event, error)
- func (b *Bus) Replay(ctx context.Context, topic string, since time.Time, limit int) ([]Event, error)
- func (b *Bus) Subscribe(topicPattern string) (<-chan Event, error)
- func (b *Bus) Unsubscribe(topicPattern string, ch <-chan Event)
- type Config
- type Event
Constants ¶
const DefaultLimit = 50
DefaultLimit is the default maximum number of events returned by queries. Matches the shell _EVENT_BUS_DEFAULT_LIMIT=50.
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 ¶
ComputeExpiry calculates the expiration timestamp from a creation time and TTL in days.
func FormatTimestamp ¶
FormatTimestamp formats a time as ISO-8601 UTC matching the shell output:
date -u +%Y-%m-%dT%H:%M:%SZ
func GenerateEventID ¶
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 ¶
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 (*Bus) Cleanup ¶
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 ¶
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 ¶
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 ¶
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.