Help us shape the future of Go! Take the 2026 Go Developer Survey

event

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package event provides EventBus options for retry and DLQ configurations.

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

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

func NewBaseEvent(name string, payload interface{}) *BaseEvent

NewBaseEvent 创建基础事件。

func (*BaseEvent) Name

func (e *BaseEvent) Name() string

func (*BaseEvent) OccurredAt

func (e *BaseEvent) OccurredAt() time.Time

func (*BaseEvent) Payload

func (e *BaseEvent) Payload() interface{}

type DLQHandler added in v0.2.0

type DLQHandler func(ctx context.Context, event integrationcontract.Event, err error)

DLQHandler is the callback function invoked when an event exhausts all retry attempts.

type EventBusOptions added in v0.2.0

type EventBusOptions struct {
	MaxRetries       int           // Maximum retry attempts (default: 3)
	InitialBackoff   time.Duration // Initial retry delay (default: 50ms)
	MaxBackoff       time.Duration // Maximum retry delay (default: 1s)
	EnableDLQ        bool          // Enable Dead-Letter Queue routing (default: true)
	DLQHandler       DLQHandler    // Custom DLQ handler
	AsyncWorkerCount int           // Number of background workers for PublishAsync (default: 8)
	AsyncQueueSize   int           // Buffer size for async event queue (default: 1024)
}

EventBusOptions configures the EventBus behavior including exponential retries and DLQ.

func DefaultEventBusOptions added in v0.2.0

func DefaultEventBusOptions() EventBusOptions

DefaultEventBusOptions returns sensible defaults for production.

type LocalEventBus

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

func NewLocalEventBus

func NewLocalEventBus(opts ...Option) *LocalEventBus

NewLocalEventBus 创建具备重试、DLQ 与异步 Worker 协程池能力的本地事件总线。

func (*LocalEventBus) Close added in v0.2.0

func (b *LocalEventBus) Close() error

Close stops the background async workers and releases resources. Implements io.Closer for container lifecycle cleanup.

func (*LocalEventBus) HasSubscribers

func (b *LocalEventBus) HasSubscribers(eventName string) bool

HasSubscribers 检查事件是否有订阅者。

func (*LocalEventBus) Publish

Publish 同步发布事件(带有指数退避重试与死信队列 DLQ 路由)。

func (*LocalEventBus) PublishAsync

func (b *LocalEventBus) PublishAsync(ctx context.Context, event integrationcontract.Event) error

PublishAsync 异步发布事件(通过定长 Worker 协程池削峰处理)。

func (*LocalEventBus) Subscribe

func (b *LocalEventBus) Subscribe(eventName string, handler integrationcontract.EventHandler)

Subscribe 订阅事件。

func (*LocalEventBus) Unsubscribe

func (b *LocalEventBus) Unsubscribe(eventName string)

Unsubscribe 取消订阅事件。

中文说明: - 移除指定事件的所有处理器; - 用于清理或重新配置事件处理。

type Option added in v0.2.0

type Option func(*EventBusOptions)

Option configures EventBusOptions.

func WithAsyncWorkers added in v0.2.0

func WithAsyncWorkers(workerCount, queueSize int) Option

WithAsyncWorkers configures the worker pool and queue size for asynchronous event publishing.

func WithBackoff added in v0.2.0

func WithBackoff(initial, max time.Duration) Option

WithBackoff configures exponential backoff delays.

func WithDLQHandler added in v0.2.0

func WithDLQHandler(handler DLQHandler) Option

WithDLQHandler configures the Dead Letter Queue handler callback.

func WithMaxRetries added in v0.2.0

func WithMaxRetries(maxRetries int) Option

WithMaxRetries configures max retry attempts.

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

Boot initializes the event bus provider. No additional startup logic required.

Boot 初始化事件总线 provider。 无需额外启动逻辑。

func (*Provider) IsDefer

func (p *Provider) IsDefer() bool

IsDefer indicates event bus should not defer loading. Events may be needed early in application lifecycle.

IsDefer 表示事件总线不应延迟加载。 事件可能在应用生命周期早期就需要。

func (*Provider) Name

func (p *Provider) Name() string

Name returns provider name for identification.

Name 返回 provider 名称,用于标识。

func (*Provider) Provides

func (p *Provider) Provides() []string

Provides returns the capability keys this provider exposes. Exposes EventKey for event bus service.

Provides 返回 provider 暴露的能力键。 暴露 EventKey 用于事件总线服务。

func (*Provider) Register

func (p *Provider) Register(c runtimecontract.Container) error

Register binds the event bus factory to the container. Core logic: Create LocalEventBus, bind to container.

Register 将事件总线工厂绑定到容器。 核心逻辑:创建 LocalEventBus、绑定到容器。

Jump to

Keyboard shortcuts

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