-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
347 lines (321 loc) · 10.6 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
/*
Copyright © 2022 MicroOps-cn.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package redis
import (
"bytes"
"context"
"encoding"
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
"github.com/go-kit/log/level"
"github.com/go-redis/redis"
"github.com/gogo/protobuf/jsonpb"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"github.com/MicroOps-cn/fuck/clients/tls"
"github.com/MicroOps-cn/fuck/log"
"github.com/MicroOps-cn/fuck/safe"
"github.com/MicroOps-cn/fuck/signals"
w "github.com/MicroOps-cn/fuck/wrapper"
)
type Client struct {
client *redis.Client
options *Options
}
func (r Client) MarshalJSONPB(_ *jsonpb.Marshaler) ([]byte, error) {
return r.MarshalJSON()
}
func (r Client) MarshalJSON() ([]byte, error) {
return json.Marshal(r.options)
}
func (r *Client) UnmarshalJSON(data []byte) (err error) {
if r.options == nil {
r.options = &Options{}
}
if err = json.Unmarshal(data, r.options); err != nil {
return err
}
if r.client, err = NewRedisClient(context.Background(), r.options); err != nil {
return err
}
return
}
type Options struct {
o *redis.Options
URL string `json:"url,omitempty"`
Password safe.String `json:"password"`
// Database to be selected after connecting to the server.
DB *int `json:"db"`
// Maximum number of retries before giving up.
// Default is to not retry failed commands.
MaxRetries *int `json:"max_retries"`
// Minimum backoff between each retry.
// Default is 8 milliseconds; -1 disables backoff.
MinRetryBackoff *model.Duration `json:"min_retry_backoff"`
// Maximum backoff between each retry.
// Default is 512 milliseconds; -1 disables backoff.
MaxRetryBackoff *model.Duration `json:"max_retry_backoff"`
// Dial timeout for establishing new connections.
// Default is 5 seconds.
DialTimeout *model.Duration `json:"dial_timeout"`
// Timeout for socket reads. If reached, commands will fail
// with a timeout instead of blocking. Use value -1 for no timeout and 0 for default.
// Default is 3 seconds.
ReadTimeout *model.Duration `json:"read_timeout"`
// Timeout for socket writes. If reached, commands will fail
// with a timeout instead of blocking.
// Default is ReadTimeout.
WriteTimeout *model.Duration `json:"write_timeout"`
// Maximum number of socket connections.
// Default is 10 connections per every CPU as reported by runtime.NumCPU.
PoolSize *int `json:"pool_size"`
// Minimum number of idle connections which is useful when establishing
// new connection is slow.
MinIdleConns *int `json:"min_idle_conns"`
// Connection age at which client retires (closes) the connection.
// Default is to not close aged connections.
MaxConnAge *time.Duration `json:"max_conn_age"`
// Amount of time client waits for connection if all connections
// are busy before returning an error.
// Default is ReadTimeout + 1 second.
PoolTimeout *time.Duration `json:"pool_timeout"`
// Amount of time after which client closes idle connections.
// Should be less than server's timeout.
// Default is 5 minutes. -1 disables idle timeout check.
IdleTimeout *time.Duration `json:"idle_timeout"`
// Frequency of idle checks made by idle connections reaper.
// Default is 1 minute. -1 disables idle connections reaper,
// but idle connections are still discarded by the client
// if IdleTimeout is set.
IdleCheckFrequency *time.Duration `json:"idle_check_frequency"`
TLSConfig *tls.TLSOptions `json:"tls_config" yaml:"tls_config" mapstructure:"tls_config"`
}
func (o *Options) UnmarshalJSON(data []byte) (err error) {
if len(data) > 0 && data[0] == '"' {
if err = json.Unmarshal(data, &o.URL); err != nil {
return err
}
o.o, err = redis.ParseURL(o.URL)
if err != nil {
return fmt.Errorf("failed to parse redis url: %s", err)
}
return o.Password.SetValue(w.DefaultString(o.Password.String(), o.o.Password))
}
type plain Options
err = json.Unmarshal(data, (*plain)(o))
if err != nil {
return err
}
o.o, err = redis.ParseURL(o.URL)
if err != nil {
return fmt.Errorf("failed to parse redis url: %s", err)
}
if err = o.Password.SetValue(w.DefaultString(o.Password.String(), o.o.Password)); err != nil {
return err
}
o.o.DB = *w.DefaultPointer(o.DB, &o.o.DB)
o.o.MaxRetries = *w.DefaultPointer(o.MaxRetries, &o.o.MaxRetries)
o.o.MinRetryBackoff = *w.DefaultPointer[time.Duration]((*time.Duration)(o.MinRetryBackoff), &o.o.MinRetryBackoff)
o.o.MaxRetryBackoff = *w.DefaultPointer[time.Duration]((*time.Duration)(o.MaxRetryBackoff), &o.o.MaxRetryBackoff)
o.o.DialTimeout = *w.DefaultPointer[time.Duration]((*time.Duration)(o.DialTimeout), &o.o.DialTimeout)
o.o.ReadTimeout = *w.DefaultPointer[time.Duration]((*time.Duration)(o.ReadTimeout), &o.o.ReadTimeout)
o.o.WriteTimeout = *w.DefaultPointer[time.Duration]((*time.Duration)(o.WriteTimeout), &o.o.WriteTimeout)
o.o.PoolSize = *w.DefaultPointer(o.PoolSize, &o.o.PoolSize)
o.o.MinIdleConns = *w.DefaultPointer(o.MinIdleConns, &o.o.MinIdleConns)
o.o.MaxConnAge = *w.DefaultPointer(o.MaxConnAge, &o.o.MaxConnAge)
o.o.PoolTimeout = *w.DefaultPointer(o.PoolTimeout, &o.o.PoolTimeout)
o.o.IdleTimeout = *w.DefaultPointer(o.IdleTimeout, &o.o.IdleTimeout)
o.o.IdleCheckFrequency = *w.DefaultPointer(o.IdleCheckFrequency, &o.o.IdleCheckFrequency)
if o.TLSConfig != nil {
tlsConfig, err := tls.NewTLSConfig(o.TLSConfig)
if err != nil {
return err
}
o.o.TLSConfig = tlsConfig
}
return nil
}
func NewRedisClient(ctx context.Context, option *Options) (*redis.Client, error) {
var err error
logger := log.GetContextLogger(ctx)
o := *option.o
o.Password, err = option.Password.UnsafeString()
if err != nil {
return nil, err
}
level.Debug(logger).Log("msg", "connect to redis server", "host", o.Addr, "db", o.DB)
client := redis.NewClient(&o)
if err = client.Ping().Err(); err != nil {
level.Error(logger).Log("msg", "Redis connection failed", "err", err)
_ = client.Close()
return nil, err
}
level.Info(logger).Log("msg", "connected to redis server", "host", option.o.Addr, "db", option.o.DB)
stopCh := signals.SetupSignalHandler(logger)
stopCh.PreStop(signals.LevelDB, func() {
if err = client.Close(); err != nil {
level.Error(logger).Log("msg", "Redis client shutdown error", "err", err)
time.Sleep(1 * time.Second)
}
level.Debug(logger).Log("msg", "Closed Redis connection")
})
return client, nil
}
func (o *Options) GetPeer() (string, int) {
u, err := url.Parse(o.URL)
if err != nil {
return "", 0
}
port, _ := strconv.Atoi(u.Port())
return u.Hostname(), port
}
func parseArg(v interface{}) (string, error) {
switch v := v.(type) {
case nil:
return "", nil
case string:
return v, nil
case []byte:
return string(v), nil
case int:
return strconv.Itoa(v), nil
case int8:
return strconv.Itoa(int(v)), nil
case int16:
return strconv.Itoa(int(v)), nil
case int32:
return strconv.Itoa(int(v)), nil
case int64:
return strconv.FormatInt(v, 10), nil
case uint:
return strconv.FormatUint(uint64(v), 10), nil
case uint8:
return strconv.FormatUint(uint64(v), 10), nil
case uint16:
return strconv.FormatUint(uint64(v), 10), nil
case uint32:
return strconv.FormatUint(uint64(v), 10), nil
case uint64:
return strconv.FormatUint(v, 10), nil
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 64), nil
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), nil
case bool:
return strconv.FormatBool(v), nil
case encoding.BinaryMarshaler:
b, err := v.MarshalBinary()
if err != nil {
return "", err
}
return string(b), nil
default:
return "", fmt.Errorf(
"redis: can't marshal %T (implement encoding.BinaryMarshaler)", v)
}
}
type Cmder struct {
redis.Cmder
}
func (c Cmder) String() string {
var buf bytes.Buffer
for idx, arg := range c.Args() {
if idx != 0 {
buf.WriteString(" ")
}
if s, err := parseArg(arg); err != nil {
buf.Write(w.M(json.Marshal(fmt.Sprintf("<%s>", err))))
} else {
buf.WriteString(s)
}
}
return buf.String()
}
const instrumentationName = "github.com/MicroOps-cn/fuck/clients/redis"
func (r *Client) Redis(ctx context.Context) *redis.Client {
tracer := otel.GetTracerProvider().Tracer(instrumentationName)
logger := log.GetContextLogger(ctx, log.WithCaller(7))
session := r.client.WithContext(ctx)
host, port := r.options.GetPeer()
session.WrapProcess(func(oldProcess func(cmd redis.Cmder) error) func(cmd redis.Cmder) error {
return func(cmd redis.Cmder) (err error) {
_, span := tracer.Start(ctx, "ExecuteRedisCommand."+cmd.Name(),
trace.WithAttributes(
attribute.String("db.info", r.client.String()),
attribute.String("net.peer.name", host),
attribute.Int("net.peer.port", port),
attribute.String("db.statement", (&Cmder{Cmder: cmd}).String()),
attribute.String("db.system", "redis"),
),
)
defer func() {
span.End()
if err != nil {
if err != redis.Nil {
span.SetStatus(codes.Error, err.Error())
level.Error(logger).Log("msg", "failed to exec Redis Command", "cmd", Cmder{Cmder: cmd}, "err", err)
} else {
span.SetStatus(codes.Error, err.Error())
level.Debug(logger).Log("msg", "failed to exec Redis Command", "cmd", Cmder{Cmder: cmd}, "err", err)
}
} else {
span.SetStatus(codes.Ok, "")
level.Debug(logger).Log("msg", "exec Redis Command", "cmd", Cmder{Cmder: cmd})
}
}()
return oldProcess(cmd)
}
})
return session
}
var ErrStopLoop = errors.New("stop")
func ForeachSet(ctx context.Context, c *redis.Client, key string, cursor uint64, pageSize int64, f func(key, val string) error) (err error) {
var listLength int64
if ret, err := c.SCard(key).Result(); err != nil {
return err
} else if listLength = ret; listLength == 0 {
return nil
}
if pageSize == 0 {
pageSize = 100
}
var ret []string
for {
select {
case <-ctx.Done():
default:
ret, cursor, err = c.SScan(key, cursor, "*", pageSize).Result()
if err != nil {
return err
}
for _, member := range ret {
if err = f(key, member); err != nil {
if err == ErrStopLoop {
break
}
return err
}
}
if int64(len(ret)) < pageSize {
return nil
}
}
}
}