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"
)
var NoLockServerError = fmt.Errorf("no lock server found")
type DistributedLockManager struct {
lockManager *LockManager
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) {
servers := dlm.LockRing.GetSnapshot()
if servers == nil {
err = fmt.Errorf("no lock server found")
err = NoLockServerError
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) {
servers := dlm.LockRing.GetSnapshot()
if servers == nil {
err = fmt.Errorf("no lock server found")
err = NoLockServerError
return
}

View file

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