124 lines
4.4 KiB
Go
124 lines
4.4 KiB
Go
|
|
package constant
|
||
|
|
|
||
|
|
import "fmt"
|
||
|
|
|
||
|
|
type RedisResult int
|
||
|
|
|
||
|
|
const (
|
||
|
|
LockSuccess = RedisResult(1)
|
||
|
|
UnLockSuccess = RedisResult(1)
|
||
|
|
RefreshLockSuccess = RedisResult(1)
|
||
|
|
UnRLockSuccess = RedisResult(0)
|
||
|
|
RLockFailure = RedisResult(-1)
|
||
|
|
UnRLockFailureWithWLockOccupancy = RedisResult(-2)
|
||
|
|
WLockFailureWithRLockOccupancy = RedisResult(-3)
|
||
|
|
WLockFailureWithWLockOccupancy = RedisResult(-4)
|
||
|
|
UnWLockFailureWithRLockOccupancy = RedisResult(-5)
|
||
|
|
UnWLockFailureWithWLockOccupancy = RedisResult(-6)
|
||
|
|
WLockFailureWithNotFirstPriority = RedisResult(-7)
|
||
|
|
RefreshLockFailure = RedisResult(-8)
|
||
|
|
UnknownInternalError = RedisResult(-99)
|
||
|
|
)
|
||
|
|
|
||
|
|
type RedisLockType int
|
||
|
|
|
||
|
|
const (
|
||
|
|
LockType = RedisLockType(iota)
|
||
|
|
UnLockType
|
||
|
|
RefreshLockType
|
||
|
|
)
|
||
|
|
|
||
|
|
type RedisError struct {
|
||
|
|
Code RedisResult
|
||
|
|
Message string
|
||
|
|
}
|
||
|
|
|
||
|
|
func (e *RedisError) Error() string {
|
||
|
|
return fmt.Sprintf("redis execution code:%d,message:%s\n", e.Code, e.Message)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (e *RedisError) OutputResultMessage() string {
|
||
|
|
return e.Message
|
||
|
|
}
|
||
|
|
|
||
|
|
func (e *RedisError) OutputResultCode() int {
|
||
|
|
return int(e.Code)
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewRedisError(res RedisResult) error {
|
||
|
|
resInt := int(res)
|
||
|
|
switch resInt {
|
||
|
|
case -1:
|
||
|
|
return &RedisError{Code: -1, Message: "redis lock read lock failure,the lock is already occupied by another processes write lock"}
|
||
|
|
default:
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func ConvertResultToErr(res RedisResult, lockType RedisLockType, redisMsg string) error {
|
||
|
|
resInt := int(res)
|
||
|
|
switch resInt {
|
||
|
|
case 1:
|
||
|
|
if lockType == LockType {
|
||
|
|
return &RedisError{Code: res, Message: "redis lock success"}
|
||
|
|
} else if lockType == UnLockType {
|
||
|
|
return &RedisError{Code: res, Message: "redis unlock success"}
|
||
|
|
} else {
|
||
|
|
return &RedisError{Code: res, Message: "redis refresh lock success"}
|
||
|
|
}
|
||
|
|
case 0:
|
||
|
|
return &RedisError{Code: res, Message: "redis unlock read lock success, the lock is still occupied by other processes read lock"}
|
||
|
|
case -1:
|
||
|
|
return &RedisError{Code: res, Message: "redis lock read lock failure,the lock is already occupied by another processes write lock"}
|
||
|
|
case -2:
|
||
|
|
return &RedisError{Code: res, Message: "redis un lock read lock failure,the lock is already occupied by another processes write lock"}
|
||
|
|
case -3:
|
||
|
|
return &RedisError{Code: res, Message: "redis lock write lock failure,the lock is already occupied by anthor processes read lock"}
|
||
|
|
case -4:
|
||
|
|
return &RedisError{Code: res, Message: "redis lock write lock failure,the lock is already occupied by anthor processes write lock"}
|
||
|
|
case -5:
|
||
|
|
return &RedisError{Code: res, Message: "redis un lock write lock failure,the lock is already occupied by another processes read lock"}
|
||
|
|
case -6:
|
||
|
|
return &RedisError{Code: res, Message: "redis un lock write lock failure,the lock is already occupied by another processes write lock"}
|
||
|
|
case -7:
|
||
|
|
return &RedisError{Code: res, Message: "redis lock write lock failure,the first priority in the current process non-waiting queue"}
|
||
|
|
case -8:
|
||
|
|
return &RedisError{Code: res, Message: "redis refresh lock failure,the lock not exist"}
|
||
|
|
default:
|
||
|
|
return &RedisError{Code: res, Message: fmt.Sprintf("unkown redis execution result:%s\n", redisMsg)}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TranslateResultToStr(res RedisResult, lockType RedisLockType) string {
|
||
|
|
resInt := int(res)
|
||
|
|
switch resInt {
|
||
|
|
case 1:
|
||
|
|
if lockType == LockType {
|
||
|
|
return "redis lock success"
|
||
|
|
} else if lockType == UnLockType {
|
||
|
|
return "redis unlock success"
|
||
|
|
} else {
|
||
|
|
return "redis refresh lock success"
|
||
|
|
}
|
||
|
|
case 0:
|
||
|
|
return "redis unlock read lock success, the lock is still occupied by other processes read lock"
|
||
|
|
case -1:
|
||
|
|
return "redis lock read lock failure,the lock is already occupied by another processes write lock"
|
||
|
|
case -2:
|
||
|
|
return "redis un lock read lock failure,the lock is already occupied by another processes write lock"
|
||
|
|
case -3:
|
||
|
|
return "redis lock write lock failure,the lock is already occupied by anthor processes read lock"
|
||
|
|
case -4:
|
||
|
|
return "redis lock write lock failure,the lock is already occupied by anthor processes write lock"
|
||
|
|
case -5:
|
||
|
|
return "redis un lock write lock failure,the lock is already occupied by another processes read lock"
|
||
|
|
case -6:
|
||
|
|
return "redis un lock write lock failure,the lock is already occupied by another processes write lock"
|
||
|
|
case -7:
|
||
|
|
return "redis lock write lock failure,the first priority in the current process non-waiting queue"
|
||
|
|
case -8:
|
||
|
|
return "redis refresh lock failure,the lock not exist"
|
||
|
|
}
|
||
|
|
return "unkown redis execution result"
|
||
|
|
}
|