/
redis.go
87 lines (74 loc) · 2.14 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
package common
import (
"crypto/tls"
"time"
"github.com/gomodule/redigo/redis"
"github.com/RichardKnop/machinery/v1/config"
)
var (
defaultConfig = &config.RedisConfig{
MaxIdle: 10,
MaxActive: 100,
IdleTimeout: 300,
Wait: true,
ReadTimeout: 15,
WriteTimeout: 15,
ConnectTimeout: 15,
NormalTasksPollPeriod: 1000,
DelayedTasksPollPeriod: 20,
}
)
// RedisConnector ...
type RedisConnector struct{}
// NewPool returns a new pool of Redis connections
func (rc *RedisConnector) NewPool(socketPath, host, password string, db int, cnf *config.RedisConfig, tlsConfig *tls.Config) *redis.Pool {
if cnf == nil {
cnf = defaultConfig
}
return &redis.Pool{
MaxIdle: cnf.MaxIdle,
IdleTimeout: time.Duration(cnf.IdleTimeout) * time.Second,
MaxActive: cnf.MaxActive,
Wait: cnf.Wait,
Dial: func() (redis.Conn, error) {
c, err := rc.open(socketPath, host, password, db, cnf, tlsConfig)
if err != nil {
return nil, err
}
if db != 0 {
_, err = c.Do("SELECT", db)
if err != nil {
return nil, err
}
}
return c, err
},
// PINGs connections that have been idle more than 10 seconds
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if time.Since(t) < time.Duration(10*time.Second) {
return nil
}
_, err := c.Do("PING")
return err
},
}
}
// Open a new Redis connection
func (rc *RedisConnector) open(socketPath, host, password string, db int, cnf *config.RedisConfig, tlsConfig *tls.Config) (redis.Conn, error) {
var opts = []redis.DialOption{
redis.DialDatabase(db),
redis.DialReadTimeout(time.Duration(cnf.ReadTimeout) * time.Second),
redis.DialWriteTimeout(time.Duration(cnf.WriteTimeout) * time.Second),
redis.DialConnectTimeout(time.Duration(cnf.ConnectTimeout) * time.Second),
}
if tlsConfig != nil {
opts = append(opts, redis.DialTLSConfig(tlsConfig), redis.DialUseTLS(true))
}
if password != "" {
opts = append(opts, redis.DialPassword(password))
}
if socketPath != "" {
return redis.Dial("unix", socketPath, opts...)
}
return redis.Dial("tcp", host, opts...)
}