-
Notifications
You must be signed in to change notification settings - Fork 17
/
lock.go
155 lines (143 loc) · 3.41 KB
/
lock.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package godis
import (
"errors"
"strconv"
"time"
)
//ErrLockTimeOut when get lock exceed the timeout,then return error
var ErrLockTimeOut = errors.New("get lock timeout")
//Lock different keys with different lock
type Lock struct {
name string
}
//Locker the lock client
type Locker struct {
timeout time.Duration
ch chan bool
pool *Pool
}
//NewLocker create new locker
func NewLocker(option *Option, lockOption *LockOption) *Locker {
if lockOption == nil {
lockOption = &LockOption{}
}
if lockOption.Timeout.Nanoseconds() == 0 {
lockOption.Timeout = 5 * time.Second
}
pool := NewPool(&PoolConfig{MaxTotal: 500}, option)
return &Locker{
timeout: lockOption.Timeout,
ch: make(chan bool, 1),
pool: pool,
}
}
//LockOption locker options
type LockOption struct {
Timeout time.Duration //lock wait timeout
}
//TryLock acquire a lock,when it returns a non nil locker,get lock success,
// otherwise, it returns an error,get lock failed
func (l *Locker) TryLock(key string) (*Lock, error) {
deadline := time.Now().Add(l.timeout)
value := strconv.FormatInt(deadline.UnixNano(), 10)
for {
redis, err := l.pool.GetResource()
if err != nil {
return nil, err
}
if time.Now().After(deadline) {
return nil, ErrLockTimeOut
}
status, err := redis.SetWithParamsAndTime(key, value, "nx", "px", l.timeout.Nanoseconds()/1e6)
redis.Close()
if err == nil && status == keywordOk.name {
if len(l.ch) > 0 {
<-l.ch
}
return &Lock{name: key}, nil
}
select {
case <-l.ch:
continue
case <-time.After(l.timeout):
return nil, ErrLockTimeOut
}
}
}
//UnLock when your business end,then release the locker
func (l *Locker) UnLock(lock *Lock) error {
redis, err := l.pool.GetResource()
if err != nil {
return err
}
defer redis.Close()
if len(l.ch) == 0 {
l.ch <- true
}
c, err := redis.Del(lock.name)
if err != nil {
return err
}
if c == 0 {
return nil
}
return nil
}
//ClusterLocker cluster lock client
type ClusterLocker struct {
timeout time.Duration
ch chan bool
redisCluster *RedisCluster
}
//NewClusterLocker create new cluster locker
func NewClusterLocker(option *ClusterOption, lockOption *LockOption) *ClusterLocker {
if lockOption == nil {
lockOption = &LockOption{}
}
if lockOption.Timeout.Nanoseconds() == 0 {
lockOption.Timeout = 5 * time.Second
}
return &ClusterLocker{
timeout: lockOption.Timeout,
ch: make(chan bool, 1),
redisCluster: NewRedisCluster(option),
}
}
//TryLock acquire a lock,when it returns a non nil locker,get lock success,
// otherwise, it returns an error,get lock failed
func (l *ClusterLocker) TryLock(key string) (*Lock, error) {
deadline := time.Now().Add(l.timeout)
value := strconv.FormatInt(deadline.UnixNano(), 10)
for {
if time.Now().After(deadline) {
return nil, ErrLockTimeOut
}
if len(l.ch) == 0 {
status, err := l.redisCluster.SetWithParamsAndTime(key, value, "nx", "px", l.timeout.Nanoseconds()/1e6)
//get lock success
if err == nil && status == keywordOk.name {
if len(l.ch) > 0 {
<-l.ch
}
return &Lock{name: key}, nil
}
}
select {
case <-l.ch:
continue
case <-time.After(l.timeout):
return nil, ErrLockTimeOut
}
}
}
//UnLock when your business end,then release the locker
func (l *ClusterLocker) UnLock(lock *Lock) error {
if len(l.ch) == 0 {
l.ch <- true
}
c, err := l.redisCluster.Del(lock.name)
if c == 0 {
return nil
}
return err
}