Skip to content

Commit

Permalink
net/redisclient: fix nil pointer dereference (#2939)
Browse files Browse the repository at this point in the history
Fix use of logger before initialization.

Followup on #2261

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Feb 19, 2024
1 parent f99cd22 commit da3d600
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 8 additions & 5 deletions net/redisclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ func NewRedisRingClient(ro *RedisOptions) *RedisRingClient {
ringOptions.NewConsistentHash = NewMultiprobe
}

if ro.Log == nil {
ro.Log = &logging.DefaultLog{}
}

if ro.AddrUpdater != nil {
address, err := ro.AddrUpdater()
for i := 0; i < retryCount; i++ {
Expand All @@ -232,16 +236,15 @@ func NewRedisRingClient(ro *RedisOptions) *RedisRingClient {
address, err = ro.AddrUpdater()
}
if err != nil {
r.log.Errorf("Failed at redisclient startup %v", err)
ro.Log.Errorf("Failed at redisclient startup %v", err)
}
ringOptions.Addrs = createAddressMap(address)
} else {
ringOptions.Addrs = createAddressMap(ro.Addrs)
}
if ro.Log == nil {
ro.Log = &logging.DefaultLog{}
}
ro.Log.Infof("create ring with addresses: %v", ro.Addrs)

ro.Log.Infof("Created ring with addresses: %v", ro.Addrs)

ringOptions.ReadTimeout = ro.ReadTimeout
ringOptions.WriteTimeout = ro.WriteTimeout
ringOptions.PoolTimeout = ro.PoolTimeout
Expand Down
15 changes: 15 additions & 0 deletions net/redisclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net

import (
"context"
"fmt"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -1084,3 +1085,17 @@ func TestRedisClientSetAddr(t *testing.T) {
})
}
}

func TestRedisClientFailingAddrUpdater(t *testing.T) {
cli := NewRedisRingClient(&RedisOptions{
AddrUpdater: func() ([]string, error) {
return nil, fmt.Errorf("failed to get addresses")
},
UpdateInterval: 1 * time.Second,
})
defer cli.Close()

if cli.RingAvailable() {
t.Error("Unexpected available ring")
}
}

0 comments on commit da3d600

Please sign in to comment.