-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocker.go
139 lines (125 loc) · 3.52 KB
/
locker.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
138
139
package orm
import (
"context"
"fmt"
"time"
"github.com/bsm/redislock"
"github.com/pkg/errors"
)
type lockerClient interface {
Obtain(context context.Context, key string, ttl time.Duration, opt *redislock.Options) (*redislock.Lock, error)
}
type standardLockerClient struct {
client *redislock.Client
}
func (l *standardLockerClient) Obtain(context context.Context, key string, ttl time.Duration, opt *redislock.Options) (*redislock.Lock, error) {
return l.client.Obtain(context, key, ttl, opt)
}
type Locker struct {
locker lockerClient
r *redisCache
}
func (r *redisCache) GetLocker() *Locker {
if r.locker == nil {
r.locker = &Locker{locker: &standardLockerClient{client: redislock.New(r.client)}, r: r}
}
return r.locker
}
func (l *Locker) Obtain(ctx Context, key string, ttl time.Duration, waitTimeout time.Duration) (lock *Lock, obtained bool) {
if ttl == 0 {
panic(errors.New("ttl must be higher than zero"))
}
if waitTimeout > ttl {
panic(errors.New("waitTimeout can't be higher than ttl"))
}
hasLogger, _ := ctx.getRedisLoggers()
start := getNow(hasLogger)
var options *redislock.Options
if waitTimeout > 0 {
options = &redislock.Options{}
interval := time.Second
limit := 1
if waitTimeout < interval {
interval = waitTimeout
} else {
limit = int(waitTimeout / time.Second)
}
options.RetryStrategy = redislock.LimitRetry(redislock.LinearBackoff(interval), limit)
}
redisLock, err := l.locker.Obtain(ctx.Context(), key, ttl, options)
if err != nil {
if err == redislock.ErrNotObtained {
if hasLogger {
message := fmt.Sprintf("LOCK OBTAIN %s TTL %s WAIT %s", key, ttl.String(), waitTimeout.String())
l.fillLogFields(ctx, "LOCK OBTAIN", message, start, true, nil)
}
return nil, false
}
}
if hasLogger {
message := fmt.Sprintf("LOCK OBTAIN %s TTL %s WAIT %s", key, ttl.String(), waitTimeout.String())
l.fillLogFields(ctx, "LOCK OBTAIN", message, start, false, nil)
}
checkError(err)
lock = &Lock{lock: redisLock, locker: l, ttl: ttl, key: key, has: true}
return lock, true
}
type Lock struct {
lock *redislock.Lock
key string
ttl time.Duration
locker *Locker
has bool
}
func (l *Lock) Release(ctx Context) {
if !l.has {
return
}
l.has = false
hasLogger, _ := ctx.getRedisLoggers()
start := getNow(hasLogger)
err := l.lock.Release(context.Background())
ok := true
if err == redislock.ErrLockNotHeld {
err = nil
ok = false
}
if hasLogger {
l.locker.fillLogFields(ctx, "LOCK RELEASE", "LOCK RELEASE "+l.key, start, !ok, err)
}
checkError(err)
}
func (l *Lock) TTL(ctx Context) time.Duration {
hasLogger, _ := ctx.getRedisLoggers()
start := getNow(hasLogger)
t, err := l.lock.TTL(ctx.Context())
if hasLogger {
l.locker.fillLogFields(ctx, "LOCK TTL", "LOCK TTL "+l.key, start, false, err)
}
checkError(err)
return t
}
func (l *Lock) Refresh(ctx Context, ttl time.Duration) bool {
if !l.has {
return false
}
hasLogger, _ := ctx.getRedisLoggers()
start := getNow(hasLogger)
err := l.lock.Refresh(ctx.Context(), ttl, nil)
ok := true
if err == redislock.ErrNotObtained {
ok = false
err = nil
l.has = false
}
if hasLogger {
message := fmt.Sprintf("LOCK REFRESH %s %s", l.key, l.ttl)
l.locker.fillLogFields(ctx, "LOCK REFRESH", message, start, !ok, err)
}
checkError(err)
return ok
}
func (l *Locker) fillLogFields(ctx Context, operation, query string, start *time.Time, cacheMiss bool, err error) {
_, loggers := ctx.getRedisLoggers()
fillLogFields(ctx, loggers, l.r.config.GetCode(), sourceRedis, operation, query, start, cacheMiss, err)
}