Skip to content

Commit 28a3c97

Browse files
authored
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>
1 parent 66b61c4 commit 28a3c97

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ func (opt *Options) init() {
178178
opt.Network = "tcp"
179179
}
180180
}
181+
if opt.Protocol < 2 {
182+
opt.Protocol = 3
183+
}
181184
if opt.DialTimeout == 0 {
182185
opt.DialTimeout = 5 * time.Second
183186
}

options_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,26 @@ func TestReadTimeoutOptions(t *testing.T) {
222222
}
223223
}
224224
}
225+
226+
func TestProtocolOptions(t *testing.T) {
227+
testCasesMap := map[int]int{
228+
0: 3,
229+
1: 3,
230+
2: 2,
231+
3: 3,
232+
}
233+
234+
o := &Options{}
235+
o.init()
236+
if o.Protocol != 3 {
237+
t.Errorf("got %d instead of %d as protocol option", o.Protocol, 3)
238+
}
239+
240+
for set, want := range testCasesMap {
241+
o := &Options{Protocol: set}
242+
o.init()
243+
if o.Protocol != want {
244+
t.Errorf("got %d instead of %d as protocol option", o.Protocol, want)
245+
}
246+
}
247+
}

redis.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,9 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
302302
conn := newConn(c.opt, connPool)
303303

304304
var auth bool
305-
protocol := c.opt.Protocol
306-
// By default, use RESP3 in current version.
307-
if protocol < 2 {
308-
protocol = 3
309-
}
310-
311305
// for redis-server versions that do not support the HELLO command,
312306
// RESP2 will continue to be used.
313-
if err = conn.Hello(ctx, protocol, username, password, c.opt.ClientName).Err(); err == nil {
307+
if err = conn.Hello(ctx, c.opt.Protocol, username, password, c.opt.ClientName).Err(); err == nil {
314308
auth = true
315309
} else if !isRedisError(err) {
316310
// When the server responds with the RESP protocol and the result is not a normal

0 commit comments

Comments
 (0)