/
redis.go
357 lines (292 loc) · 10 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
Package redis provides a Hord database driver for Redis.
Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. To use this driver, import it as follows:
import (
"github.com/madflojo/hord"
"github.com/madflojo/hord/redis"
)
# Connecting to the Database
Use the Dial() function to create a new client for interacting with Redis.
var db hord.Database
db, err := redis.Dial(redis.Config{})
if err != nil {
// Handle connection error
}
# Initialize database
Hord provides a Setup() function for preparing a database. This function is safe to execute after every Dial().
err := db.Setup()
if err != nil {
// Handle setup error
}
# Database Operations
Hord provides a simple abstraction for working with Redis, with easy-to-use methods such as Get() and Set() to read and write values.
// Connect to the Redis database
db, err := redis.Dial(redis.Config{})
if err != nil {
// Handle connection error
}
err := db.Setup()
if err != nil {
// Handle setup error
}
// Set a value
err = db.Set("key", []byte("value"))
if err != nil {
// Handle error
}
// Retrieve a value
value, err := db.Get("key")
if err != nil {
// Handle error
}
*/
package redis
import (
"crypto/tls"
"fmt"
"github.com/FZambia/sentinel"
"github.com/gomodule/redigo/redis"
"github.com/madflojo/hord"
"time"
)
// Config provides configuration options for connecting to and controlling the behavior of Redis.
type Config struct {
// ConnectTimeout is used to specify a global connection timeout value.
ConnectTimeout time.Duration
// Database specifies the Redis database to connect to and use. If not set, default is 0.
Database int
// IdleTimeout will close idle connections that have remained idle beyond the specified time duration.
IdleTimeout time.Duration
// KeepAlive defines the TCP Keep-Alive interval for Redis connections. By default this set to 5 minutes, this
// setting is useful for detecting TCP sessions that are stale.
KeepAlive time.Duration
// MaxActive is the maximum number of connections that can be allocated and used for the Redis connection pool.
MaxActive int
// MaxConnLifetime will set a maximum lifespan for connections. This setting will close connections in the pool
// after the time duration is exceeded, regardless of whether the connection is active or not.
MaxConnLifetime time.Duration
// MaxIdle sets the maximum number of idle connections the Redis connection pool will allow.
MaxIdle int
// Password specifies the AUTH token to be used for Redis Authentication.
Password string
// ReadTimeout is used to specify a global read timeout for each Redis command.
ReadTimeout time.Duration
// SentinelConfig is used to configure Sentinel connection details. If not using Redis Sentinel or Discovery
// Service, leave this blank.
SentinelConfig SentinelConfig
// Server specifies the Redis Server to connect to. If using Redis Sentinel or Discovery Service leave this
// blank.
Server string
// SkipTLSVerify will disable the TLS hostname checking. Warning, using this setting opens the risk of
// man-in-the-middle attacks.
SkipTLSVerify bool
// TLSConfig allows users to specify TLS settings for connecting to Redis. This is a standard TLS configuration
// and can be used to configure 2-way TLS for Redis and Redis Sentinel.
TLSConfig *tls.Config
// WriteTimeout is used to specify a global write timeout for each Redis command.
WriteTimeout time.Duration
}
// SentinelConfig can be used to configure the Redis client to connect using Redis Sentinel or Enterprise Redis
// Discovery Service.
type SentinelConfig struct {
// Servers is a list of Sentinel servers to connect to and use for master discovery.
Servers []string
// Master is the name of the Redis master that the Sentinel Servers monitor.
Master string
}
// Database is used to interface with Redis. It also satisfies the Hord Database interface.
type Database struct {
// config holds the initial Redis configuration used to create this Database instance.
config Config
// pool is the Redis connection pool
pool *redis.Pool
// sentinel holds the Redis sentinel connections
sentinel *sentinel.Sentinel
}
// Dial will establish a Redis connection pool using the configuration provided. It provides back an interface that
// satisfies the hord.Database interface.
func Dial(conf Config) (*Database, error) {
db := &Database{
config: conf,
}
// Verify that Either Server or Sentinel Servers is set
if db.config.Server == "" && len(db.config.SentinelConfig.Servers) == 0 {
return db, fmt.Errorf("must specify either a Redis Server or Sentinel Pool")
}
// Setup Redis DailOptions
opts := []redis.DialOption{}
opts = append(opts, redis.DialConnectTimeout(db.config.ConnectTimeout))
opts = append(opts, redis.DialDatabase(db.config.Database))
opts = append(opts, redis.DialKeepAlive(db.config.KeepAlive))
opts = append(opts, redis.DialPassword(db.config.Password))
opts = append(opts, redis.DialReadTimeout(db.config.ReadTimeout))
opts = append(opts, redis.DialWriteTimeout(db.config.WriteTimeout))
if db.config.TLSConfig != nil {
opts = append(opts, redis.DialUseTLS(true), redis.DialTLSConfig(db.config.TLSConfig))
opts = append(opts, redis.DialTLSSkipVerify(db.config.SkipTLSVerify))
}
// If Sentinel is set, let's connect
if len(db.config.SentinelConfig.Servers) > 0 {
if db.config.SentinelConfig.Master == "" {
return nil, fmt.Errorf("if using Sentinel the Redis Master must be defined")
}
db.sentinel = &sentinel.Sentinel{
Addrs: db.config.SentinelConfig.Servers,
MasterName: db.config.SentinelConfig.Master,
Dial: func(addr string) (redis.Conn, error) {
c, err := redis.Dial("tcp", addr, opts...)
if err != nil {
return nil, err
}
return c, nil
},
}
}
// Create a Redis Connection Pool
db.pool = &redis.Pool{
IdleTimeout: db.config.IdleTimeout,
MaxActive: db.config.MaxActive,
MaxConnLifetime: db.config.MaxConnLifetime,
MaxIdle: db.config.MaxIdle,
Wait: true,
// Used to create new connections for the pool
Dial: func() (redis.Conn, error) {
var err error
server := db.config.Server
if db.sentinel != nil {
server, err = db.sentinel.MasterAddr()
if err != nil {
return nil, err
}
}
c, err := redis.Dial("tcp", server, opts...)
if err != nil {
return nil, err
}
return c, nil
},
// Used to Test the provided connection
TestOnBorrow: func(c redis.Conn, _ time.Time) error {
if !sentinel.TestRole(c, "master") {
return fmt.Errorf("server is not a master")
}
_, err := c.Do("PING")
if err != nil {
return fmt.Errorf("connection is unhealthy, failed ping %s", err)
}
return nil
},
}
// Execute HealthCheck to verify connectivity
err := db.HealthCheck()
if err != nil {
return db, fmt.Errorf("connection is unhealthy, failed ping %s", err)
}
return db, nil
}
// Setup does nothing with Redis, this is only here to meet interface requirements.
func (db *Database) Setup() error {
// Execute HealthCheck to verify connectivity
err := db.HealthCheck()
if err != nil {
return fmt.Errorf("connection is unhealthy, failed ping %s", err)
}
return nil
}
// Get is called to retrieve data from the database. This function will take in a key and return
// the data or any errors received from querying the database.
func (db *Database) Get(key string) ([]byte, error) {
if err := hord.ValidKey(key); err != nil {
return nil, err
}
if db == nil || db.pool == nil {
return nil, hord.ErrNoDial
}
c := db.pool.Get()
defer c.Close()
d, err := redis.Bytes(c.Do("GET", key))
if err != nil && err != redis.ErrNil {
return nil, fmt.Errorf("unable to fetch data from Redis - %s", err)
}
if err == redis.ErrNil {
return []byte(""), hord.ErrNil
}
return d, nil
}
// Set is called when data within the database needs to be updated or inserted. This function will
// take the data provided and create an entry within the database using the key as a lookup value.
func (db *Database) Set(key string, data []byte) error {
if err := hord.ValidKey(key); err != nil {
return err
}
if err := hord.ValidData(data); err != nil {
return err
}
if db == nil || db.pool == nil {
return hord.ErrNoDial
}
c := db.pool.Get()
defer c.Close()
_, err := c.Do("SET", key, data)
if err != nil {
return fmt.Errorf("unable to write data to Redis - %s", err)
}
return nil
}
// Delete is called when data within the database needs to be deleted. This function will delete
// the data stored within the database for the specified key.
func (db *Database) Delete(key string) error {
if err := hord.ValidKey(key); err != nil {
return err
}
if db == nil || db.pool == nil {
return hord.ErrNoDial
}
c := db.pool.Get()
defer c.Close()
_, err := c.Do("DEL", key)
if err != nil {
return fmt.Errorf("unable to remove key from Redis - %s", err)
}
return nil
}
// Keys is called to retrieve a list of keys stored within the database. This function will query
// the database returning all keys used within the hord database.
func (db *Database) Keys() ([]string, error) {
if db == nil || db.pool == nil {
return []string{}, hord.ErrNoDial
}
c := db.pool.Get()
defer c.Close()
keys, err := redis.Strings(c.Do("KEYS", "*"))
if err != nil {
return keys, fmt.Errorf("unable to fetch keys from Redis - %s", err)
}
return keys, nil
}
// HealthCheck is used to verify connectivity and health of the database. This function
// simply runs a generic ping against the database. If the ping errors in any fashion this
// function will return an error.
func (db *Database) HealthCheck() error {
// Return error if pool is not created
if db == nil || db.pool == nil {
return hord.ErrNoDial
}
c := db.pool.Get()
defer c.Close()
_, err := c.Do("PING")
if err != nil {
return fmt.Errorf("unable to ping Redis - %s", err)
}
return nil
}
// Close will close all connections to Redis and clean up the pool.
func (db *Database) Close() {
if db == nil || db.pool == nil {
return
}
defer db.pool.Close()
if db.sentinel != nil {
defer db.sentinel.Close()
}
}