Skip to content

Commit

Permalink
Merge pull request #121 from spotahome/fix-multiple-values-custom-config
Browse files Browse the repository at this point in the history
Fix multiple values custom config
  • Loading branch information
jchanam authored Feb 27, 2019
2 parents e9036c4 + d8c722c commit ebb70bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
4 changes: 4 additions & 0 deletions example/redisfailover/custom-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ spec:
- "maxclients 100"
- "hz 50"
- "timeout 60"
- "tcp-keepalive 60"
- "client-output-buffer-limit normal 0 0 0"
- "client-output-buffer-limit slave 1000000000 1000000000 0"
- "client-output-buffer-limit pubsub 33554432 8388608 60"
44 changes: 25 additions & 19 deletions service/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ const (
redisPort = "6379"
sentinelPort = "26379"
masterName = "mymaster"
sentinelSetCommand = "SENTINEL set %s %s"
redisSetCommand = "CONFIG set %s"
)

var (
Expand Down Expand Up @@ -177,7 +175,7 @@ func (c *client) MonitorRedis(ip string, monitor string, quorum string) error {
defer rClient.Close()
cmd := rediscli.NewBoolCmd("SENTINEL", "REMOVE", masterName)
rClient.Process(cmd)
// We'll continue even if it fails, the priotity is to have the redises monitored
// We'll continue even if it fails, the priority is to have the redises monitored
cmd = rediscli.NewBoolCmd("SENTINEL", "MONITOR", masterName, monitor, redisPort, quorum)
rClient.Process(cmd)
_, err := cmd.Result()
Expand Down Expand Up @@ -243,8 +241,11 @@ func (c *client) SetCustomSentinelConfig(ip string, configs []string) error {
defer rClient.Close()

for _, config := range configs {
setCommand := fmt.Sprintf(sentinelSetCommand, masterName, config)
if err := c.applyConfig(setCommand, rClient); err != nil {
param, value, err := c.getConfigParameters(config)
if err != nil {
return err
}
if err := c.applySentinelConfig(param, value, rClient); err != nil {
return err
}
}
Expand All @@ -261,27 +262,32 @@ func (c *client) SetCustomRedisConfig(ip string, configs []string) error {
defer rClient.Close()

for _, config := range configs {
setCommand := fmt.Sprintf(redisSetCommand, config)
if err := c.applyConfig(setCommand, rClient); err != nil {
param, value, err := c.getConfigParameters(config)
if err != nil {
return err
}
if err := c.applyRedisConfig(param, value, rClient); err != nil {
return err
}
}
return nil
}

func (c *client) applyConfig(command string, rClient *rediscli.Client) error {
sc := strings.Split(command, " ")
// Required conversion due to language specifications
// https://golang.org/doc/faq#convert_slice_of_interface
s := make([]interface{}, len(sc))
for i, v := range sc {
s[i] = v
}
func (c *client) applyRedisConfig(parameter string, value string, rClient *rediscli.Client) error {
result := rClient.ConfigSet(parameter, value)
return result.Err()
}

cmd := rediscli.NewBoolCmd(s...)
func (c *client) applySentinelConfig(parameter string, value string, rClient *rediscli.Client) error {
cmd := rediscli.NewStatusCmd("SENTINEL", "set", masterName, parameter, value)
rClient.Process(cmd)
if _, err := cmd.Result(); err != nil {
return err
return cmd.Err()
}

func (c *client) getConfigParameters(config string) (parameter string, value string, err error) {
s := strings.Split(config, " ")
if len(s) < 2 {
return "", "", fmt.Errorf("configuration '%s' malformed", config)
}
return nil
return s[0], strings.Join(s[1:], " "), nil
}

0 comments on commit ebb70bd

Please sign in to comment.