bus

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package bus provides a lightweight, type-safe in-process message bus. Supports 1:N pub/sub and 1:1 typed command routing via TypedBus.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoHandler      = errors.New("bus: no handler for key")
	ErrPanicRecovered = errors.New("panic recovered")
)

Functions

func Register

func Register[Req, Res any](b *TypedBus, h TypedHandler[Req, Res])

Register adds a command handler. Last registration wins.

func Send

func Send[Req, Res any](b *TypedBus, ctx context.Context, req Req) (Res, error)

Send dispatches a command to its registered handler.

Types

type Bus

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

Bus is a generic pub/sub message bus. Safe for concurrent use.

func New

func New[T any](mws ...Middleware[T]) *Bus[T]

New creates a Bus with optional middleware applied to all subscribers.

func (*Bus[T]) Publish

func (b *Bus[T]) Publish(ctx context.Context, subject string, msg T) error

Publish delivers msg to all handlers for subject. Returns first error; all handlers still run.

func (*Bus[T]) Subscribe

func (b *Bus[T]) Subscribe(subject string, h Handler[T])

Subscribe registers a handler. Middleware is applied at subscribe time — zero alloc on Publish.

type Handler

type Handler[T any] func(ctx context.Context, msg T) error

Handler is the subscriber function signature.

type Middleware

type Middleware[T any] func(Handler[T]) Handler[T]

Middleware wraps a Handler for cross-cutting concerns.

func Logging

func Logging[T any](log *slog.Logger) Middleware[T]

func Recover

func Recover[T any](log *slog.Logger) Middleware[T]

func Timeout

func Timeout[T any](d time.Duration) Middleware[T]

type TypedBus

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

func NewTyped

func NewTyped() *TypedBus

type TypedHandler

type TypedHandler[Req, Res any] func(ctx context.Context, req Req) (Res, error)

Directories

Path Synopsis
Package mediator provides a CQRS-style mediator.
Package mediator provides a CQRS-style mediator.

Jump to

Keyboard shortcuts

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