Skip to content

Commit 2144310

Browse files
dimvmihailenco
authored andcommitted
Use node address instead of relying on loopback reported by redis
1 parent b52814f commit 2144310

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

cluster.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package redis
33
import (
44
"fmt"
55
"math/rand"
6+
"net"
67
"sync"
78
"sync/atomic"
89
"time"
@@ -244,7 +245,7 @@ type clusterState struct {
244245
slots [][]*clusterNode
245246
}
246247

247-
func newClusterState(nodes *clusterNodes, slots []ClusterSlot) (*clusterState, error) {
248+
func newClusterState(nodes *clusterNodes, slots []ClusterSlot, origin string) (*clusterState, error) {
248249
c := clusterState{
249250
nodes: nodes,
250251
slots: make([][]*clusterNode, hashtag.SlotNumber),
@@ -253,7 +254,12 @@ func newClusterState(nodes *clusterNodes, slots []ClusterSlot) (*clusterState, e
253254
for _, slot := range slots {
254255
var nodes []*clusterNode
255256
for _, slotNode := range slot.Nodes {
256-
node, err := c.nodes.Get(slotNode.Addr)
257+
addr := slotNode.Addr
258+
if isLoopbackAddr(addr) && !isLoopbackAddr(origin) {
259+
addr = origin
260+
}
261+
262+
node, err := c.nodes.Get(addr)
257263
if err != nil {
258264
return nil, err
259265
}
@@ -661,7 +667,7 @@ func (c *ClusterClient) reloadSlots() (*clusterState, error) {
661667
return nil, err
662668
}
663669

664-
return newClusterState(c.nodes, slots)
670+
return newClusterState(c.nodes, slots, node.Client.opt.Addr)
665671
}
666672

667673
// reaper closes idle connections to the cluster.
@@ -960,3 +966,17 @@ func (c *ClusterClient) txPipelineReadQueued(
960966

961967
return firstErr
962968
}
969+
970+
func isLoopbackAddr(addr string) bool {
971+
host, _, err := net.SplitHostPort(addr)
972+
if err != nil {
973+
return false
974+
}
975+
976+
ip := net.ParseIP(host)
977+
if ip == nil {
978+
return false
979+
}
980+
981+
return ip.IsLoopback()
982+
}

0 commit comments

Comments
 (0)