-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
137 lines (109 loc) · 2.82 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
package redisutil
import (
"context"
"errors"
"time"
"github.com/go-redis/redis/v7"
"github.com/sirupsen/logrus"
)
const stepDuration = 100 * time.Millisecond
var (
TimeOutErr = errors.New("timeout on lock")
)
type operator interface {
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd
GetSet(ctx context.Context, key string, value interface{}) *redis.StringCmd
Get(ctx context.Context, key string) *redis.StringCmd
Del(ctx context.Context, keys ...string) *redis.IntCmd
}
type Client struct {
Redis operator
Timeout time.Duration
}
type Lock struct {
Key string
value int64
}
func (cli *Client) GetLock(ctx context.Context, key string, expiration time.Duration) (*Lock, error) {
if cli.Timeout == 0 {
val, ok, err := cli.lockup(ctx, key, expiration)
if err != nil {
return nil, err
}
if !ok {
return nil, TimeOutErr
}
return &Lock{
Key: key,
value: val,
}, nil
}
start := time.Now()
i := start
for ; i.Before(start.Add(cli.Timeout)); i = time.Now() {
logrus.Infof("For, exp: %v", expiration)
val, ok, err := cli.lockup(ctx, key, expiration)
if err != nil {
return nil, err
}
if ok {
return &Lock{
Key: key,
value: val,
}, nil
}
time.Sleep(stepDuration)
}
return nil, TimeOutErr
}
func (cli *Client) Free(ctx context.Context, l *Lock) error {
// only DEL it if it hasn't expired
if l.value > time.Now().UnixNano() {
return cli.Redis.Del(ctx, l.Key).Err()
}
return nil
}
// 1. SETNX key, expiration time
// 2. if true, success and return
// 3. if false, lock is being held
// 4. now check to see if it's expired
// 5. GET key and check expiration time
// 6. if it's expired, GETSET new value
// 7. check result for GET and GETSET
func (cli *Client) lockup(ctx context.Context, key string, expiration time.Duration) (int64, bool, error) {
exp := cli.generateExpiration(expiration)
ok, err := cli.Redis.SetNX(ctx, key, exp, 0).Result()
if err != nil {
return 0, false, err
}
if ok {
// success
return exp, true, nil
}
// lock is being held
val, err := cli.Redis.Get(ctx, key).Int64()
if err != nil {
logrus.Errorf("redis.get error: %v", err)
return 0, false, err
}
if val < time.Now().UnixNano() {
exp = cli.generateExpiration(expiration)
oldVal, err := cli.Redis.GetSet(ctx, key, exp).Int64()
if err != nil {
logrus.Errorf("redis.getset error: %v", err)
return 0, false, err
}
if oldVal < time.Now().UnixNano() {
// success
return exp, true, nil
}
}
return 0, false, nil
}
func (cli *Client) generateExpiration(expiration time.Duration) int64 {
if expiration < time.Millisecond {
return time.Now().Add(time.Second).UnixNano()
}
return time.Now().Add(expiration).UnixNano()
}