eventbus

package
v1.0.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package eventbus provides application-owned, typed event subscriptions.

Index

Constants

This section is empty.

Variables

View Source
var ErrOverflow = errors.New("eventbus: subscriber capacity exceeded")

Functions

This section is empty.

Types

type Bus

type Bus[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any]() *Bus[T]

func (*Bus[T]) Emit

func (b *Bus[T]) Emit(event T)

func (*Bus[T]) Subscribe

func (b *Bus[T]) Subscribe(handler func(T)) func()

Subscribe preserves synchronous delivery. Handlers run outside the bus lock and must synchronize their own state when producers emit concurrently.

func (*Bus[T]) SubscribeAsync

func (b *Bus[T]) SubscribeAsync(opts SubscribeOptions[T], handler func(T) error) (*Subscription[T], error)

func (*Bus[T]) SubscribeFiltered

func (b *Bus[T]) SubscribeFiltered(filter func(T) bool, handler func(T)) func()

SubscribeFiltered preserves synchronous delivery for consumers that need an immediate visibility boundary (for example a journal before a file switch). Use SubscribeAsync for independently bounded, non-blocking consumers.

type SubscribeOptions

type SubscribeOptions[T any] struct {
	Filter   func(T) bool
	Buffer   int
	MaxBytes int64
	Size     func(T) int64
	Clone    func(T) T
	OnError  func(error)
	OnDrop   func(uint64)
}

SubscribeOptions bounds pending events, including the executing handler. Filter, Size and Clone run on the producer and must be fast. Clone transfers ownership before Emit returns. Other subscribers must treat events as immutable. OnError and OnDrop run on the worker, never on the producer. Callbacks must not wait on this subscription's Done or Close. Filter, Size and Clone must not reenter the subscription (including Err/Cancel).

type Subscription

type Subscription[T any] struct {
	// contains filtered or unexported fields
}

Subscription owns a serial worker. Cancel discards queued work; an admitted handler may finish. Close drains admitted work. Blocking handlers must observe their own cancellation context; the bus cannot interrupt user code.

func (*Subscription[T]) Cancel

func (s *Subscription[T]) Cancel()

func (*Subscription[T]) Close

func (s *Subscription[T]) Close(ctx context.Context) error

Close stops admission and waits for admitted work and callbacks to finish.

func (*Subscription[T]) Done

func (s *Subscription[T]) Done() <-chan struct{}

func (*Subscription[T]) Err

func (s *Subscription[T]) Err() error

func (*Subscription[T]) Stopped

func (s *Subscription[T]) Stopped() <-chan struct{}

Stopped closes as soon as admission stops, even if the handler is blocked.

Jump to

Keyboard shortcuts

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