Skip to content

Commit 6ab4a8d

Browse files
committed
remove nest for loop when GetNumberSentinelSlavesInMemory
1 parent 82af0a6 commit 6ab4a8d

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

pkg/client/redis/client.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"net"
7-
"reflect"
87
"regexp"
98
"strconv"
109
"strings"
@@ -104,24 +103,27 @@ func (c *client) GetNumberSentinelSlavesInMemory(ip string, auth *util.AuthConfi
104103
return 0, err
105104
}
106105
nSlaves := len(slaveInfoBlobs)
107-
OUTER:
108106
for _, slaveInfoBlob := range slaveInfoBlobs {
109-
slaveInfo := reflect.ValueOf(slaveInfoBlob)
110-
for key, value := range slaveInfoBlob.([]interface{}) {
111-
stringValue := value.(string)
112-
if stringValue == "slave-priority" {
113-
slavePriority := fmt.Sprintf("%+v", slaveInfo.Index(key+1))
114-
if slavePriority == "0" {
115-
nSlaves -= 1
116-
}
117-
continue OUTER
118-
}
107+
slavePriority := slaveInfoFieldByName("slave-priority", slaveInfoBlob)
108+
if slavePriority == "0" {
109+
nSlaves -= 1
119110
}
120111
}
121112

122113
return int32(nSlaves), nil
123114
}
124115

116+
func slaveInfoFieldByName(name string, slaveInfoBlob interface{}) string {
117+
slaveInfo := slaveInfoBlob.([]interface{})
118+
for key, value := range slaveInfo {
119+
stringValue := value.(string)
120+
if stringValue == name {
121+
return slaveInfo[key+1].(string)
122+
}
123+
}
124+
return ""
125+
}
126+
125127
func isSentinelReady(info string) error {
126128
matchStatus := sentinelStatusRE.FindStringSubmatch(info)
127129
if len(matchStatus) == 0 || matchStatus[1] != "ok" {

0 commit comments

Comments
 (0)