/
redis.go
273 lines (207 loc) · 5.74 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package redis
import (
"encoding/json"
"fmt"
r "github.com/garyburd/redigo/redis"
ld "github.com/launchdarkly/go-client"
"github.com/patrickmn/go-cache"
"time"
)
// A Redis-backed feature store.
type RedisFeatureStore struct {
prefix string
pool *r.Pool
cache *cache.Cache
timeout time.Duration
}
const initKey = "$initialized$"
var pool *r.Pool
func newPool(url string) *r.Pool {
pool = &r.Pool{
MaxIdle: 20,
MaxActive: 16,
Wait: true,
IdleTimeout: 300 * time.Second,
Dial: func() (c r.Conn, err error) {
c, err = r.DialURL(url)
return
},
TestOnBorrow: func(c r.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
return pool
}
func (store *RedisFeatureStore) getConn() r.Conn {
return store.pool.Get()
}
// Constructs a new Redis-backed feature store connecting to the specified URL with a default
// connection pool configuration (16 concurrent connections, connection requests block).
// Attaches a prefix string to all keys to namespace LaunchDarkly-specific keys. If the
// specified prefix is the empty string, it defaults to "launchdarkly".
func NewRedisFeatureStoreFromUrl(url, prefix string, timeout time.Duration) *RedisFeatureStore {
return NewRedisFeatureStoreWithPool(newPool(url), prefix, timeout)
}
// Constructs a new Redis-backed feature store with the specified redigo pool configuration.
// Attaches a prefix string to all keys to namespace LaunchDarkly-specific keys. If the
// specified prefix is the empty string, it defaults to "launchdarkly".
func NewRedisFeatureStoreWithPool(pool *r.Pool, prefix string, timeout time.Duration) *RedisFeatureStore {
var c *cache.Cache
if prefix == "" {
prefix = "launchdarkly"
}
if timeout > 0 {
c = cache.New(timeout, 5*time.Minute)
}
store := RedisFeatureStore{
prefix: prefix,
pool: pool,
cache: c,
timeout: timeout,
}
return &store
}
// Constructs a new Redis-backed feature store connecting to the specified host and port with a default
// connection pool configuration (16 concurrent connections, connection requests block).
// Attaches a prefix string to all keys to namespace LaunchDarkly-specific keys. If the
// specified prefix is the empty string, it defaults to "launchdarkly"
func NewRedisFeatureStore(host string, port int, prefix string, timeout time.Duration) *RedisFeatureStore {
return NewRedisFeatureStoreFromUrl(fmt.Sprintf("redis://%s:%d", host, port), prefix, timeout)
}
func (store *RedisFeatureStore) featuresKey() string {
return store.prefix + ":features"
}
func (store *RedisFeatureStore) Get(key string) (*ld.Feature, error) {
var feature ld.Feature
if store.cache != nil {
if data, present := store.cache.Get(key); present {
if feature, ok := data.(ld.Feature); ok {
if feature.Deleted {
return nil, nil
}
return &feature, nil
}
}
}
c := store.getConn()
defer c.Close()
jsonStr, err := r.String(c.Do("HGET", store.featuresKey(), key))
if err != nil {
if err == r.ErrNil {
return nil, nil
}
return nil, err
}
if jsonErr := json.Unmarshal([]byte(jsonStr), &feature); jsonErr != nil {
return nil, jsonErr
}
if feature.Deleted {
return nil, nil
}
if store.cache != nil {
store.cache.Set(key, feature, store.timeout)
}
return &feature, nil
}
func (store *RedisFeatureStore) All() (map[string]*ld.Feature, error) {
var feature ld.Feature
results := make(map[string]*ld.Feature)
c := store.getConn()
defer c.Close()
values, err := r.StringMap(c.Do("HGETALL", store.featuresKey()))
if err != nil && err != r.ErrNil {
return nil, err
}
for k, v := range values {
jsonErr := json.Unmarshal([]byte(v), &feature)
if jsonErr != nil {
return nil, err
}
if !feature.Deleted {
results[k] = &feature
}
}
return results, nil
}
func (store *RedisFeatureStore) Init(features map[string]*ld.Feature) error {
c := store.getConn()
defer c.Close()
c.Send("MULTI")
c.Send("DEL", store.featuresKey())
if store.cache != nil {
store.cache.Flush()
}
for k, v := range features {
data, jsonErr := json.Marshal(v)
if jsonErr != nil {
return jsonErr
}
c.Send("HSET", store.featuresKey(), k, data)
if store.cache != nil {
store.cache.Set(k, v, store.timeout)
}
}
_, err := c.Do("EXEC")
return err
}
func (store *RedisFeatureStore) Delete(key string, version int) error {
c := store.getConn()
defer c.Close()
c.Send("WATCH", store.featuresKey())
defer c.Send("UNWATCH")
feature, featureErr := store.Get(key)
if featureErr != nil {
return featureErr
}
if feature != nil && feature.Version >= version {
return nil
}
feature.Deleted = true
feature.Version = version
data, jsonErr := json.Marshal(feature)
if jsonErr != nil {
return jsonErr
}
_, err := c.Do("HSET", store.featuresKey(), key, data)
if err == nil && store.cache != nil {
store.cache.Set(key, feature, store.timeout)
}
return err
}
func (store *RedisFeatureStore) Upsert(key string, f ld.Feature) error {
c := store.getConn()
defer c.Close()
c.Send("WATCH", store.featuresKey())
defer c.Send("UNWATCH")
o, featureErr := store.Get(key)
if featureErr != nil {
return featureErr
}
if o != nil && o.Version >= f.Version {
return nil
}
data, jsonErr := json.Marshal(f)
if jsonErr != nil {
return jsonErr
}
_, err := c.Do("HSET", store.featuresKey(), key, data)
if err == nil && store.cache != nil {
store.cache.Set(key, f, store.timeout)
}
return err
}
func (store *RedisFeatureStore) Initialized() bool {
if store.cache != nil {
if _, present := store.cache.Get(initKey); present {
return true
}
}
c := store.getConn()
defer c.Close()
init, err := r.Bool(c.Do("EXISTS", store.featuresKey()))
if store.cache != nil && err == nil && init {
store.cache.Set(initKey, true, store.timeout)
}
return err == nil && init
}