Skip to content

Commit 1c9309f

Browse files
authored
Set client name in HELLO RESP handshake (#3294)
1 parent 555a41e commit 1c9309f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

redis.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
310310

311311
// for redis-server versions that do not support the HELLO command,
312312
// RESP2 will continue to be used.
313-
if err = conn.Hello(ctx, protocol, username, password, "").Err(); err == nil {
313+
if err = conn.Hello(ctx, protocol, username, password, c.opt.ClientName).Err(); err == nil {
314314
auth = true
315315
} else if !isRedisError(err) {
316316
// When the server responds with the RESP protocol and the result is not a normal

redis_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,32 @@ var _ = Describe("Client", func() {
186186
Expect(val).Should(ContainSubstring("name=hi"))
187187
})
188188

189+
It("should attempt to set client name in HELLO", func() {
190+
opt := redisOptions()
191+
opt.ClientName = "hi"
192+
db := redis.NewClient(opt)
193+
194+
defer func() {
195+
Expect(db.Close()).NotTo(HaveOccurred())
196+
}()
197+
198+
// Client name should be already set on any successfully initialized connection
199+
name, err := db.ClientGetName(ctx).Result()
200+
Expect(err).NotTo(HaveOccurred())
201+
Expect(name).Should(Equal("hi"))
202+
203+
// HELLO should be able to explicitly overwrite the client name
204+
conn := db.Conn()
205+
hello, err := conn.Hello(ctx, 3, "", "", "hi2").Result()
206+
Expect(err).NotTo(HaveOccurred())
207+
Expect(hello["proto"]).Should(Equal(int64(3)))
208+
name, err = conn.ClientGetName(ctx).Result()
209+
Expect(err).NotTo(HaveOccurred())
210+
Expect(name).Should(Equal("hi2"))
211+
err = conn.Close()
212+
Expect(err).NotTo(HaveOccurred())
213+
})
214+
189215
It("should client PROTO 2", func() {
190216
opt := redisOptions()
191217
opt.Protocol = 2

0 commit comments

Comments
 (0)