Add sentinel connection support via redis-url

This commit is contained in:
Ken Hibino 2022-05-04 06:47:08 -07:00
parent 6dbc580738
commit 0527b6c483
2 changed files with 33 additions and 6 deletions

View File

@ -83,23 +83,31 @@ func parseFlags(progname string, args []string) (cfg *Config, output string, err
return &conf, buf.String(), nil
}
// TODO: Write test and refactor this code.
// TODO: Refactor this code!
func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) {
var opts redis.UniversalOptions
sentinel := false
if cfg.RedisClusterNodes != "" {
opts.Addrs = strings.Split(cfg.RedisClusterNodes, ",")
opts.Password = cfg.RedisPassword
} else {
if cfg.RedisURL != "" {
res, err := redis.ParseURL(cfg.RedisURL)
res, err := asynq.ParseRedisURI(cfg.RedisURL)
if err != nil {
return nil, err
}
opts.Addrs = append(opts.Addrs, res.Addr)
opts.DB = res.DB
opts.Password = res.Password
switch v := res.(type) {
case asynq.RedisClientOpt:
opts.Addrs = append(opts.Addrs, v.Addr)
opts.DB = v.DB
opts.Password = v.Password
case asynq.RedisFailoverClientOpt:
opts.Addrs = append(opts.Addrs, v.SentinelAddrs...)
opts.SentinelPassword = v.SentinelPassword
opts.MasterName = v.MasterName
sentinel = true
}
} else {
opts.Addrs = []string{cfg.RedisAddr}
opts.DB = cfg.RedisDB
@ -124,6 +132,14 @@ func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) {
TLSConfig: opts.TLSConfig,
}, nil
}
if sentinel {
return asynq.RedisFailoverClientOpt{
MasterName: opts.MasterName,
SentinelAddrs: opts.Addrs,
SentinelPassword: opts.SentinelPassword,
TLSConfig: opts.TLSConfig,
}, nil
}
return asynq.RedisClientOpt{
Addr: opts.Addrs[0],
DB: opts.DB,

View File

@ -97,6 +97,17 @@ func TestMakeRedisConnOpt(t *testing.T) {
Password: "bar",
},
},
{
desc: "With redis-sentinel URL",
cfg: &Config{
RedisURL: "redis-sentinel://:secretpassword@localhost:5000,localhost:5001,localhost:5002?master=mymaster",
},
want: asynq.RedisFailoverClientOpt{
MasterName: "mymaster",
SentinelAddrs: []string{
"localhost:5000", "localhost:5001", "localhost:5002"},
},
},
{
desc: "With cluster nodes",
cfg: &Config{