-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheventbus.go
More file actions
45 lines (37 loc) · 1.31 KB
/
eventbus.go
File metadata and controls
45 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package eventbus
import (
"context"
)
var (
// Version 版本
Version = "v1"
)
// Subscriber 订阅接口
type Subscriber interface {
Subscribe(ctx context.Context, topic string, h SubscribeHandler) (err error)
SubscribeAsync(ctx context.Context, topic string, h SubscribeHandler) (err error)
}
// SubscribeHandler 订阅处理
type SubscribeHandler func(ctx context.Context, msg *Message) error
// Publisher 发布接口
type Publisher interface {
Publish(ctx context.Context, topic string, v interface{}) (err error)
PublishAsync(ctx context.Context, topic string, v interface{}) (err error)
}
// EventBus 事件总线
type EventBus interface {
Publisher
Subscriber
}
// Closer 定义了可以关闭资源的接口
// 某些 EventBus 实现(如 RabbitMQ)需要清理内部资源(如连接池)
// 可以通过类型断言使用此接口: if closer, ok := bus.(eventbus.Closer); ok { closer.Close() }
type Closer interface {
Close() error
}
// StreamCleaner 定义了可以清理流资源的接口
// 某些基于流的 EventBus 实现(如 Redis Stream)需要手动清理 Stream 或消费组
// 可以通过类型断言使用此接口: if cleaner, ok := bus.(eventbus.StreamCleaner); ok { cleaner.CleanupStream(ctx, topic) }
type StreamCleaner interface {
CleanupStream(ctx context.Context, stream string) error
}