forked from envoyproxy/ratelimit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver_impl.go
192 lines (159 loc) · 5.04 KB
/
driver_impl.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
package redis
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/mediocregopher/radix/v3/trace"
stats "github.com/lyft/gostats"
"github.com/mediocregopher/radix/v3"
logger "github.com/sirupsen/logrus"
)
type poolStats struct {
connectionActive stats.Gauge
connectionTotal stats.Counter
connectionClose stats.Counter
}
func newPoolStats(scope stats.Scope) poolStats {
ret := poolStats{}
ret.connectionActive = scope.NewGauge("cx_active")
ret.connectionTotal = scope.NewCounter("cx_total")
ret.connectionClose = scope.NewCounter("cx_local_close")
return ret
}
func poolTrace(ps *poolStats) trace.PoolTrace {
return trace.PoolTrace{
ConnCreated: func(_ trace.PoolConnCreated) {
ps.connectionTotal.Add(1)
ps.connectionActive.Add(1)
},
ConnClosed: func(_ trace.PoolConnClosed) {
ps.connectionActive.Sub(1)
ps.connectionClose.Add(1)
},
}
}
type clientImpl struct {
client radix.Client
stats poolStats
implicitPipelining bool
}
func checkError(err error) {
if err != nil {
panic(RedisError(err.Error()))
}
}
func GetClientTLSConfig(redisTlsCACerts string) (*tls.Config, error) {
// Get the SystemCertPool, continue with an empty pool on error
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
// Read in the cert file
certs, err := ioutil.ReadFile(redisTlsCACerts)
if err != nil {
return nil, err
}
// Append our cert to the system pool
if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
logger.Warnf("No certs appended, using system certs only")
}
// Trust the augmented cert pool in our client
return &tls.Config{
RootCAs: rootCAs,
}, nil
}
func NewClientImpl(scope stats.Scope, useTls bool, auth, redisSocketType, redisType, url string, poolSize int,
pipelineWindow time.Duration, pipelineLimit int, tlsCACerts string) Client {
logger.Warnf("connecting to redis on %s with pool size %d", url, poolSize)
df := func(network, addr string) (radix.Conn, error) {
var dialOpts []radix.DialOpt
if useTls {
if tlsCACerts != "" {
tlsConfig, err := GetClientTLSConfig(tlsCACerts)
checkError(err)
dialOpts = append(dialOpts, radix.DialUseTLS(tlsConfig))
} else {
dialOpts = append(dialOpts, radix.DialUseTLS(&tls.Config{}))
}
}
if auth != "" {
logger.Warnf("enabling authentication to redis on %s", url)
dialOpts = append(dialOpts, radix.DialAuthPass(auth))
}
return radix.Dial(network, addr, dialOpts...)
}
stats := newPoolStats(scope)
opts := []radix.PoolOpt{radix.PoolConnFunc(df), radix.PoolWithTrace(poolTrace(&stats))}
implicitPipelining := true
if pipelineWindow == 0 && pipelineLimit == 0 {
implicitPipelining = false
} else {
opts = append(opts, radix.PoolPipelineWindow(pipelineWindow, pipelineLimit))
}
logger.Debugf("Implicit pipelining enabled: %v", implicitPipelining)
poolFunc := func(network, addr string) (radix.Client, error) {
return radix.NewPool(network, addr, poolSize, opts...)
}
var client radix.Client
var err error
switch strings.ToLower(redisType) {
case "single":
client, err = poolFunc(redisSocketType, url)
case "cluster":
urls := strings.Split(url, ",")
if implicitPipelining == false {
panic(RedisError("Implicit Pipelining must be enabled to work with Redis Cluster Mode. Set values for REDIS_PIPELINE_WINDOW or REDIS_PIPELINE_LIMIT to enable implicit pipelining"))
}
logger.Warnf("Creating cluster with urls %v", urls)
client, err = radix.NewCluster(urls, radix.ClusterPoolFunc(poolFunc))
case "sentinel":
urls := strings.Split(url, ",")
if len(urls) < 2 {
panic(RedisError("Expected master name and a list of urls for the sentinels, in the format: <redis master name>,<sentinel1>,...,<sentineln>"))
}
client, err = radix.NewSentinel(urls[0], urls[1:], radix.SentinelPoolFunc(poolFunc))
default:
panic(RedisError("Unrecognized redis type " + redisType))
}
checkError(err)
// Check if connection is good
var pingResponse string
checkError(client.Do(radix.Cmd(&pingResponse, "PING")))
if pingResponse != "PONG" {
checkError(fmt.Errorf("connecting redis error: %s", pingResponse))
}
return &clientImpl{
client: client,
stats: stats,
implicitPipelining: implicitPipelining,
}
}
func (c *clientImpl) DoCmd(rcv interface{}, cmd, key string, args ...interface{}) error {
return c.client.Do(radix.FlatCmd(rcv, cmd, key, args...))
}
func (c *clientImpl) Close() error {
return c.client.Close()
}
func (c *clientImpl) NumActiveConns() int {
return int(c.stats.connectionActive.Value())
}
func (c *clientImpl) PipeAppend(pipeline Pipeline, rcv interface{}, cmd, key string, args ...interface{}) Pipeline {
return append(pipeline, radix.FlatCmd(rcv, cmd, key, args...))
}
func (c *clientImpl) PipeDo(pipeline Pipeline) error {
if c.implicitPipelining {
for _, action := range pipeline {
if err := c.client.Do(action); err != nil {
return err
}
}
return nil
}
return c.client.Do(radix.Pipeline(pipeline...))
}
func (c *clientImpl) ImplicitPipeliningEnabled() bool {
return c.implicitPipelining
}