From da3d60089507fdf3ac37459f76dbcb50f3fec61b Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Mon, 19 Feb 2024 20:33:56 +0100 Subject: [PATCH] net/redisclient: fix nil pointer dereference (#2939) Fix use of logger before initialization. Followup on #2261 Signed-off-by: Alexander Yastrebov --- net/redisclient.go | 13 ++++++++----- net/redisclient_test.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/net/redisclient.go b/net/redisclient.go index 14f6193145..5a117ad139 100644 --- a/net/redisclient.go +++ b/net/redisclient.go @@ -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++ { @@ -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 diff --git a/net/redisclient_test.go b/net/redisclient_test.go index a4d25e1cb6..24049c538e 100644 --- a/net/redisclient_test.go +++ b/net/redisclient_test.go @@ -2,6 +2,7 @@ package net import ( "context" + "fmt" "sync" "testing" "time" @@ -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") + } +}