-
Notifications
You must be signed in to change notification settings - Fork 1
/
redislock.go
188 lines (145 loc) · 4.44 KB
/
redislock.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package dlock
import (
"context"
"time"
"github.com/go-redsync/redsync"
"github.com/gomodule/redigo/redis"
)
type RedisPoolOption interface {
Apply(*rpool)
}
type withPassword string
func (w withPassword) Apply(o *rpool) { o.passwd = string(w) }
// WithPassword provides an option to set a password to a Redis pool.
func WithPassword(v string) RedisPoolOption { return withPassword(v) }
type withTimeout time.Duration
func (w withTimeout) Apply(o *rpool) { o.timeout = time.Duration(w) }
// WithTimeout provides an option to set a timeout to a Redis pool.
func WithTimeout(v time.Duration) RedisPoolOption { return withTimeout(v) }
// RedisPool returns a connection pool for Redis.
func NewRedisPool(host string, opts ...RedisPoolOption) *redis.Pool {
pool := &rpool{
host: host,
timeout: 5,
maxIdle: 3,
maxActive: 4,
wait: true,
idleTm: time.Second * 240,
}
// Apply provided options, if any.
for _, opt := range opts {
opt.Apply(pool)
}
var dialOpts []redis.DialOption
if pool.passwd != "" {
dialOpts = append(dialOpts, redis.DialPassword(pool.passwd))
}
dialOpts = append(dialOpts, redis.DialConnectTimeout(time.Second*pool.timeout))
return &redis.Pool{
MaxIdle: pool.maxIdle,
MaxActive: pool.maxActive,
Wait: pool.wait,
IdleTimeout: pool.idleTm,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", pool.host, dialOpts...)
},
}
}
type rpool struct {
host string
passwd string
timeout time.Duration
maxIdle int
maxActive int
wait bool
idleTm time.Duration
}
type RedisLockOption interface {
Apply(*rlock)
}
type withHost string
func (w withHost) Apply(o *rlock) { o.hosts = append(o.hosts, string(w)) }
// WithHost provides an option to set a single Redis host for the lock.
func WithHost(v string) RedisLockOption { return withHost(v) }
type withHosts struct{ hosts []string }
func (w withHosts) Apply(o *rlock) { o.hosts = append(o.hosts, w.hosts...) }
// WithHosts provides an option to set a list of Redis hosts for the lock.
func WithHosts(v []string) RedisLockOption { return withHosts{v} }
type withPools struct{ pools []*redis.Pool }
func (w withPools) Apply(o *rlock) {
for _, v := range w.pools {
o.pools = append(o.pools, v)
}
}
// WithPools provides an option to set a list of Redis pools for the lock.
func WithPools(v []*redis.Pool) RedisLockOption { return withPools{v} }
type withExtendAfter time.Duration
func (w withExtendAfter) Apply(o *rlock) { o.extend = time.Duration(w) }
// WithExtendAfter provides an option to set the duration before extending the lock.
func WithExtendAfter(v time.Duration) RedisLockOption { return withExtendAfter(v) }
type withRedsyncOptions struct{ opts []redsync.Option }
func (w withRedsyncOptions) Apply(o *rlock) { o.rsopts = w.opts }
// WithRedsyncOptions provides an option to set additional options to the underlying redsync Mutex.
func WithRedsyncOptions(v []redsync.Option) RedisLockOption { return withRedsyncOptions{v} }
// NewRedisLock creates an object that can be used to acquire/release a lock using
// Redis. This is built on top of redsync with the addition of context and implementing
// the Locker interface.
func NewRedisLock(name string, opts ...RedisLockOption) *rlock {
lock := &rlock{
hosts: []string{},
pools: []redsync.Pool{},
rsopts: []redsync.Option{},
extend: time.Second * 5,
}
// Apply provided options, if any.
for _, opt := range opts {
opt.Apply(lock)
}
if len(lock.hosts) > 0 {
for _, h := range lock.hosts {
p := NewRedisPool(h)
lock.pools = append(lock.pools, p)
}
}
rs := redsync.New(lock.pools)
lock.m = rs.NewMutex(name, lock.rsopts...)
return lock
}
type rlock struct {
hosts []string
pools []redsync.Pool
rsopts []redsync.Option
m *redsync.Mutex
extend time.Duration
quit context.Context
cancel context.CancelFunc
}
// Lock attempts to grab the named lock. It will also attempt to extend it until
// Unlock is called or if ctx is cancelled or expired.
func (l *rlock) Lock(ctx context.Context) error {
if err := l.m.Lock(); err != nil {
return err
}
l.quit, l.cancel = context.WithCancel(ctx)
ticker := time.NewTicker(l.extend)
// Continue lock until unlocked.
go func() {
for {
select {
case <-ticker.C:
case <-l.quit.Done():
return
}
// TODO: Do something with errors.
// Propagate to Unlock?
l.m.Extend()
}
}()
return nil
}
func (l *rlock) Unlock() error {
if l.cancel != nil {
l.cancel()
}
return nil
}