Skip to content

Commit

Permalink
fix when prefer_replica set to true, master node is nil (#843)
Browse files Browse the repository at this point in the history
Co-authored-by: dywang <[email protected]>
  • Loading branch information
wdy62808 and dywang-moonton authored Jul 19, 2024
1 parent 5c11f28 commit 8e54c49
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions internal/client/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ func NewRedisClient(ctx context.Context, address string, username string, passwo
reply = r.DoWithStringReply("info", "replication")
// get best replica
if replica {
replicaInfo := getReplicaAddr(reply)
// slave node return BestReplica is none,set value by address
if replicaInfo.BestReplica == "" {
replicaInfo.BestReplica = address
}
replicaInfo := getReplicaAddr(reply, address)
log.Infof("best replica: %s", replicaInfo.BestReplica)
r = NewRedisClient(ctx, replicaInfo.BestReplica, username, password, Tls, false)
}
Expand All @@ -98,7 +94,7 @@ type RedisReplicaInfo struct {
BestReplica string
}

func getReplicaAddr(info string) RedisReplicaInfo {
func getReplicaAddr(info, addr string) RedisReplicaInfo {
infoReplica := RedisReplicaInfo{}
replicas := make([]Replica, 0)
slaveInfoRegexp := regexp.MustCompile(`slave\d+:ip=.*`)
Expand All @@ -107,6 +103,7 @@ func getReplicaAddr(info string) RedisReplicaInfo {
switch {
case strings.HasPrefix(line, "role:slave"):
infoReplica.Role = "slave"
infoReplica.BestReplica = addr
return infoReplica
case strings.HasPrefix(line, "role:master"):
infoReplica.Role = "master"
Expand Down

0 comments on commit 8e54c49

Please sign in to comment.