mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-01-19 03:05:53 +08:00
Refactor flag parsing code
This commit is contained in:
parent
0527b6c483
commit
b7c2ebeff3
@ -12,7 +12,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
|
||||||
"github.com/hibiken/asynq"
|
"github.com/hibiken/asynq"
|
||||||
"github.com/hibiken/asynq/x/metrics"
|
"github.com/hibiken/asynq/x/metrics"
|
||||||
"github.com/hibiken/asynqmon"
|
"github.com/hibiken/asynqmon"
|
||||||
@ -83,69 +82,52 @@ func parseFlags(progname string, args []string) (cfg *Config, output string, err
|
|||||||
return &conf, buf.String(), nil
|
return &conf, buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Refactor this code!
|
func makeTLSConfig(cfg *Config) *tls.Config {
|
||||||
func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) {
|
if cfg.RedisTLS == "" && !cfg.RedisInsecureTLS {
|
||||||
var opts redis.UniversalOptions
|
return nil
|
||||||
sentinel := false
|
}
|
||||||
|
return &tls.Config{
|
||||||
|
ServerName: cfg.RedisTLS,
|
||||||
|
InsecureSkipVerify: cfg.RedisInsecureTLS,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if cfg.RedisClusterNodes != "" {
|
func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) {
|
||||||
opts.Addrs = strings.Split(cfg.RedisClusterNodes, ",")
|
// Connecting to redis-cluster
|
||||||
opts.Password = cfg.RedisPassword
|
if len(cfg.RedisClusterNodes) > 0 {
|
||||||
} else {
|
return asynq.RedisClusterClientOpt{
|
||||||
if cfg.RedisURL != "" {
|
Addrs: strings.Split(cfg.RedisClusterNodes, ","),
|
||||||
|
Password: cfg.RedisPassword,
|
||||||
|
TLSConfig: makeTLSConfig(cfg),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connecting to redis-sentinels
|
||||||
|
if strings.HasPrefix(cfg.RedisURL, "redis-sentinel") {
|
||||||
res, err := asynq.ParseRedisURI(cfg.RedisURL)
|
res, err := asynq.ParseRedisURI(cfg.RedisURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
switch v := res.(type) {
|
connOpt := res.(asynq.RedisFailoverClientOpt) // safe to type-assert
|
||||||
case asynq.RedisClientOpt:
|
connOpt.TLSConfig = makeTLSConfig(cfg)
|
||||||
opts.Addrs = append(opts.Addrs, v.Addr)
|
return connOpt, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connecting to single redis server
|
||||||
|
var connOpt asynq.RedisClientOpt
|
||||||
|
if len(cfg.RedisURL) > 0 {
|
||||||
|
res, err := asynq.ParseRedisURI(cfg.RedisURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
connOpt = res.(asynq.RedisClientOpt) // safe to type-assert
|
||||||
} else {
|
} else {
|
||||||
opts.Addrs = []string{cfg.RedisAddr}
|
connOpt.Addr = cfg.RedisAddr
|
||||||
opts.DB = cfg.RedisDB
|
connOpt.DB = cfg.RedisDB
|
||||||
opts.Password = cfg.RedisPassword
|
connOpt.Password = cfg.RedisPassword
|
||||||
}
|
}
|
||||||
}
|
connOpt.TLSConfig = makeTLSConfig(cfg)
|
||||||
|
return connOpt, nil
|
||||||
if cfg.RedisTLS != "" {
|
|
||||||
opts.TLSConfig = &tls.Config{ServerName: cfg.RedisTLS}
|
|
||||||
}
|
|
||||||
if cfg.RedisInsecureTLS {
|
|
||||||
if opts.TLSConfig == nil {
|
|
||||||
opts.TLSConfig = &tls.Config{}
|
|
||||||
}
|
|
||||||
opts.TLSConfig.InsecureSkipVerify = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.RedisClusterNodes != "" {
|
|
||||||
return asynq.RedisClusterClientOpt{
|
|
||||||
Addrs: opts.Addrs,
|
|
||||||
Password: opts.Password,
|
|
||||||
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,
|
|
||||||
Password: opts.Password,
|
|
||||||
TLSConfig: opts.TLSConfig,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -106,6 +106,7 @@ func TestMakeRedisConnOpt(t *testing.T) {
|
|||||||
MasterName: "mymaster",
|
MasterName: "mymaster",
|
||||||
SentinelAddrs: []string{
|
SentinelAddrs: []string{
|
||||||
"localhost:5000", "localhost:5001", "localhost:5002"},
|
"localhost:5000", "localhost:5001", "localhost:5002"},
|
||||||
|
Password: "secretpassword",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user