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
+14 -6
View File
@@ -119,11 +119,15 @@ type VideoConfig struct {
}
type RedisConfig struct {
Address string `yaml:"address"`
Username string `yaml:"username"`
Password string `yaml:"password"`
DB int `yaml:"db"`
UseTLS bool `yaml:"use_tls"`
Address string `yaml:"address"`
Username string `yaml:"username"`
Password string `yaml:"password"`
DB int `yaml:"db"`
UseTLS bool `yaml:"use_tls"`
MasterName string `yaml:"sentinel_master_name"`
SentinelUsername string `yaml:"sentinel_username"`
SentinelPassword string `yaml:"sentinel_password"`
SentinelAddresses []string `yaml:"sentinel_addresses"`
}
type RoomConfig struct {
@@ -289,7 +293,11 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) {
}
func (conf *Config) HasRedis() bool {
return conf.Redis.Address != ""
return conf.Redis.Address != "" || conf.Redis.SentinelAddresses != nil
}
func (conf *Config) UseSentinel() bool {
return conf.Redis.SentinelAddresses != nil
}
func (conf *Config) updateFromCLI(c *cli.Context) error {
+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")