feat: disables sending HELLO if protocol is -1

This commit is contained in:
ljun20160606 2023-06-28 10:34:43 +08:00
parent f994ff1cd9
commit 646d99e12a
2 changed files with 22 additions and 18 deletions

View File

@ -46,7 +46,8 @@ type Options struct {
OnConnect func(ctx context.Context, cn *Conn) error OnConnect func(ctx context.Context, cn *Conn) error
// Protocol 2 or 3. Use the version to negotiate RESP version with redis-server. // Protocol 2 or 3. Use the version to negotiate RESP version with redis-server.
// Default is 3. // Default is 3; -1 disables sending HELLO means that the version depends on the default
// settings of redis-server.
Protocol int Protocol int
// Use the specified Username to authenticate the current connection // Use the specified Username to authenticate the current connection
// with one of the connections defined in the ACL list when connecting // with one of the connections defined in the ACL list when connecting

View File

@ -280,24 +280,27 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
var auth bool var auth bool
protocol := c.opt.Protocol protocol := c.opt.Protocol
// By default, use RESP3 in current version. // disables sending HELLO if protocol is lower than 0.
if protocol < 2 { if protocol >= 0 {
protocol = 3 // 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, "").Err(); err == nil { if err := conn.Hello(ctx, protocol, username, password, "").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
// execution result of the HELLO command, we consider it to be an indication that // execution result of the HELLO command, we consider it to be an indication that
// the server does not support the HELLO command. // the server does not support the HELLO command.
// The server may be a redis-server that does not support the HELLO command, // The server may be a redis-server that does not support the HELLO command,
// or it could be DragonflyDB or a third-party redis-proxy. They all respond // or it could be DragonflyDB or a third-party redis-proxy. They all respond
// with different error string results for unsupported commands, making it // with different error string results for unsupported commands, making it
// difficult to rely on error strings to determine all results. // difficult to rely on error strings to determine all results.
return err return err
}
} }
if !c.opt.DisableIndentity { if !c.opt.DisableIndentity {
libName := "" libName := ""