-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
74 lines (61 loc) · 1.78 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
package heximage
import (
"net/url"
"time"
"github.com/garyburd/redigo/redis"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
// ConnectRedis connects to the redis instance, pings it, and returns the pool
// object on sucesfull connect.
func ConnectRedis(dsn string, maxActive int) (*redis.Pool, error) {
// Parse the dsn down to a URL.
addr, err := url.Parse(dsn)
if err != nil {
return nil, errors.Wrap(err, "can't parse the redis server dsn")
}
// Create the pool.
pool := redis.Pool{
MaxActive: maxActive,
MaxIdle: 3,
IdleTimeout: 30 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", addr.Host)
if err != nil {
return nil, errors.Wrap(err, "can't dial the redis server")
}
if addr.User != nil {
password, _ := addr.User.Password()
if password != "" {
if _, err = c.Do("AUTH", password); err != nil {
c.Close()
return nil, errors.Wrap(err, "can't auth to the redis server")
}
log.WithField("query", "redis").Debugf("AUTH <redacted>")
}
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if time.Since(t) < 1*time.Minute {
return nil
}
// Test that we can ping if the last time we tested this connection was
// more than 1 minute ago.
if _, err := c.Do("PING"); err != nil {
return errors.Wrap(err, "can't ping redis server after timeout exhausted")
}
log.WithField("query", "redis").Debugf("PING")
return nil
},
}
// Try and get the pool connection.
con := pool.Get()
defer con.Close()
// Ensure we can ping the server.
if _, err := con.Do("PING"); err != nil {
return nil, errors.Wrap(err, "can't ping the redis server for the first time")
}
log.WithField("query", "redis").Debugf("PING")
return &pool, nil
}