Initial commit: Basic project structure and dependencies
This commit is contained in:
92
internal/shared/interfaces/event.go
Normal file
92
internal/shared/interfaces/event.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Event 事件接口
|
||||
type Event interface {
|
||||
// 事件基础信息
|
||||
GetID() string
|
||||
GetType() string
|
||||
GetVersion() string
|
||||
GetTimestamp() time.Time
|
||||
|
||||
// 事件数据
|
||||
GetPayload() interface{}
|
||||
GetMetadata() map[string]interface{}
|
||||
|
||||
// 事件来源
|
||||
GetSource() string
|
||||
GetAggregateID() string
|
||||
GetAggregateType() string
|
||||
|
||||
// 序列化
|
||||
Marshal() ([]byte, error)
|
||||
Unmarshal(data []byte) error
|
||||
}
|
||||
|
||||
// EventHandler 事件处理器接口
|
||||
type EventHandler interface {
|
||||
// 处理器标识
|
||||
GetName() string
|
||||
GetEventTypes() []string
|
||||
|
||||
// 事件处理
|
||||
Handle(ctx context.Context, event Event) error
|
||||
|
||||
// 处理器配置
|
||||
IsAsync() bool
|
||||
GetRetryConfig() RetryConfig
|
||||
}
|
||||
|
||||
// DomainEvent 领域事件基础接口
|
||||
type DomainEvent interface {
|
||||
Event
|
||||
|
||||
// 领域特定信息
|
||||
GetDomainVersion() string
|
||||
GetCausationID() string
|
||||
GetCorrelationID() string
|
||||
}
|
||||
|
||||
// RetryConfig 重试配置
|
||||
type RetryConfig struct {
|
||||
MaxRetries int `json:"max_retries"`
|
||||
RetryDelay time.Duration `json:"retry_delay"`
|
||||
BackoffFactor float64 `json:"backoff_factor"`
|
||||
MaxDelay time.Duration `json:"max_delay"`
|
||||
}
|
||||
|
||||
// EventStore 事件存储接口
|
||||
type EventStore interface {
|
||||
// 事件存储
|
||||
SaveEvent(ctx context.Context, event Event) error
|
||||
SaveEvents(ctx context.Context, events []Event) error
|
||||
|
||||
// 事件查询
|
||||
GetEvents(ctx context.Context, aggregateID string, fromVersion int) ([]Event, error)
|
||||
GetEventsByType(ctx context.Context, eventType string, limit int) ([]Event, error)
|
||||
GetEventsSince(ctx context.Context, timestamp time.Time, limit int) ([]Event, error)
|
||||
|
||||
// 快照支持
|
||||
SaveSnapshot(ctx context.Context, aggregateID string, snapshot interface{}) error
|
||||
GetSnapshot(ctx context.Context, aggregateID string) (interface{}, error)
|
||||
}
|
||||
|
||||
// EventBus 事件总线接口
|
||||
type EventBus interface {
|
||||
// 事件发布
|
||||
Publish(ctx context.Context, event Event) error
|
||||
PublishBatch(ctx context.Context, events []Event) error
|
||||
|
||||
// 事件订阅
|
||||
Subscribe(eventType string, handler EventHandler) error
|
||||
Unsubscribe(eventType string, handler EventHandler) error
|
||||
|
||||
// 订阅管理
|
||||
GetSubscribers(eventType string) []EventHandler
|
||||
Start(ctx context.Context) error
|
||||
Stop(ctx context.Context) error
|
||||
}
|
||||
Reference in New Issue
Block a user