-
Notifications
You must be signed in to change notification settings - Fork 25
/
redis.go
152 lines (127 loc) · 3.97 KB
/
redis.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
140
141
142
143
144
145
146
147
148
149
150
151
152
package lock
import (
"sync"
"time"
"github.com/aldor007/mort/pkg/response"
"github.com/bsm/redislock"
goRedis "github.com/go-redis/redis/v8"
"context"
"strings"
)
func parseAddress(addrs []string) map[string]string {
mp := make(map[string]string, len(addrs))
for _, addr := range addrs {
parts := strings.Split(addr, ":")
mp[parts[0]] = parts[0] + ":" + parts[1]
}
return mp
}
type rediser interface {
SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *goRedis.BoolCmd
Eval(ctx context.Context, script string, keys []string, args ...interface{}) *goRedis.Cmd
EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *goRedis.Cmd
ScriptExists(ctx context.Context, scripts ...string) *goRedis.BoolSliceCmd
ScriptLoad(ctx context.Context, script string) *goRedis.StringCmd
Subscribe(ctx context.Context, channels ...string) *goRedis.PubSub
Publish(ctx context.Context, channel string, message interface{}) *goRedis.IntCmd
}
type internalLockRedis struct {
lock *redislock.Lock
pubsub *goRedis.PubSub
}
// RedisLock is in Redis lock for single mort instance
type RedisLock struct {
client *redislock.Client
memoryLock *MemoryLock
locks map[string]internalLockRedis
lock sync.RWMutex
LockTimeout int
redisClient rediser
}
// NewRedis create connection to redis and update it config from clientConfig map
func NewRedisLock(redisAddress []string, clientConfig map[string]string) *RedisLock {
ring := goRedis.NewRing(&goRedis.RingOptions{
Addrs: parseAddress(redisAddress),
})
if clientConfig != nil {
for key, value := range clientConfig {
ring.ConfigSet(context.Background(), key, value)
}
}
locker := redislock.New(ring)
return &RedisLock{client: locker, memoryLock: NewMemoryLock(), locks: make(map[string]internalLockRedis), LockTimeout: 60, redisClient: ring}
}
func NewRedisCluster(redisAddress []string, clientConfig map[string]string) *RedisLock {
ring := goRedis.NewClusterClient(&goRedis.ClusterOptions{
Addrs: redisAddress,
})
if clientConfig != nil {
for key, value := range clientConfig {
ring.ConfigSet(context.Background(), key, value)
}
}
locker := redislock.New(ring)
return &RedisLock{client: locker, memoryLock: NewMemoryLock(), locks: make(map[string]internalLockRedis), LockTimeout: 60, redisClient: ring}
}
// NotifyAndRelease tries notify all waiting goroutines about response
func (m *RedisLock) NotifyAndRelease(ctx context.Context, key string, originalResponse *response.Response) {
m.lock.Lock()
defer m.lock.Unlock()
lock, ok := m.locks[key]
if ok {
lock.lock.Release(ctx)
delete(m.locks, key)
if lock.pubsub != nil {
m.redisClient.Publish(ctx, key, 1)
lock.pubsub.Close()
}
}
m.memoryLock.NotifyAndRelease(ctx, key, originalResponse)
}
// Lock create unique entry in Redis map
func (m *RedisLock) Lock(ctx context.Context, key string) (result LockResult, ok bool) {
m.lock.Lock()
defer m.lock.Unlock()
lock, ok := m.locks[key]
if ok {
lock.lock.Refresh(ctx, time.Second*time.Duration(m.LockTimeout/2), nil)
} else {
lock, err := m.client.Obtain(ctx, key, time.Duration(m.LockTimeout)*time.Second, nil)
pubsub := m.redisClient.Subscribe(ctx, key)
lockData := internalLockRedis{lock: lock, pubsub: pubsub}
if err == redislock.ErrNotObtained {
result, ok = m.memoryLock.forceLockAndAddWatch(ctx, key)
ok = false
// Go channel which receives messages.
ch := pubsub.Channel()
go func() {
for {
select {
case <-ch:
m.NotifyAndRelease(ctx, key, nil)
}
}
}()
return
} else if err != nil {
result.Error = err
return
}
m.locks[key] = lockData
}
return m.memoryLock.Lock(ctx, key)
}
// Release remove entry from Redis map
func (m *RedisLock) Release(ctx context.Context, key string) {
m.lock.Lock()
defer m.lock.Unlock()
lock, ok := m.locks[key]
if ok {
lock.lock.Release(ctx)
if lock.pubsub != nil {
lock.pubsub.Close()
}
delete(m.locks, key)
m.memoryLock.Release(ctx, key)
}
}