Documentation
¶
Overview ¶
Package event provides local in-memory event bus implementation for gorp framework. Suitable for single-application internal event communication. Supports synchronous and asynchronous publishing, multiple handlers per event.
事件包提供本地内存事件总线实现,用于 gorp 框架。 适用于单体应用内部事件通信。 支持同步和异步发布、单事件多处理器。
Package event provides event bus provider for gorp framework. Registers local in-memory event bus as default implementation.
事件包提供事件总线 provider,用于 gorp 框架。 注册本地内存事件总线作为默认实现。
Index ¶
- type BaseEvent
- type LocalEventBus
- func (b *LocalEventBus) HasSubscribers(eventName string) bool
- func (b *LocalEventBus) Publish(ctx context.Context, event integrationcontract.Event) error
- func (b *LocalEventBus) PublishAsync(ctx context.Context, event integrationcontract.Event) error
- func (b *LocalEventBus) Subscribe(eventName string, handler integrationcontract.EventHandler)
- func (b *LocalEventBus) Unsubscribe(eventName string)
- type Provider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseEvent ¶
type BaseEvent struct {
// contains filtered or unexported fields
}
BaseEvent 基础事件实现。
中文说明: - 提供事件的默认实现,可直接使用或作为嵌入; - 包含事件名称、载荷和时间戳。
func NewBaseEvent ¶
NewBaseEvent 创建基础事件。
func (*BaseEvent) OccurredAt ¶
type LocalEventBus ¶
type LocalEventBus struct {
// contains filtered or unexported fields
}
LocalEventBus 本地内存事件总线。
中文说明: - 基于 goroutine 的本地事件总线实现; - 支持同步和异步发布; - 支持一个事件多个处理器(广播模式); - 适合单体应用内部事件通信,后续可演进为 MQ。
func (*LocalEventBus) HasSubscribers ¶
func (b *LocalEventBus) HasSubscribers(eventName string) bool
HasSubscribers 检查事件是否有订阅者。
func (*LocalEventBus) Publish ¶
func (b *LocalEventBus) Publish(ctx context.Context, event integrationcontract.Event) error
Publish 同步发布事件。
中文说明: - 阻塞直到所有处理器执行完毕; - 处理器按注册顺序依次执行; - 如果某个处理器失败,记录错误但继续执行其他处理器; - 返回第一个遇到的错误。
func (*LocalEventBus) PublishAsync ¶
func (b *LocalEventBus) PublishAsync(ctx context.Context, event integrationcontract.Event) error
PublishAsync 异步发布事件。
中文说明: - 立即返回,事件在后台 goroutine 中处理; - 使用 goroutine.SafeGo 包装,确保 panic 不会导致程序崩溃; - 适合不需要等待处理完成的场景。
func (*LocalEventBus) Subscribe ¶
func (b *LocalEventBus) Subscribe(eventName string, handler integrationcontract.EventHandler)
Subscribe 订阅事件。
中文说明: - 注册事件处理器; - 同一事件可以有多个处理器,按注册顺序执行; - 处理器执行失败不会影响其他处理器的执行。
func (*LocalEventBus) Unsubscribe ¶
func (b *LocalEventBus) Unsubscribe(eventName string)
Unsubscribe 取消订阅事件。
中文说明: - 移除指定事件的所有处理器; - 用于清理或重新配置事件处理。
type Provider ¶
type Provider struct{}
Provider registers the event bus service contract. Core logic: Bind LocalEventBus factory to container.
Provider 注册事件总线服务契约。 核心逻辑:将 LocalEventBus 工厂绑定到容器。
func NewProvider ¶
func NewProvider() *Provider
NewProvider creates a new event bus provider.
NewProvider 创建新的事件总线 provider。
func (*Provider) Boot ¶
func (p *Provider) Boot(runtimecontract.Container) error
Boot initializes the event bus provider. No additional startup logic required.
Boot 初始化事件总线 provider。 无需额外启动逻辑。
func (*Provider) IsDefer ¶
IsDefer indicates event bus should not defer loading. Events may be needed early in application lifecycle.
IsDefer 表示事件总线不应延迟加载。 事件可能在应用生命周期早期就需要。