Documentation
¶
Index ¶
- Constants
- type Action
- func (x *Action) AddPayload(key string, value any) *Action
- func (x *Action) ClearErr() *Action
- func (x *Action) ClearPayloadMap() *Action
- func (x *Action) Cost() time.Duration
- func (x *Action) End() *Action
- func (x *Action) ErrorIs(err error) bool
- func (x *Action) GetErrMsg() string
- func (x *Action) GetPayload(key string) (any, bool)
- func (x *Action) GetPayloadAsInt(key string) int
- func (x *Action) GetPayloadAsString(key string) string
- func (x *Action) GetPayloadMap() map[string]any
- func (x *Action) SetErr(err error) *Action
- func (x *Action) SetName(name string) *Action
- func (x *Action) SetPayloadMap(payloadMap map[string]any) *Action
- type Event
- func (x *Event) AddAction(action *Action) *Event
- func (x *Event) AddActionByName(actionName string) *Event
- func (x *Event) AddListeners(listener Listener) *Event
- func (x *Event) ClearListeners() *Event
- func (x *Event) End() *Event
- func (x *Event) Fork() *Event
- func (x *Event) GetParentID() string
- func (x *Event) IsRootEvent() bool
- func (x *Event) Publish(ctx context.Context, listeners ...Listener)
- func (x *Event) SetErr(err error) *Event
- func (x *Event) SetListeners(listeners []Listener) *Event
- func (x *Event) SetLockId(lockId string) *Event
- func (x *Event) SetLockInformation(lockInformation *storage.LockInformation) *Event
- func (x *Event) SetOwnerId(ownerId string) *Event
- func (x *Event) SetParent(event *Event) *Event
- func (x *Event) SetRootID(rootID string) *Event
- func (x *Event) SetStorageName(storageName string) *Event
- func (x *Event) SetType(eventType EventType) *Event
- func (x *Event) SetWatchDogId(watchDogId string) *Event
- func (x *Event) ToJsonString() string
- func (x *Event) ToJsonStringE() (string, error)
- type EventType
- type Eventable
- type Listener
- type ListenerWrapper
Constants ¶
View Source
const EventIDPrefix = "storage-lock-event-"
EventIDPrefix 事件的ID的前缀,用于方便区分是否是事件的ID
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action struct {
// Action被创建的时间
StartTime *time.Time `json:"start_time"`
// Action结束的时间,有时候会用不到
EndTime *time.Time `json:"end_time"`
// Action的名字
Name string `json:"name"`
// 执行此Action时发生的错误
Err error `json:"err"`
// action可以携带一些自己单独的上下文信息之类的
PayloadMap map[string]any `json:"payload_map"`
}
Action 一个事件可以添加若干个Action,每个Action都会有一些时间、名称、上下文之类的
func (*Action) AddPayload ¶
AddPayload 往action上增加payload
func (*Action) ClearPayloadMap ¶
ClearPayloadMap 清空payloadMap
func (*Action) GetPayload ¶
GetPayload 从payloadMap中获取value
func (*Action) GetPayloadAsInt ¶
GetPayloadAsInt 获取payload作为int返回
func (*Action) GetPayloadAsString ¶
GetPayloadAsString 从payloadMap中获取value,以string返回
func (*Action) GetPayloadMap ¶
type Event ¶
type Event struct {
// 事件的唯一ID
ID string `json:"id"`
// 此事件树的树根ID,用于对事件进行聚合分组
RootID string `json:"root_id"`
// 如果此事件拥有父事件,则保留一个指向父事件的引用
ParentID string `json:"parent_id"`
Parent *Event `json:"-"`
// 事件可以绑定到一个锁
LockId string `json:"lock_id"`
// 当前的事件是由谁产生的
OwnerId string `json:"owner_id"`
// 事件可以绑定到一个Storage
StorageName string `json:"storage_name"`
// 事件拥有开始时间和结束时间,可以通过这个来借助事件进行一些性能统计之类的
StartTime *time.Time `json:"start_time"`
EndTime *time.Time `json:"end_time"`
// 事件的类型
EventType EventType `json:"event_type"`
// 事件持续过程中的一些行为记录
Actions []*Action `json:"actions"`
// 事件可以绑定一个WatchDog
WatchDogId string `json:"watch_dog_id"`
// 事件可以绑定到锁信息
LockInformation *storage.LockInformation `json:"lock_information"`
// 如果有事件级别的错误,则可以放在这里,不过还是推荐优先使用Action级别的错误对Action进行精准绑定
Err error `json:"err"`
// 订阅了此时间的Listener都有哪些
Listeners []Listener `json:"-"`
}
Event 表示一个事件
func EventFromJsonStringE ¶
func (*Event) AddActionByName ¶
AddActionByName 添加Action名,一般用于只需要记录名称和时间不需要记录其它字段的时候
func (*Event) AddListeners ¶
func (*Event) ClearListeners ¶
func (*Event) GetParentID ¶
func (*Event) IsRootEvent ¶
IsRootEvent 此事件是否是最底层的事件,整个事件的fork关系可以看做是一颗树
func (*Event) SetListeners ¶
func (*Event) SetLockInformation ¶
func (x *Event) SetLockInformation(lockInformation *storage.LockInformation) *Event
func (*Event) SetOwnerId ¶
func (*Event) SetStorageName ¶
func (*Event) SetWatchDogId ¶
func (*Event) ToJsonString ¶
func (*Event) ToJsonStringE ¶
type Listener ¶
type Listener interface {
// Name 当有多个事件监听器的时候用于区分彼此
Name() string
// On 每次事件被触发的时候此函数被执行一次
On(ctx context.Context, e *Event)
}
Listener 事件监听器,当有任何事件的时候都会触发此监听器把事件传给它处理
type ListenerWrapper ¶
type ListenerWrapper struct {
// contains filtered or unexported fields
}
ListenerWrapper 如果不想声明struct实现Listener接口的话,可以使用这个struct来对函数来包裹函数实现
func NewListenerWrapper ¶
func NewListenerWrapper(name string, listenerFunc func(ctx context.Context, e *Event)) *ListenerWrapper
func (*ListenerWrapper) Name ¶
func (x *ListenerWrapper) Name() string
Click to show internal directories.
Click to hide internal directories.