2024-11-03 21:28:07 +08:00
|
|
|
package contract
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
|
|
|
const QueueKey = "hade:queue"
|
|
|
|
|
|
|
|
type QueueService interface {
|
|
|
|
// PublishEvent 发布事件到队列,并持久化
|
|
|
|
PublishEvent(ctx context.Context, event Event) error
|
|
|
|
|
|
|
|
// SubscribeEvent 订阅事件
|
|
|
|
SubscribeEvent(ctx context.Context, topic string, handler func(event Event) error) error
|
|
|
|
|
2024-11-17 21:38:55 +08:00
|
|
|
// ProcessSubscribe 执行订阅
|
|
|
|
ProcessSubscribe()
|
|
|
|
|
|
|
|
// RegisterSubscribe 注册订阅 订阅事件
|
|
|
|
RegisterSubscribe(topic string, handler func(event Event) error) error
|
|
|
|
|
2024-11-03 21:28:07 +08:00
|
|
|
// ReplayEvents 从指定的时间点或事件ID开始重放事件
|
|
|
|
ReplayEvents(ctx context.Context, topic string, fromID string, fromTimestamp int64, handler func(event Event) error) error
|
|
|
|
|
2024-11-17 21:38:55 +08:00
|
|
|
// GetRegisterSubscribe 根据主题获取已经注册的订阅
|
|
|
|
GetRegisterSubscribe(topic string) []EventHandler
|
|
|
|
|
2024-11-03 21:28:07 +08:00
|
|
|
// GetEventById 根据事件ID获取事件
|
|
|
|
GetEventById(ctx context.Context, topic string, eventID string) (Event, error)
|
|
|
|
|
|
|
|
// GetEventByTime 根据事件ID获取事件
|
|
|
|
GetEventByTime(ctx context.Context, topic string, fromTimestamp int64) (Event, error)
|
|
|
|
|
|
|
|
// Close 关闭队列连接
|
|
|
|
Close() error
|
|
|
|
|
|
|
|
// NewEventAndPublish 创建并推送事件方法
|
|
|
|
NewEventAndPublish(ctx context.Context, topic string, payload interface{}) error
|
2024-11-17 21:38:55 +08:00
|
|
|
|
|
|
|
// SetContext 为订阅设置上下文
|
|
|
|
SetContext(ctx context.Context)
|
|
|
|
|
|
|
|
// GetContext 为订阅设置上下文
|
|
|
|
GetContext() context.Context
|
2024-11-03 21:28:07 +08:00
|
|
|
}
|
|
|
|
|
2024-11-17 21:38:55 +08:00
|
|
|
type EventHandler func(event Event) error
|
|
|
|
|
2024-11-03 21:28:07 +08:00
|
|
|
type Event interface {
|
2024-11-13 23:01:41 +08:00
|
|
|
GetEventKey() string // 事件唯一标识
|
2024-11-03 21:28:07 +08:00
|
|
|
EventTopic() string // 事件类型
|
|
|
|
EventTimestamp() int64 // 事件发生时间
|
|
|
|
Payload() interface{} // 事件负载
|
|
|
|
}
|