diff --git a/redis.go b/redis.go index ec3ff616..533c63c3 100644 --- a/redis.go +++ b/redis.go @@ -310,7 +310,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error { // 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, "").Err(); err == nil { + if err = conn.Hello(ctx, 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 diff --git a/redis_test.go b/redis_test.go index f8c91b4a..27a24c9c 100644 --- a/redis_test.go +++ b/redis_test.go @@ -186,6 +186,32 @@ var _ = Describe("Client", func() { Expect(val).Should(ContainSubstring("name=hi")) }) + It("should attempt to set client name in HELLO", func() { + opt := redisOptions() + opt.ClientName = "hi" + db := redis.NewClient(opt) + + defer func() { + Expect(db.Close()).NotTo(HaveOccurred()) + }() + + // Client name should be already set on any successfully initialized connection + name, err := db.ClientGetName(ctx).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(name).Should(Equal("hi")) + + // HELLO should be able to explicitly overwrite the client name + conn := db.Conn() + hello, err := conn.Hello(ctx, 3, "", "", "hi2").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(hello["proto"]).Should(Equal(int64(3))) + name, err = conn.ClientGetName(ctx).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(name).Should(Equal("hi2")) + err = conn.Close() + Expect(err).NotTo(HaveOccurred()) + }) + It("should client PROTO 2", func() { opt := redisOptions() opt.Protocol = 2