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) MarshalJSON() ([]byte, error)
- func (x *Action) SetErr(err error) *Action
- func (x *Action) SetName(name string) *Action
- func (x *Action) SetPayloadMap(payloadMap map[string]any) *Action
- func (x *Action) UnmarshalJSON(data []byte) error
- 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) MarshalJSON() ([]byte, error)
- 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)
- func (x *Event) UnmarshalJSON(data []byte) error
- type EventType
- type Eventable
- type Listener
- type ListenerWrapper
Constants ¶
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返回。 支持整数类型和可解析的十进制数字字符串;缺少key、溢出或类型不支持时返回0。
func (*Action) GetPayloadAsString ¶
GetPayloadAsString 从payloadMap中获取value并以string返回。 对于序列化后常见的字符串、数字和布尔值会转换为其文本表示; 缺少key或值类型不支持时返回空字符串。
func (*Action) GetPayloadMap ¶
func (*Action) MarshalJSON ¶ added in v0.0.5
func (*Action) SetPayloadMap ¶
SetPayloadMap 设置action的payload map
func (*Action) UnmarshalJSON ¶ added in v0.0.5
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) MarshalJSON ¶ added in v0.0.5
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 ¶
func (*Event) UnmarshalJSON ¶ added in v0.0.5
type EventType ¶
type EventType int
const ( // EventTypeUnknown 未设置事件类型 EventTypeUnknown EventType = iota // EventTypeCreateLock 创建锁的过程中产生的事件 EventTypeCreateLock // EventTypeLock 获取锁的过程中产生的事件 EventTypeLock // EventTypeUnlock 释放锁的过程中产生的事件 EventTypeUnlock // EventTypeWatchDog 看门狗产生的事件 EventTypeWatchDog // EventTypeExpired 锁因租约过期被抢占(记录是墓碑或超时后他人接管)。见 ActionLockExpired。 EventTypeExpired // EventTypeBusy 竞争锁时发现锁被他人持有而等待重试。见 ActionLockBusy。 EventTypeBusy // EventTypeRenewed 锁租约被看门狗或主动 Renew 成功续期。见 ActionWatchDogRefreshSuccess。 EventTypeRenewed // EventTypeLost 看门狗确认持有权丢失(他人抢占、锁删除或租约过期),业务侧应停止使用该锁。 // 对应 OnLockLost 回调与 ActionNotLockOwner/ErrLockLost 路径。 EventTypeLost // EventTypeWaitChanged 锁记录上的等待队列发生变化(有人排队、离开或队首接管)。 EventTypeWaitChanged )
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