/
error.go
44 lines (36 loc) · 1 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package mortise
import (
"fmt"
)
// fencing token outdated
type ErrOutdatedToken struct {
currentToken int64
holdToken int64
}
func (e *ErrOutdatedToken) Error() string {
return fmt.Sprintf("key:%d outdated, current key is %d", e.holdToken, e.currentToken)
}
// mutex has been acquired and still not release
type ErrMutexOccupied struct {
currentToken int64
holdToken int64
}
func (e *ErrMutexOccupied) Error() string {
return fmt.Sprintf("key:%d has been occupied by %d", e.holdToken, e.currentToken)
}
// redis internal error
type ErrRedis struct {
err error
}
func (e *ErrRedis) Error() string {
return fmt.Sprintf("redis exception:%+v", e.err)
}
// compare fencing token and return proper error
func processFencingToken(lockedToken int64, haveToken int64) error {
if lockedToken > haveToken {
return &ErrOutdatedToken{currentToken: lockedToken, holdToken: haveToken}
} else if lockedToken < haveToken {
return &ErrMutexOccupied{currentToken: lockedToken, holdToken: haveToken}
}
return nil
}