eventbus

package
v1.33.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: 5 Imported by: 0

Documentation

Overview

Package eventbus 提供进程内事件总线(发布/订阅)。 支持同步与异步两种投递模式;异步模式下发布不阻塞调用方。 典型用途:业务模块解耦(订单创建后通知库存/通知/审计)、进程内观察者模式。

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 是进程内事件总线。

func New

func New(async bool) *Bus

New 创建事件总线;async 为 true 时 Publish 异步投递(每个订阅者独立 goroutine)。

func (*Bus) Publish

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

Publish 发布事件。 同步模式:按订阅顺序执行,任一订阅者返回错误即停止并返回该错误。 异步模式:每个订阅者在独立 goroutine 中执行,错误仅记录到日志输出(不返回)。

func (*Bus) Subscribe

func (b *Bus) Subscribe(event string, handler Handler) func()

Subscribe 订阅指定事件;返回取消订阅函数(幂等)。

func (*Bus) SubscribeAll

func (b *Bus) SubscribeAll(handler Handler) func()

SubscribeAll 订阅全部事件(用于日志、审计等横切关注点)。

type Event

type Event struct {
	// Name 是事件名(如 order.created),订阅者按名称匹配。
	Name string
	// Data 是事件附带数据。
	Data interface{}
}

Event 是事件负载。

type Handler

type Handler func(ctx context.Context, event Event) error

Handler 是事件处理函数;返回错误会中止同事件后续订阅者(同步模式)。

Jump to

Keyboard shortcuts

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