Add clean up of cluster addrs

This commit is contained in:
Denis Maksimov 2023-09-16 12:31:53 +03:00
parent 4ad1a27a4c
commit ed0d974851
2 changed files with 21 additions and 0 deletions

View File

@ -98,8 +98,18 @@ func makeTLSConfig(cfg *Config) *tls.Config {
} }
func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) { func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) {
cleanUpClusterNodes := func(clusterNodes string) string {
clusterNodes = strings.ReplaceAll(clusterNodes, "]", "")
clusterNodes = strings.ReplaceAll(clusterNodes, "[", "")
clusterNodes = strings.ReplaceAll(clusterNodes, "\"", "")
clusterNodes = strings.ReplaceAll(clusterNodes, " ", "")
clusterNodes = strings.ReplaceAll(clusterNodes, "\n", "")
return clusterNodes
}
// Connecting to redis-cluster // Connecting to redis-cluster
if len(cfg.RedisClusterNodes) > 0 { if len(cfg.RedisClusterNodes) > 0 {
cfg.RedisClusterNodes = cleanUpClusterNodes(cfg.RedisClusterNodes)
return asynq.RedisClusterClientOpt{ return asynq.RedisClusterClientOpt{
Addrs: strings.Split(cfg.RedisClusterNodes, ","), Addrs: strings.Split(cfg.RedisClusterNodes, ","),
Password: cfg.RedisPassword, Password: cfg.RedisPassword,

View File

@ -119,6 +119,17 @@ func TestMakeRedisConnOpt(t *testing.T) {
"localhost:5000", "localhost:5001", "localhost:5002", "localhost:5003", "localhost:5004", "localhost:5005"}, "localhost:5000", "localhost:5001", "localhost:5002", "localhost:5003", "localhost:5004", "localhost:5005"},
}, },
}, },
{
desc: "With cluster nodes array",
cfg: &Config{
RedisClusterNodes: `["localhost:5000", "localhost:5001", "localhost:5002",
"localhost:5003", "localhost:5004", "localhost:5005"]`,
},
want: asynq.RedisClusterClientOpt{
Addrs: []string{
"localhost:5000", "localhost:5001", "localhost:5002", "localhost:5003", "localhost:5004", "localhost:5005"},
},
},
} }
for _, tc := range tests { for _, tc := range tests {