memory

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package memory implements an in-process event bus with sync/async dispatch, wildcard topic matching, and middleware support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

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

Bus is an in-memory event bus.

func New

func New(opts ...Option) *Bus

New creates a new in-memory event bus.

func (*Bus) Close

func (b *Bus) Close() error

Close shuts down the bus. For async mode, it drains the queue.

func (*Bus) Metrics

func (b *Bus) Metrics() eventbus.Metrics

Metrics returns a snapshot of bus metrics.

func (*Bus) Publish

func (b *Bus) Publish(ctx context.Context, e *eventbus.Event) error

Publish sends an event to all matching subscribers.

func (*Bus) Subscribe

func (b *Bus) Subscribe(topic string, handler eventbus.Handler) eventbus.Subscription

Subscribe registers a handler for the given topic pattern. Returns a Subscription that can be used with Unsubscribe.

func (*Bus) SubscriberCount

func (b *Bus) SubscriberCount(topic string) int

SubscriberCount returns the number of subscribers for a topic pattern.

func (*Bus) Topics

func (b *Bus) Topics() []string

Topics returns all registered topic patterns.

func (*Bus) Unsubscribe

func (b *Bus) Unsubscribe(sub eventbus.Subscription) error

Unsubscribe removes a subscription.

type DispatchMode

type DispatchMode int

DispatchMode controls how events are delivered to handlers.

const (
	// Sync dispatches events synchronously — Publish blocks until all
	// handlers complete. Errors from one handler stop delivery to
	// subsequent handlers.
	Sync DispatchMode = iota

	// Async dispatches events in goroutines — Publish returns immediately
	// after enqueuing. Handler errors are collected but not returned to
	// the publisher.
	Async

	// Parallel dispatches events to all handlers concurrently in
	// separate goroutines, then waits for all to complete. Publish
	// blocks until all handlers finish.
	Parallel
)

type Option

type Option func(*Bus)

Option configures the Bus.

func WithAsyncBufferSize

func WithAsyncBufferSize(n int) Option

WithAsyncBufferSize sets the buffer size for async dispatch. Only effective with Async mode. Default: 1024.

func WithDispatchMode

func WithDispatchMode(mode DispatchMode) Option

WithDispatchMode sets the dispatch mode (default: Sync).

func WithMiddleware

func WithMiddleware(mw ...eventbus.Middleware) Option

WithMiddleware adds middleware applied to all handlers.

Jump to

Keyboard shortcuts

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