1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-19 11:43:14 +03:00

chore: set the default value for the options.protocol in the init() of options (#3387)

* chore: set the default value for the `options.protocol` in the `init()` of `options`

Signed-off-by: fukua95 <fukua95@gmail.com>

* add a test

Signed-off-by: fukua95 <fukua95@gmail.com>

---------

Signed-off-by: fukua95 <fukua95@gmail.com>
This commit is contained in:
fukua95
2025-05-27 19:53:41 +08:00
committed by GitHub
parent 66b61c432c
commit 28a3c97409
3 changed files with 27 additions and 7 deletions

View File

@ -178,6 +178,9 @@ func (opt *Options) init() {
opt.Network = "tcp" opt.Network = "tcp"
} }
} }
if opt.Protocol < 2 {
opt.Protocol = 3
}
if opt.DialTimeout == 0 { if opt.DialTimeout == 0 {
opt.DialTimeout = 5 * time.Second opt.DialTimeout = 5 * time.Second
} }

View File

@ -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)
}
}
}

View File

@ -302,15 +302,9 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
conn := newConn(c.opt, connPool) conn := newConn(c.opt, connPool)
var auth bool 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, // for redis-server versions that do not support the HELLO command,
// RESP2 will continue to be used. // 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 auth = true
} else if !isRedisError(err) { } else if !isRedisError(err) {
// When the server responds with the RESP protocol and the result is not a normal // When the server responds with the RESP protocol and the result is not a normal