Skip to content

Set client name in HELLO handshake during connection initialization #3294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading