From b0141f07c5087103a0572db2a94cdd7367f5ff09 Mon Sep 17 00:00:00 2001 From: fukua95 Date: Fri, 23 May 2025 16:48:51 +0800 Subject: [PATCH 1/2] chore: set the default value for the `options.protocol` in the `init()` of `options` Signed-off-by: fukua95 --- options.go | 3 +++ redis.go | 8 +------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/options.go b/options.go index 3ffcd07ed..bf6c032ec 100644 --- a/options.go +++ b/options.go @@ -178,6 +178,9 @@ func (opt *Options) init() { opt.Network = "tcp" } } + if opt.Protocol < 2 { + opt.Protocol = 3 + } if opt.DialTimeout == 0 { opt.DialTimeout = 5 * time.Second } diff --git a/redis.go b/redis.go index f50df5689..19ff57b01 100644 --- a/redis.go +++ b/redis.go @@ -302,15 +302,9 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error { conn := newConn(c.opt, connPool) var auth bool - protocol := c.opt.Protocol - // By default, use RESP3 in current version. - if protocol < 2 { - protocol = 3 - } - // for redis-server versions that do not support the HELLO command, // RESP2 will continue to be used. - if err = conn.Hello(ctx, protocol, username, password, c.opt.ClientName).Err(); err == nil { + if err = conn.Hello(ctx, c.opt.Protocol, username, password, c.opt.ClientName).Err(); err == nil { auth = true } else if !isRedisError(err) { // When the server responds with the RESP protocol and the result is not a normal From 02b5cadeb99553bcd66894a7ebfc42ad8be74f1d Mon Sep 17 00:00:00 2001 From: fukua95 Date: Fri, 23 May 2025 21:39:37 +0800 Subject: [PATCH 2/2] add a test Signed-off-by: fukua95 --- options_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/options_test.go b/options_test.go index d46ecc858..8de4986b3 100644 --- a/options_test.go +++ b/options_test.go @@ -222,3 +222,26 @@ func TestReadTimeoutOptions(t *testing.T) { } } } + +func TestProtocolOptions(t *testing.T) { + testCasesMap := map[int]int{ + 0: 3, + 1: 3, + 2: 2, + 3: 3, + } + + o := &Options{} + o.init() + if o.Protocol != 3 { + t.Errorf("got %d instead of %d as protocol option", o.Protocol, 3) + } + + for set, want := range testCasesMap { + o := &Options{Protocol: set} + o.init() + if o.Protocol != want { + t.Errorf("got %d instead of %d as protocol option", o.Protocol, want) + } + } +}