You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

distlock.go 1.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package distlock
  2. import "fmt"
  3. type Lock struct {
  4. Path []string // 锁路径,存储的是路径的每一部分
  5. Name string // 锁名
  6. Target any // 锁对象,由具体的Provider去解析
  7. }
  8. type LockRequest struct {
  9. Locks []Lock
  10. }
  11. func (b *LockRequest) Add(lock Lock) {
  12. b.Locks = append(b.Locks, lock)
  13. }
  14. type LockProvider interface {
  15. // CanLock 判断这个锁能否锁定成功
  16. CanLock(lock Lock) error
  17. // 锁定。在内部可以不用判断能否加锁,外部需要保证调用此函数前调用了CanLock进行检查
  18. Lock(reqID string, lock Lock) error
  19. // 解锁
  20. Unlock(reqID string, lock Lock) error
  21. // GetTargetString 将锁对象序列化为字符串,方便存储到ETCD
  22. GetTargetString(target any) (string, error)
  23. // ParseTargetString 解析字符串格式的锁对象数据
  24. ParseTargetString(targetStr string) (any, error)
  25. // Clear 清除内部所有状态
  26. Clear()
  27. }
  28. type LockTargetBusyError struct {
  29. lockName string
  30. }
  31. func (e *LockTargetBusyError) Error() string {
  32. return fmt.Sprintf("the lock object is locked by %s", e.lockName)
  33. }
  34. func NewLockTargetBusyError(lockName string) *LockTargetBusyError {
  35. return &LockTargetBusyError{
  36. lockName: lockName,
  37. }
  38. }

公共库

Contributors (1)