Skip to content

Commit

Permalink
Add sentinel connection support via redis-url
Browse files Browse the repository at this point in the history
  • Loading branch information
hibiken committed May 6, 2022
1 parent 6dbc580 commit 0527b6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
28 changes: 22 additions & 6 deletions cmd/asynqmon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions cmd/asynqmon/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 0527b6c

Please sign in to comment.