Skip to content

Commit c7bc54b

Browse files
authored
Merge pull request #2333 from monkey92t/fix_2312
feat: add ClientName option
2 parents a4336cb + a872c35 commit c7bc54b

11 files changed

+121
-15
lines changed

cluster.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ type ClusterOptions struct {
2929
// A seed list of host:port addresses of cluster nodes.
3030
Addrs []string
3131

32+
// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
33+
ClientName string
34+
3235
// NewClient creates a cluster node client with provided name and options.
3336
NewClient func(opt *Options) *Client
3437

@@ -208,6 +211,7 @@ func setupClusterConn(u *url.URL, host string, o *ClusterOptions) (*ClusterOptio
208211
func setupClusterQueryParams(u *url.URL, o *ClusterOptions) (*ClusterOptions, error) {
209212
q := queryOptions{q: u.Query()}
210213

214+
o.ClientName = q.string("client_name")
211215
o.MaxRedirects = q.int("max_redirects")
212216
o.ReadOnly = q.bool("read_only")
213217
o.RouteByLatency = q.bool("route_by_latency")
@@ -250,8 +254,9 @@ func setupClusterQueryParams(u *url.URL, o *ClusterOptions) (*ClusterOptions, er
250254

251255
func (opt *ClusterOptions) clientOptions() *Options {
252256
return &Options{
253-
Dialer: opt.Dialer,
254-
OnConnect: opt.OnConnect,
257+
ClientName: opt.ClientName,
258+
Dialer: opt.Dialer,
259+
OnConnect: opt.OnConnect,
255260

256261
Username: opt.Username,
257262
Password: opt.Password,
@@ -871,7 +876,7 @@ func (c *ClusterClient) Close() error {
871876
return c.nodes.Close()
872877
}
873878

874-
// Do creates a Cmd from the args and processes the cmd.
879+
// Do create a Cmd from the args and processes the cmd.
875880
func (c *ClusterClient) Do(ctx context.Context, args ...interface{}) *Cmd {
876881
cmd := NewCmd(ctx, args...)
877882
_ = c.Process(ctx, cmd)

cluster_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ var _ = Describe("ClusterClient", func() {
589589
Describe("ClusterClient", func() {
590590
BeforeEach(func() {
591591
opt = redisClusterOptions()
592+
opt.ClientName = "cluster_hi"
592593
client = cluster.newClusterClient(ctx, opt)
593594

594595
err := client.ForEachMaster(ctx, func(ctx context.Context, master *redis.Client) error {
@@ -679,6 +680,20 @@ var _ = Describe("ClusterClient", func() {
679680
Expect(assertSlotsEqual(res, wanted)).NotTo(HaveOccurred())
680681
})
681682

683+
It("should cluster client setname", func() {
684+
err := client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
685+
return c.Ping(ctx).Err()
686+
})
687+
Expect(err).NotTo(HaveOccurred())
688+
689+
_ = client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
690+
val, err := c.ClientList(ctx).Result()
691+
Expect(err).NotTo(HaveOccurred())
692+
Expect(val).Should(ContainSubstring("name=cluster_hi"))
693+
return nil
694+
})
695+
})
696+
682697
It("should CLUSTER NODES", func() {
683698
res, err := client.ClusterNodes(ctx).Result()
684699
Expect(err).NotTo(HaveOccurred())
@@ -1408,6 +1423,10 @@ func TestParseClusterURL(t *testing.T) {
14081423
test: "UseDefault",
14091424
url: "redis://localhost:123?conn_max_idle_time=",
14101425
o: &redis.ClusterOptions{Addrs: []string{"localhost:123"}, ConnMaxIdleTime: 0},
1426+
}, {
1427+
test: "ClientName",
1428+
url: "redis://localhost:123?client_name=cluster_hi",
1429+
o: &redis.ClusterOptions{Addrs: []string{"localhost:123"}, ClientName: "cluster_hi"},
14111430
}, {
14121431
test: "UseDefaultMissing=",
14131432
url: "redis://localhost:123?conn_max_idle_time",

options.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ type Limiter interface {
2727
ReportResult(result error)
2828
}
2929

30-
// Options keeps the settings to setup redis connection.
30+
// Options keeps the settings to set up redis connection.
3131
type Options struct {
3232
// The network type, either tcp or unix.
3333
// Default is tcp.
3434
Network string
3535
// host:port address.
3636
Addr string
3737

38+
// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
39+
ClientName string
40+
3841
// Dialer creates new network connection and has priority over
3942
// Network and Addr options.
4043
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
@@ -426,6 +429,7 @@ func setupConnParams(u *url.URL, o *Options) (*Options, error) {
426429
o.DB = db
427430
}
428431

432+
o.ClientName = q.string("client_name")
429433
o.MaxRetries = q.int("max_retries")
430434
o.MinRetryBackoff = q.duration("min_retry_backoff")
431435
o.MaxRetryBackoff = q.duration("max_retry_backoff")

options_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func TestParseURL(t *testing.T) {
5959
}, {
6060
url: "redis://localhost:123/?db=2&conn_max_idle_time", // missing "=" at the end
6161
o: &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: 0},
62+
}, {
63+
url: "redis://localhost:123/?db=2&client_name=hi", // client name
64+
o: &Options{Addr: "localhost:123", DB: 2, ClientName: "hi"},
6265
}, {
6366
url: "unix:///tmp/redis.sock",
6467
o: &Options{Addr: "/tmp/redis.sock"},

redis.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
260260
pipe.ReadOnly(ctx)
261261
}
262262

263+
if c.opt.ClientName != "" {
264+
pipe.ClientSetName(ctx, c.opt.ClientName)
265+
}
266+
263267
return nil
264268
})
265269
if err != nil {

redis_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ var _ = Describe("Client", func() {
169169
Expect(db2.Close()).NotTo(HaveOccurred())
170170
})
171171

172+
It("should client setname", func() {
173+
opt := redisOptions()
174+
opt.ClientName = "hi"
175+
db := redis.NewClient(opt)
176+
177+
defer func() {
178+
Expect(db.Close()).NotTo(HaveOccurred())
179+
}()
180+
181+
Expect(db.Ping(ctx).Err()).NotTo(HaveOccurred())
182+
val, err := db.ClientList(ctx).Result()
183+
Expect(err).NotTo(HaveOccurred())
184+
Expect(val).Should(ContainSubstring("name=hi"))
185+
})
186+
172187
It("processes custom commands", func() {
173188
cmd := redis.NewCmd(ctx, "PING")
174189
_ = client.Process(ctx, cmd)

ring.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type RingOptions struct {
5151
// NewClient creates a shard client with provided options.
5252
NewClient func(opt *Options) *Client
5353

54+
// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
55+
ClientName string
56+
5457
// Frequency of PING commands sent to check shards availability.
5558
// Shard is considered down after 3 subsequent failed checks.
5659
HeartbeatFrequency time.Duration
@@ -129,8 +132,9 @@ func (opt *RingOptions) init() {
129132

130133
func (opt *RingOptions) clientOptions() *Options {
131134
return &Options{
132-
Dialer: opt.Dialer,
133-
OnConnect: opt.OnConnect,
135+
ClientName: opt.ClientName,
136+
Dialer: opt.Dialer,
137+
OnConnect: opt.OnConnect,
134138

135139
Username: opt.Username,
136140
Password: opt.Password,
@@ -522,7 +526,7 @@ func (c *Ring) SetAddrs(addrs map[string]string) {
522526
c.sharding.SetAddrs(addrs)
523527
}
524528

525-
// Do creates a Cmd from the args and processes the cmd.
529+
// Do create a Cmd from the args and processes the cmd.
526530
func (c *Ring) Do(ctx context.Context, args ...interface{}) *Cmd {
527531
cmd := NewCmd(ctx, args...)
528532
_ = c.Process(ctx, cmd)

ring_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var _ = Describe("Redis Ring", func() {
2929

3030
BeforeEach(func() {
3131
opt := redisRingOptions()
32+
opt.ClientName = "ring_hi"
3233
opt.HeartbeatFrequency = heartbeat
3334
ring = redis.NewRing(opt)
3435

@@ -50,6 +51,20 @@ var _ = Describe("Redis Ring", func() {
5051
Expect(err).To(MatchError("context canceled"))
5152
})
5253

54+
It("should ring client setname", func() {
55+
err := ring.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
56+
return c.Ping(ctx).Err()
57+
})
58+
Expect(err).NotTo(HaveOccurred())
59+
60+
_ = ring.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
61+
val, err := c.ClientList(ctx).Result()
62+
Expect(err).NotTo(HaveOccurred())
63+
Expect(val).Should(ContainSubstring("name=ring_hi"))
64+
return nil
65+
})
66+
})
67+
5368
It("distributes keys", func() {
5469
setRingKeys()
5570

sentinel.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type FailoverOptions struct {
2424
// A seed list of host:port addresses of sentinel nodes.
2525
SentinelAddrs []string
2626

27+
// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
28+
ClientName string
29+
2730
// If specified with SentinelPassword, enables ACL-based authentication (via
2831
// AUTH <user> <pass>).
2932
SentinelUsername string
@@ -78,7 +81,8 @@ type FailoverOptions struct {
7881

7982
func (opt *FailoverOptions) clientOptions() *Options {
8083
return &Options{
81-
Addr: "FailoverClient",
84+
Addr: "FailoverClient",
85+
ClientName: opt.ClientName,
8286

8387
Dialer: opt.Dialer,
8488
OnConnect: opt.OnConnect,
@@ -110,7 +114,8 @@ func (opt *FailoverOptions) clientOptions() *Options {
110114

111115
func (opt *FailoverOptions) sentinelOptions(addr string) *Options {
112116
return &Options{
113-
Addr: addr,
117+
Addr: addr,
118+
ClientName: opt.ClientName,
114119

115120
Dialer: opt.Dialer,
116121
OnConnect: opt.OnConnect,
@@ -141,6 +146,8 @@ func (opt *FailoverOptions) sentinelOptions(addr string) *Options {
141146

142147
func (opt *FailoverOptions) clusterOptions() *ClusterOptions {
143148
return &ClusterOptions{
149+
ClientName: opt.ClientName,
150+
144151
Dialer: opt.Dialer,
145152
OnConnect: opt.OnConnect,
146153

sentinel_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis_test
22

33
import (
4+
"context"
45
"net"
56

67
. "github.com/onsi/ginkgo"
@@ -17,6 +18,7 @@ var _ = Describe("Sentinel", func() {
1718

1819
BeforeEach(func() {
1920
client = redis.NewFailoverClient(&redis.FailoverOptions{
21+
ClientName: "sentinel_hi",
2022
MasterName: sentinelName,
2123
SentinelAddrs: sentinelAddrs,
2224
MaxRetries: -1,
@@ -125,6 +127,13 @@ var _ = Describe("Sentinel", func() {
125127
err := client.Ping(ctx).Err()
126128
Expect(err).NotTo(HaveOccurred())
127129
})
130+
131+
It("should sentinel client setname", func() {
132+
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
133+
val, err := client.ClientList(ctx).Result()
134+
Expect(err).NotTo(HaveOccurred())
135+
Expect(val).Should(ContainSubstring("name=sentinel_hi"))
136+
})
128137
})
129138

130139
var _ = Describe("NewFailoverClusterClient", func() {
@@ -134,6 +143,7 @@ var _ = Describe("NewFailoverClusterClient", func() {
134143

135144
BeforeEach(func() {
136145
client = redis.NewFailoverClusterClient(&redis.FailoverOptions{
146+
ClientName: "sentinel_cluster_hi",
137147
MasterName: sentinelName,
138148
SentinelAddrs: sentinelAddrs,
139149

@@ -213,6 +223,20 @@ var _ = Describe("NewFailoverClusterClient", func() {
213223
_, err = startRedis(masterPort)
214224
Expect(err).NotTo(HaveOccurred())
215225
})
226+
227+
It("should sentinel cluster client setname", func() {
228+
err := client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
229+
return c.Ping(ctx).Err()
230+
})
231+
Expect(err).NotTo(HaveOccurred())
232+
233+
_ = client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
234+
val, err := c.ClientList(ctx).Result()
235+
Expect(err).NotTo(HaveOccurred())
236+
Expect(val).Should(ContainSubstring("name=sentinel_cluster_hi"))
237+
return nil
238+
})
239+
})
216240
})
217241

218242
var _ = Describe("SentinelAclAuth", func() {

universal.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type UniversalOptions struct {
1414
// of cluster/sentinel nodes.
1515
Addrs []string
1616

17+
// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
18+
ClientName string
19+
1720
// Database to be selected after connecting to the server.
1821
// Only single-node and failover clients.
1922
DB int
@@ -69,9 +72,10 @@ func (o *UniversalOptions) Cluster() *ClusterOptions {
6972
}
7073

7174
return &ClusterOptions{
72-
Addrs: o.Addrs,
73-
Dialer: o.Dialer,
74-
OnConnect: o.OnConnect,
75+
Addrs: o.Addrs,
76+
ClientName: o.ClientName,
77+
Dialer: o.Dialer,
78+
OnConnect: o.OnConnect,
7579

7680
Username: o.Username,
7781
Password: o.Password,
@@ -112,6 +116,7 @@ func (o *UniversalOptions) Failover() *FailoverOptions {
112116
return &FailoverOptions{
113117
SentinelAddrs: o.Addrs,
114118
MasterName: o.MasterName,
119+
ClientName: o.ClientName,
115120

116121
Dialer: o.Dialer,
117122
OnConnect: o.OnConnect,
@@ -151,9 +156,10 @@ func (o *UniversalOptions) Simple() *Options {
151156
}
152157

153158
return &Options{
154-
Addr: addr,
155-
Dialer: o.Dialer,
156-
OnConnect: o.OnConnect,
159+
Addr: addr,
160+
ClientName: o.ClientName,
161+
Dialer: o.Dialer,
162+
OnConnect: o.OnConnect,
157163

158164
DB: o.DB,
159165
Username: o.Username,

0 commit comments

Comments
 (0)