-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis_mutex.go
105 lines (95 loc) · 2.07 KB
/
redis_mutex.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
package sync
import (
"context"
"time"
"github.com/go-redis/redis/v8"
)
// RedisSync ...
type RedisSync struct {
Opts *Options
Redis *redis.Client
}
// NewRedisSync ...
func NewRedisSync(redis *redis.Client, opts ...Option) *RedisSync {
return &RedisSync{
Redis: redis,
Opts: newOptions(opts...),
}
}
// NewMutex 创建互斥锁
func (r *RedisSync) NewMutex(key string, opts ...Option) Mutexer {
optx := *r.Opts
for _, o := range opts {
o(&optx)
}
rm := &redisMutex{
sessionID: sessionID(),
opts: &optx,
redis: r.Redis,
key: key,
}
return rm
}
// redisMutex redis互斥锁
type redisMutex struct {
sessionID string
opts *Options
redis *redis.Client
key string
lockCtx context.Context
lockCancel context.CancelFunc
}
// Lock 加锁
func (rm *redisMutex) Lock() (err error) {
var flag bool
lockName := rm.lockName()
flag, err = rm.redis.SetNX(context.Background(), lockName, rm.sessionID, rm.opts.LockTimeout).Result()
if err != nil {
return
}
if !flag {
err = ErrLockFailed
return
}
rm.lockCtx, rm.lockCancel = context.WithCancel(context.Background())
go func() {
t := time.NewTimer(rm.opts.WaitRetry)
defer t.Stop()
for {
select {
case <-t.C:
_ = rm.redis.Expire(context.Background(), lockName, rm.opts.WaitRetry)
t.Reset(rm.opts.WaitRetry)
// TODO: 如果延迟失败
// fmt.Println("触发延迟key条件")
case <-rm.lockCtx.Done():
// fmt.Println("解锁触发上下文结束事件,结束整个for循环")
return
}
}
}()
return
}
const (
luaRelease = `if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end`
)
// Unlock 解锁
func (rm *redisMutex) Unlock() (err error) {
var flag bool
lockName := rm.lockName()
flag, err = rm.redis.Eval(context.Background(), luaRelease, []string{lockName}, rm.sessionID).Bool()
if err != nil {
return
}
if !flag {
err = ErrUnlockFailed
return
}
if rm.lockCancel != nil {
rm.lockCancel()
}
return
}
func (rm *redisMutex) lockName() string {
return rm.opts.KeyPrefix + rm.key
}