Documentation
¶
Overview ¶
Package event 提供跨进程事件总线抽象:发布 / 按消费者组订阅。
本包不含业务事件名或载荷定义;业务目录放在使用方。
Handler 两类结果:
- 业务结果:自行打日志后 return nil;若 return 普通 error,框架仅打印后 ACK(不重试)
- 通信/基础设施失败:return event.NoAck(err),框架不 ACK,保留 pending 重试
Handler 须幂等。
Index ¶
- Variables
- func IsNoAck(err error) bool
- func IsNonIgnorable(err error) booldeprecated
- func NoAck(err error) error
- func NoAckf(format string, args ...any) error
- func NonIgnorable(err error) errordeprecated
- func NonIgnorableF(format string, args ...any) errordeprecated
- func SetDebug(debug bool)
- type Backend
- type Def
- type Envelope
- type Handler
- type NoAckError
- type NonIgnorableErrordeprecated
- type Publisher
- type StreamMessage
- type Subscriber
Constants ¶
This section is empty.
Variables ¶
View Source
var Debug = false
Functions ¶
func IsNonIgnorable
deprecated
func NonIgnorable
deprecated
func NonIgnorableF
deprecated
Types ¶
type Backend ¶
type Backend interface {
// XAdd 写入流。maxLen>0 时近似封顶(如 Redis MAXLEN ~);0 不裁剪。
XAdd(ctx context.Context, stream string, values map[string]string, maxLen int64) (id string, err error)
XGroupCreateMkStream(ctx context.Context, stream, group, start string) error
// XReadGroup id 为 ">"(仅新消息)或 "0"/"0-0"(本 consumer 的 pending)。
XReadGroup(ctx context.Context, stream, group, consumer string, count int64, block time.Duration, id string) ([]StreamMessage, error)
// XReadGroupMulti 一次读取多个 stream;ids 与 streams 等长(多为 ">" 或 "0-0")。
// block>0 且 ids 均为 ">" 时带 BLOCK。返回的 StreamMessage.Stream 为对应 stream key。
XReadGroupMulti(ctx context.Context, streams []string, group, consumer string, count int64, block time.Duration, ids []string) ([]StreamMessage, error)
// XAutoClaim 认领组内空闲过久的 pending(含其他 consumer)。
XAutoClaim(ctx context.Context, stream, group, consumer string, minIdle time.Duration, start string, count int64) (nextStart string, msgs []StreamMessage, err error)
// XPendingIDs 列出组内 pending 的消息 ID(最多 count 条)。
XPendingIDs(ctx context.Context, stream, group string, count int64) (ids []string, err error)
// XClaim 强制认领指定 pending(minIdle=0 表示立刻认领)。
XClaim(ctx context.Context, stream, group, consumer string, minIdle time.Duration, ids ...string) ([]StreamMessage, error)
XAck(ctx context.Context, stream, group string, ids ...string) error
}
Backend 底层流操作(由基础设施适配 Redis 等,本包不绑定具体驱动)。
type Def ¶
type Def struct {
Type string // Envelope.Type / topic
MaxLen int64 // Publish 时写入流的近似上限;0 跟随 Bus 默认
}
Def 业务事件目录项(名称与该 topic 留存策略,由业务包在常量旁声明)。
type Envelope ¶
type Envelope struct {
ID string // 后端生成的消息 ID(发布时可空)
Type string // 事件类型(由业务约定)
OccurredAt time.Time // 业务发生时间
Payload []byte // 业务载荷(通常 JSON)
Headers map[string]string // 可选扩展头
MaxLen int64 // 该次写入流近似上限;0 表示跟随 Bus 默认
}
Envelope 事件信封(传输层)。
type Handler ¶
Handler 处理一条事件。 业务异常:日志用,return nil 或普通 error(框架 ACK)。 通信异常:return NoAck(err)(框架不 ACK)。
type NoAckError ¶
type NoAckError struct {
Err error
}
NoAckError 通信/基础设施失败:订阅方不得 ACK,保留 pending 以便重试。 与业务异常分离——业务异常只用于打印/写日志(return nil,或 return 普通 err 由框架打日志后 ACK)。
func (*NoAckError) Error ¶
func (e *NoAckError) Error() string
func (*NoAckError) Unwrap ¶
func (e *NoAckError) Unwrap() error
type NonIgnorableError
deprecated
type NonIgnorableError = NoAckError
Deprecated: 使用 NoAckError。
Click to show internal directories.
Click to hide internal directories.