-
Notifications
You must be signed in to change notification settings - Fork 876
/
redis.go
119 lines (101 loc) · 2.76 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
package exporter
import (
"net/url"
"strings"
"time"
"github.com/gomodule/redigo/redis"
"github.com/mna/redisc"
log "github.com/sirupsen/logrus"
)
func (e *Exporter) configureOptions(uri string) ([]redis.DialOption, error) {
tlsConfig, err := e.CreateClientTLSConfig()
if err != nil {
return nil, err
}
options := []redis.DialOption{
redis.DialConnectTimeout(e.options.ConnectionTimeouts),
redis.DialReadTimeout(e.options.ConnectionTimeouts),
redis.DialWriteTimeout(e.options.ConnectionTimeouts),
redis.DialTLSConfig(tlsConfig),
}
if e.options.User != "" {
options = append(options, redis.DialUsername(e.options.User))
}
if e.options.Password != "" {
options = append(options, redis.DialPassword(e.options.Password))
}
if e.options.PasswordMap[uri] != "" {
options = append(options, redis.DialPassword(e.options.PasswordMap[uri]))
}
return options, nil
}
func (e *Exporter) connectToRedis() (redis.Conn, error) {
uri := e.redisAddr
if !strings.Contains(uri, "://") {
uri = "redis://" + uri
}
options, err := e.configureOptions(uri)
if err != nil {
return nil, err
}
log.Debugf("Trying DialURL(): %s", uri)
c, err := redis.DialURL(uri, options...)
if err != nil {
log.Debugf("DialURL() failed, err: %s", err)
if frags := strings.Split(e.redisAddr, "://"); len(frags) == 2 {
log.Debugf("Trying: Dial(): %s %s", frags[0], frags[1])
c, err = redis.Dial(frags[0], frags[1], options...)
} else {
log.Debugf("Trying: Dial(): tcp %s", e.redisAddr)
c, err = redis.Dial("tcp", e.redisAddr, options...)
}
}
return c, err
}
func (e *Exporter) connectToRedisCluster() (redis.Conn, error) {
uri := e.redisAddr
if strings.Contains(uri, "://") {
u, _ := url.Parse(uri)
if u.Port() == "" {
uri = u.Host + ":6379"
} else {
uri = u.Host
}
} else {
if frags := strings.Split(uri, ":"); len(frags) != 2 {
uri = uri + ":6379"
}
}
options, err := e.configureOptions(uri)
if err != nil {
return nil, err
}
log.Debugf("Creating cluster object")
cluster := redisc.Cluster{
StartupNodes: []string{uri},
DialOptions: options,
}
log.Debugf("Running refresh on cluster object")
if err := cluster.Refresh(); err != nil {
log.Errorf("Cluster refresh failed: %v", err)
}
log.Debugf("Creating redis connection object")
conn, err := cluster.Dial()
if err != nil {
log.Errorf("Dial failed: %v", err)
}
c, err := redisc.RetryConn(conn, 10, 100*time.Millisecond)
if err != nil {
log.Errorf("RetryConn failed: %v", err)
}
return c, err
}
func doRedisCmd(c redis.Conn, cmd string, args ...interface{}) (interface{}, error) {
log.Debugf("c.Do() - running command: %s %s", cmd, args)
res, err := c.Do(cmd, args...)
if err != nil {
log.Debugf("c.Do() - err: %s", err)
}
log.Debugf("c.Do() - done")
return res, err
}