-
Notifications
You must be signed in to change notification settings - Fork 49
/
lock.go
68 lines (59 loc) · 1.36 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
package global
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/jassue/jassue-gin/utils"
"time"
)
type Interface interface {
Get() bool
Block(seconds int64) bool
Release() bool
ForceRelease()
}
type lock struct {
context context.Context
name string
owner string
seconds int64
}
const releaseLockLuaScript = `
if redis.call("get",KEYS[1]) == ARGV[1] then
return redis.call("del",KEYS[1])
else
return 0
end
`
func Lock(name string, seconds int64) Interface {
return &lock{
context.Background(),
name,
utils.RandString(16),
seconds,
}
}
func (l *lock) Get() bool {
return App.Redis.SetNX(l.context, l.name, l.owner, time.Duration(l.seconds)*time.Second).Val()
}
func (l *lock) Block(seconds int64) bool {
timer := time.After(time.Duration(seconds) * time.Second)
for {
select {
case <-timer:
return false
default:
if l.Get() {
return true
}
time.Sleep(time.Duration(1) * time.Second)
}
}
}
func (l *lock) Release() bool {
luaScript := redis.NewScript(releaseLockLuaScript)
result := luaScript.Run(l.context, App.Redis, []string{l.name}, l.owner).Val().(int64)
return result != 0
}
func (l *lock) ForceRelease() {
App.Redis.Del(l.context, l.name).Val()
}