Skip to content

Commit

Permalink
feat: hide InitAddress when connecting to a Redis cluster using NewCl…
Browse files Browse the repository at this point in the history
…ient()
  • Loading branch information
unknowntpo committed Dec 7, 2024
1 parent 06ed4fa commit e07440a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
16 changes: 12 additions & 4 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type clusterClient struct {
type connrole struct {
conn conn
replica bool
hidden bool
}

func newClusterClient(opt *ClientOption, connFn connFn, retryer retryHandler) (*clusterClient, error) {
Expand Down Expand Up @@ -204,7 +205,8 @@ func (c *clusterClient) _refresh() (err error) {
for _, addr := range c.opt.InitAddress {
if _, ok := conns[addr]; !ok {
conns[addr] = connrole{
conn: c.connFn(addr, c.opt),
conn: c.connFn(addr, c.opt),
hidden: true,
}
}
}
Expand All @@ -218,6 +220,7 @@ func (c *clusterClient) _refresh() (err error) {
conns[addr] = connrole{
conn: cc.conn,
replica: fresh.replica,
hidden: fresh.hidden,
}
} else {
removes = append(removes, cc.conn)
Expand Down Expand Up @@ -289,8 +292,11 @@ func (c *clusterClient) single() (conn conn) {
func (c *clusterClient) nodes() []string {
c.mu.RLock()
nodes := make([]string, 0, len(c.conns))
for addr := range c.conns {
nodes = append(nodes, addr)
for addr, role := range c.conns {
// only display non-hidden connrole
if !role.hidden {
nodes = append(nodes, addr)
}
}
c.mu.RUnlock()
return nodes
Expand Down Expand Up @@ -1113,7 +1119,9 @@ func (c *clusterClient) Nodes() map[string]Client {
nodes := make(map[string]Client, len(c.conns))
disableCache := c.opt != nil && c.opt.DisableCache
for addr, cc := range c.conns {
nodes[addr] = newSingleClientWithConn(cc.conn, c.cmd, c.retry, disableCache, c.retryHandler)
if !cc.hidden {
nodes[addr] = newSingleClientWithConn(cc.conn, c.cmd, c.retry, disableCache, c.retryHandler)
}
}
c.mu.RUnlock()
return nodes
Expand Down
21 changes: 9 additions & 12 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,10 +876,9 @@ func TestClusterClientInit(t *testing.T) {
testFunc := func(t *testing.T, client *clusterClient, num *int64) {
nodes := client.nodes()
sort.Strings(nodes)
if len(nodes) != 3 ||
if len(nodes) != 2 ||
nodes[0] != "127.0.0.1:0" ||
nodes[1] != "127.0.1.1:1" ||
nodes[2] != "127.0.2.1:2" {
nodes[1] != "127.0.1.1:1" {
t.Fatalf("unexpected nodes %v", nodes)
}

Expand All @@ -891,10 +890,8 @@ func TestClusterClientInit(t *testing.T) {

nodes = client.nodes()
sort.Strings(nodes)
if len(nodes) != 3 ||
nodes[0] != "127.0.1.1:1" ||
nodes[1] != "127.0.2.1:2" ||
nodes[2] != "127.0.3.1:3" {
if len(nodes) != 1 ||
nodes[0] != "127.0.3.1:3" {
t.Fatalf("unexpected nodes %v", nodes)
}
}
Expand Down Expand Up @@ -963,11 +960,11 @@ func TestClusterClientInit(t *testing.T) {
}
nodes := client.nodes()
sort.Strings(nodes)
if len(nodes) != 4 ||
nodes[0] != "127.0.0.1:0" ||
nodes[1] != "127.0.1.1:1" ||
nodes[2] != "127.0.2.1:2" ||
nodes[3] != "127.0.3.1:3" {
if len(nodes) != 3 ||
// 127.0.0.1:0 is hidden
nodes[0] != "127.0.1.1:1" ||
nodes[1] != "127.0.2.1:2" ||
nodes[2] != "127.0.3.1:3" {
t.Fatalf("unexpected nodes %v", nodes)
}
})
Expand Down

0 comments on commit e07440a

Please sign in to comment.