Documentation
¶
Index ¶
- type MemoryBus
- func (b *MemoryBus) CloseWAL() error
- func (b *MemoryBus) DroppedEvents() int64
- func (b *MemoryBus) EnableWAL(dir string) error
- func (b *MemoryBus) Publish(ctx context.Context, event contracts.Event) error
- func (b *MemoryBus) Request(ctx context.Context, event contracts.Event, timeout time.Duration) (contracts.Event, error)
- func (b *MemoryBus) SetAuditLogger(a contracts.AuditLogger)
- func (b *MemoryBus) SetNodeID(id string)
- func (b *MemoryBus) SetPublishPolicy(p contracts.PublishPolicyProvider)
- func (b *MemoryBus) SetSubscriberTimeout(moduleID string, timeout time.Duration)
- func (b *MemoryBus) Subscribe(ctx context.Context, eventType string, handler contracts.EventHandler) error
- func (b *MemoryBus) SubscribeFrom(ctx context.Context, eventType string, handler contracts.EventHandler, ...) error
- func (b *MemoryBus) SubscribeModule(ctx context.Context, moduleID, eventType string, ...) error
- func (b *MemoryBus) SubscriberCount() int
- func (b *MemoryBus) SubscriberStats() []SubscriberStat
- func (b *MemoryBus) SubscriptionStats() []SubscriptionStat
- func (b *MemoryBus) Unsubscribe(ctx context.Context, eventType string, handler contracts.EventHandler) error
- func (b *MemoryBus) UnsubscribeAll(ctx context.Context, moduleID string) error
- func (b *MemoryBus) UpdateMinSubscriberSeq(seq uint64)
- type SubscriberStat
- type SubscriptionStat
- type WALSegment
- type WALWriter
- func (w *WALWriter) Close() error
- func (w *WALWriter) Flush() error
- func (w *WALWriter) LastSeq() uint64
- func (w *WALWriter) ReplayFrom(ctx context.Context, sinceSeq uint64, fn func(contracts.Event) error) error
- func (w *WALWriter) Segments() []WALSegment
- func (w *WALWriter) SetMinSubscriberSeq(seq uint64)
- func (w *WALWriter) Write(event contracts.Event) (uint64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MemoryBus ¶
type MemoryBus struct {
// contains filtered or unexported fields
}
MemoryBus is the default in-memory event bus. It dispatches events to matching subscribers via bounded per-subscriber channels, providing backpressure protection: slow subscribers have events dropped rather than blocking the publisher or consuming unbounded memory.
func (*MemoryBus) CloseWAL ¶ added in v0.4.0
CloseWAL flushes and closes the WAL. Safe to call on a bus without WAL.
func (*MemoryBus) DroppedEvents ¶ added in v0.4.0
DroppedEvents returns the total number of events dropped across all subscribers due to full channels. Key metric for backpressure monitoring.
func (*MemoryBus) EnableWAL ¶ added in v0.4.0
EnableWAL attaches a WAL writer to the event bus. All published events are persisted to the WAL before being dispatched to subscribers. The WAL must be closed separately via CloseWAL().
func (*MemoryBus) Publish ¶
Publish dispatches an event to all matching subscribers. Each subscriber has a bounded channel; if the channel is full, the event is dropped for that subscriber and a counter is incremented.
func (*MemoryBus) Request ¶
func (b *MemoryBus) Request(ctx context.Context, event contracts.Event, timeout time.Duration) (contracts.Event, error)
Request publishes an event and waits for a reply.
func (*MemoryBus) SetAuditLogger ¶ added in v0.4.0
func (b *MemoryBus) SetAuditLogger(a contracts.AuditLogger)
SetAuditLogger configures an audit logger for event bus operations.
func (*MemoryBus) SetPublishPolicy ¶ added in v0.4.0
func (b *MemoryBus) SetPublishPolicy(p contracts.PublishPolicyProvider)
SetPublishPolicy configures a publish policy provider consulted before every Publish() call. When nil (default), all publishes are allowed.
func (*MemoryBus) SetSubscriberTimeout ¶ added in v0.4.0
SetSubscriberTimeout sets a per-module handler timeout override. Modules with tight latency requirements can request shorter timeouts. The timeout applies to handlers registered via SubscribeModule.
func (*MemoryBus) Subscribe ¶
func (b *MemoryBus) Subscribe(ctx context.Context, eventType string, handler contracts.EventHandler) error
Subscribe registers a handler for the given event type. Use "*" to subscribe to all events. The handler is invoked by a dedicated goroutine that drains events from a bounded channel. Slow handlers have events dropped rather than blocking publishers.
func (*MemoryBus) SubscribeFrom ¶ added in v0.4.0
func (b *MemoryBus) SubscribeFrom(ctx context.Context, eventType string, handler contracts.EventHandler, sinceSeq uint64) error
SubscribeFrom subscribes to events of the given type and replays any events with sequence numbers >= sinceSeq before beginning live dispatch. sinceSeq=0 means "replay all available events."
func (*MemoryBus) SubscribeModule ¶ added in v0.4.0
func (b *MemoryBus) SubscribeModule(ctx context.Context, moduleID, eventType string, handler contracts.EventHandler) error
SubscribeModule registers a handler tagged with a module identifier.
func (*MemoryBus) SubscriberCount ¶ added in v0.4.0
SubscriberCount returns the total number of active subscriber channels. A healthy bus always returns >= 0. Used by health probes.
func (*MemoryBus) SubscriberStats ¶ added in v0.4.0
func (b *MemoryBus) SubscriberStats() []SubscriberStat
SubscriberStats returns per-subscriber metrics for monitoring.
func (*MemoryBus) SubscriptionStats ¶ added in v0.4.0
func (b *MemoryBus) SubscriptionStats() []SubscriptionStat
SubscriptionStats returns a summary of active subscriptions grouped by event type. Used by the admin API for runtime introspection.
func (*MemoryBus) Unsubscribe ¶
func (b *MemoryBus) Unsubscribe(ctx context.Context, eventType string, handler contracts.EventHandler) error
Unsubscribe removes a handler for the given event type.
func (*MemoryBus) UnsubscribeAll ¶ added in v0.4.0
UnsubscribeAll removes all subscriptions tagged with the given module ID.
func (*MemoryBus) UpdateMinSubscriberSeq ¶ added in v0.4.0
UpdateMinSubscriberSeq updates the WAL's minimum subscriber sequence for pruning purposes. Call this periodically or when subscribers advance.
type SubscriberStat ¶ added in v0.4.0
type SubscriberStat struct {
EventType string `json:"event_type"`
ModuleID string `json:"module_id,omitempty"`
Processed int64 `json:"processed"`
Dropped int64 `json:"dropped"`
AvgLatencyUs int64 `json:"avg_latency_us"`
}
SubscriberStat provides per-subscriber backpressure and latency metrics.
type SubscriptionStat ¶ added in v0.4.0
type SubscriptionStat struct {
EventType string `json:"event_type"`
SubscriberCount int `json:"subscriber_count"`
}
SubscriptionStat describes subscriptions for a given event type.
type WALSegment ¶ added in v0.4.0
type WALSegment struct {
Path string
FirstSeq uint64
LastSeq uint64
Size int64
CreatedAt time.Time
}
WALSegment is a single WAL journal file on disk.
type WALWriter ¶ added in v0.4.0
type WALWriter struct {
// contains filtered or unexported fields
}
WALWriter provides a write-ahead log for the event bus. Events are appended as JSONL with a monotonically increasing sequence number. Segments are rotated when they exceed walSegmentSize. Old segments are pruned when all active subscribers have passed their sequence numbers.
func NewWALWriter ¶ added in v0.4.0
NewWALWriter opens or creates a WAL in the given directory. If the directory doesn't exist, it is created.
func (*WALWriter) ReplayFrom ¶ added in v0.4.0
func (w *WALWriter) ReplayFrom(ctx context.Context, sinceSeq uint64, fn func(contracts.Event) error) error
ReplayFrom reads events from the WAL starting at the given sequence number and invokes the callback for each event. If sinceSeq is 0, replays from the beginning. The callback is called synchronously; it should not block.
func (*WALWriter) Segments ¶ added in v0.4.0
func (w *WALWriter) Segments() []WALSegment
Segments returns metadata for all WAL segments.
func (*WALWriter) SetMinSubscriberSeq ¶ added in v0.4.0
SetMinSubscriberSeq informs the WAL of the lowest subscriber sequence number. Segments with LastSeq < minSeq can be pruned.