Add support for Redis Sentinel (#707)

This commit is contained in:
Brint E. Kriebel
2022-05-28 10:26:58 -07:00
committed by GitHub
parent 80f8bc6c62
commit 5a74c1695d
6 changed files with 91 additions and 44 deletions
+30 -16
View File
@@ -109,25 +109,39 @@ func createRedisClient(conf *config.Config) (*redis.Client, error) {
return nil, nil
}
logger.Infow("using multi-node routing via redis", "addr", conf.Redis.Address)
rcOptions := &redis.Options{
Addr: conf.Redis.Address,
Username: conf.Redis.Username,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
}
var rc *redis.Client
var tlsConfig *tls.Config
if conf.Redis.UseTLS {
rcOptions = &redis.Options{
Addr: conf.Redis.Address,
Username: conf.Redis.Username,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
}
}
rc := redis.NewClient(rcOptions)
if conf.UseSentinel() {
logger.Infow("using multi-node routing via redis", "sentinel", true, "addr", conf.Redis.SentinelAddresses, "masterName", conf.Redis.MasterName)
rcOptions := &redis.FailoverOptions{
SentinelAddrs: conf.Redis.SentinelAddresses,
SentinelUsername: conf.Redis.SentinelUsername,
SentinelPassword: conf.Redis.SentinelPassword,
MasterName: conf.Redis.MasterName,
Username: conf.Redis.Username,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
TLSConfig: tlsConfig,
}
rc = redis.NewFailoverClient(rcOptions)
} else {
logger.Infow("using multi-node routing via redis", "sentinel", false, "addr", conf.Redis.Address)
rcOptions := &redis.Options{
Addr: conf.Redis.Address,
Username: conf.Redis.Username,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
TLSConfig: tlsConfig,
}
rc = redis.NewClient(rcOptions)
}
if err := rc.Ping(context.Background()).Err(); err != nil {
err = errors.Wrap(err, "unable to connect to redis")
+32 -18
View File
@@ -1,8 +1,7 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject
//+build !wireinject
package service
@@ -133,25 +132,40 @@ func createRedisClient(conf *config.Config) (*redis.Client, error) {
if !conf.HasRedis() {
return nil, nil
}
logger.Infow("using multi-node routing via redis", "addr", conf.Redis.Address)
rcOptions := &redis.Options{
Addr: conf.Redis.Address,
Username: conf.Redis.Username,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
}
var rc *redis.Client
var tlsConfig *tls.Config
if conf.Redis.UseTLS {
rcOptions = &redis.Options{
Addr: conf.Redis.Address,
Username: conf.Redis.Username,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
}
}
rc := redis.NewClient(rcOptions)
if conf.UseSentinel() {
logger.Infow("using multi-node routing via redis", "sentinel", true, "addr", conf.Redis.SentinelAddresses, "masterName", conf.Redis.MasterName)
rcOptions := &redis.FailoverOptions{
SentinelAddrs: conf.Redis.SentinelAddresses,
SentinelUsername: conf.Redis.SentinelUsername,
SentinelPassword: conf.Redis.SentinelPassword,
MasterName: conf.Redis.MasterName,
Username: conf.Redis.Username,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
TLSConfig: tlsConfig,
}
rc = redis.NewFailoverClient(rcOptions)
} else {
logger.Infow("using multi-node routing via redis", "sentinel", false, "addr", conf.Redis.Address)
rcOptions := &redis.Options{
Addr: conf.Redis.Address,
Username: conf.Redis.Username,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
TLSConfig: tlsConfig,
}
rc = redis.NewClient(rcOptions)
}
if err := rc.Ping(context.Background()).Err(); err != nil {
err = errors.Wrap(err, "unable to connect to redis")