Skip to content

Commit

Permalink
fix: panic when initializing rueidis.Client while Redis Cluster is no…
Browse files Browse the repository at this point in the history
…t available

Signed-off-by: Rueian <[email protected]>
  • Loading branch information
rueian committed May 11, 2024
1 parent 92698e9 commit da19971
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ type connrole struct {
replica bool
}

func newClusterClient(opt *ClientOption, connFn connFn) (client *clusterClient, err error) {
client = &clusterClient{
func newClusterClient(opt *ClientOption, connFn connFn) (*clusterClient, error) {
client := &clusterClient{
cmd: cmds.NewBuilder(cmds.InitSlot),
connFn: connFn,
opt: opt,
Expand All @@ -120,11 +120,11 @@ func newClusterClient(opt *ClientOption, connFn connFn) (client *clusterClient,
return cc
}

if err = client.init(); err != nil {
if err := client.init(); err != nil {
return nil, err
}

if err = client.refresh(context.Background()); err != nil {
if err := client.refresh(context.Background()); err != nil {
return client, err
}

Expand Down
20 changes: 20 additions & 0 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4330,3 +4330,23 @@ func TestClusterShardsParsing(t *testing.T) {
}
})
}

// https://github.com/redis/rueidis/issues/543
func TestConnectToNonAvailableCluster(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
_, err := NewClient(ClientOption{
InitAddress: []string{"127.0.0.1:3000", "127.0.0.1:3001", "127.0.0.1:3002"},
})
if err == nil {
t.Errorf("expected connect error")
}
}
}()
}
wg.Wait()
}

0 comments on commit da19971

Please sign in to comment.