/
lock.go
85 lines (71 loc) · 1.3 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
package rediskit
import (
"crypto/rand"
"errors"
"fmt"
"io"
"time"
"github.com/go-redis/redis"
)
// RedisLock Redis distributed lock
type RedisLock struct {
c *redis.Client
key string
value string
ch chan struct{}
}
// NewRedisLock new a redis lock
// key - lock name
func NewRedisLock(c *redis.Client, key string) *RedisLock {
return &RedisLock{
c: c,
key: key,
ch: make(chan struct{}),
}
}
// Lock try lock
// notice: renew every 15 sec
func (l *RedisLock) Lock() error {
var guid [16]byte
_, err := io.ReadFull(rand.Reader, guid[:16])
if err != nil {
return err
}
guidStr := fmt.Sprintf("%x", guid)
l.value = guidStr
ok, err := l.c.SetNX(l.key, l.value, 30*time.Second).Result()
if err != nil {
return err
}
if !ok {
return errors.New("can not lock")
}
go func() {
ticker := time.NewTicker(15 * time.Second)
for {
select {
case <-ticker.C:
_ = l.c.Expire(l.key, 30*time.Second).Err()
case <-l.ch:
return
}
}
}()
return nil
}
// Unlock unlock check value
func (l *RedisLock) Unlock() error {
value, err := l.c.Get(l.key).Result()
if err != nil {
return err
}
if value != l.value {
return errors.New("can not match lock value")
}
_, err = l.c.Del(l.key).Result()
if err != nil {
return err
}
l.ch <- struct{}{}
return nil
}