adjust errors

This commit is contained in:
chrislu 2023-06-25 17:37:54 -07:00
parent 06471dac9d
commit 13a8593ab9
2 changed files with 13 additions and 6 deletions

View file

@ -6,6 +6,8 @@ import (
"time" "time"
) )
var NoLockServerError = fmt.Errorf("no lock server found")
type DistributedLockManager struct { type DistributedLockManager struct {
lockManager *LockManager lockManager *LockManager
LockRing *LockRing LockRing *LockRing
@ -21,7 +23,7 @@ func NewDistributedLockManager() *DistributedLockManager {
func (dlm *DistributedLockManager) Lock(host pb.ServerAddress, key string, expiredAtNs int64, token string) (renewToken string, movedTo pb.ServerAddress, err error) { func (dlm *DistributedLockManager) Lock(host pb.ServerAddress, key string, expiredAtNs int64, token string) (renewToken string, movedTo pb.ServerAddress, err error) {
servers := dlm.LockRing.GetSnapshot() servers := dlm.LockRing.GetSnapshot()
if servers == nil { if servers == nil {
err = fmt.Errorf("no lock server found") err = NoLockServerError
return return
} }
@ -37,7 +39,7 @@ func (dlm *DistributedLockManager) Lock(host pb.ServerAddress, key string, expir
func (dlm *DistributedLockManager) Unlock(host pb.ServerAddress, key string, token string) (movedTo pb.ServerAddress, err error) { func (dlm *DistributedLockManager) Unlock(host pb.ServerAddress, key string, token string) (movedTo pb.ServerAddress, err error) {
servers := dlm.LockRing.GetSnapshot() servers := dlm.LockRing.GetSnapshot()
if servers == nil { if servers == nil {
err = fmt.Errorf("no lock server found") err = NoLockServerError
return return
} }

View file

@ -7,6 +7,11 @@ import (
"time" "time"
) )
var LockErrorNonEmptyTokenOnNewLock = fmt.Errorf("lock: non-empty token on a new lock")
var LockErrorNonEmptyTokenOnExpiredLock = fmt.Errorf("lock: non-empty token on an expired lock")
var LockErrorTokenMismatch = fmt.Errorf("lock: token mismatch")
var UnlockErrorTokenMismatch = fmt.Errorf("unlock: token mismatch")
// LockManager lock manager // LockManager lock manager
type LockManager struct { type LockManager struct {
locks *xsync.MapOf[string, *Lock] locks *xsync.MapOf[string, *Lock]
@ -31,7 +36,7 @@ func (lm *LockManager) Lock(path string, expiredAtNs int64, token string) (renew
if oldValue.ExpiredAtNs > 0 && oldValue.ExpiredAtNs < time.Now().UnixNano() { if oldValue.ExpiredAtNs > 0 && oldValue.ExpiredAtNs < time.Now().UnixNano() {
// lock is expired, set to a new lock // lock is expired, set to a new lock
if token != "" { if token != "" {
err = fmt.Errorf("lock: non-empty token on an expired lock") err = LockErrorNonEmptyTokenOnExpiredLock
return nil, false return nil, false
} else { } else {
// new lock // new lock
@ -45,7 +50,7 @@ func (lm *LockManager) Lock(path string, expiredAtNs int64, token string) (renew
renewToken = uuid.New().String() renewToken = uuid.New().String()
return &Lock{Token: renewToken, ExpiredAtNs: expiredAtNs}, false return &Lock{Token: renewToken, ExpiredAtNs: expiredAtNs}, false
} else { } else {
err = fmt.Errorf("lock: token mismatch") err = LockErrorTokenMismatch
return oldValue, false return oldValue, false
} }
} else { } else {
@ -54,7 +59,7 @@ func (lm *LockManager) Lock(path string, expiredAtNs int64, token string) (renew
renewToken = uuid.New().String() renewToken = uuid.New().String()
return &Lock{Token: renewToken, ExpiredAtNs: expiredAtNs}, false return &Lock{Token: renewToken, ExpiredAtNs: expiredAtNs}, false
} else { } else {
err = fmt.Errorf("lock: non-empty token on a new lock") err = LockErrorNonEmptyTokenOnNewLock
return nil, false return nil, false
} }
} }
@ -79,7 +84,7 @@ func (lm *LockManager) Unlock(path string, token string) (isUnlocked bool, err e
return oldValue, false return oldValue, false
} else { } else {
isUnlocked = false isUnlocked = false
err = fmt.Errorf("unlock: token mismatch") err = UnlockErrorTokenMismatch
return oldValue, false return oldValue, false
} }
} else { } else {