forked from TritonHo/optimistic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimistic.go
107 lines (86 loc) · 2.09 KB
/
optimistic.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
package optimisticLock
import (
"errors"
"time"
"github.com/go-redis/redis"
)
func New(r *redis.Client) *LockFactory {
return &LockFactory{RedisClient: r}
}
type LockFactory struct {
RedisClient *redis.Client `inject:""`
}
func (f *LockFactory) CreateDummyLock(key string) *Lock {
return &Lock{
key,
``,
time.Now(),
f.RedisClient,
}
}
func (f *LockFactory) Get(key string) *Lock {
result, err := f.RedisClient.HGetAll(key).Result()
if err == redis.Nil || len(result) == 0 {
return nil
}
if err != nil {
time.Sleep(time.Second * 2)
println(`optimisticLock::Get failed: ` + err.Error(), `retrying after 2 seconds`)
return f.Get(key)
}
contentStr, ok1 := result[`content`]
updatedAt, ok2 := result[`updated_at`]
if ok1 == false || ok2 == false {
panic(errors.New(`Invalid value stored in Redis. "content": ` + contentStr + `, "updatedAt": ` + updatedAt))
}
lastUpdatedAt, err := time.Parse(time.RFC3339Nano, updatedAt)
if err != nil {
panic(errors.New(`OptimisticLock - Internal error: updated_at is invalid time, value: ` + updatedAt))
}
return &Lock{
key,
contentStr,
lastUpdatedAt,
f.RedisClient,
}
}
type Lock struct {
Key string
Content string
lastUpdatedAt time.Time
redisClient *redis.Client
}
func (l *Lock) Update() bool {
// FIXME: use evalsha instead of direct scripting
result, err := l.redisClient.Eval(`
if redis.call('EXISTS', KEYS[1]) == 1 then
if redis.call('HGET', KEYS[1], 'updated_at') ~= KEYS[3] then
return 0
end
end
redis.call('HMSET', KEYS[1], 'content', KEYS[2], 'updated_at', KEYS[4])
return 1
`,
[]string{
l.Key,
l.Content,
l.lastUpdatedAt.Format(time.RFC3339Nano),
time.Now().UTC().Format(time.RFC3339Nano),
},
).Result()
if err != nil {
time.Sleep(time.Second * 2)
println(`optimisticLock::Update failed: ` + err.Error(), `retrying after 2 seconds`)
return l.Update()
}
switch t := result.(type) {
case string:
return t == `1`
case int64:
return t == 1
default:
// FIXME: should not reach this line
panic(errors.New(`Unknown return from redis eval. `))
}
return false
}