Skip to content

Commit 42d730c

Browse files
authored
Add support for CLUSTER LINKS command (#2504)
* feat: Adding support for Cluster Links Command Co-authored-by: Anuragkillswitch <[email protected]>
1 parent f30d7c4 commit 42d730c

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

cluster_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,30 @@ var _ = Describe("ClusterClient", func() {
677677
Expect(assertSlotsEqual(res, wanted)).NotTo(HaveOccurred())
678678
})
679679

680+
It("should CLUSTER LINKS", func() {
681+
res, err := client.ClusterLinks(ctx).Result()
682+
Expect(err).NotTo(HaveOccurred())
683+
Expect(res).NotTo(BeEmpty())
684+
685+
// Iterate over the ClusterLink results and validate the map keys.
686+
for _, link := range res {
687+
688+
Expect(link.Direction).NotTo(BeEmpty())
689+
Expect([]string{"from", "to"}).To(ContainElement(link.Direction))
690+
Expect(link.Node).NotTo(BeEmpty())
691+
Expect(link.CreateTime).To(BeNumerically(">", 0))
692+
693+
Expect(link.Events).NotTo(BeEmpty())
694+
validEventChars := []rune{'r', 'w'}
695+
for _, eventChar := range link.Events {
696+
Expect(validEventChars).To(ContainElement(eventChar))
697+
}
698+
699+
Expect(link.SendBufferAllocated).To(BeNumerically(">=", 0))
700+
Expect(link.SendBufferUsed).To(BeNumerically(">=", 0))
701+
}
702+
})
703+
680704
It("should cluster client setname", func() {
681705
err := client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
682706
return c.Ping(ctx).Err()

command.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,3 +4246,92 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error {
42464246

42474247
return nil
42484248
}
4249+
4250+
// ---------------------------------------------------------------------------------------------------
4251+
4252+
type ClusterLink struct {
4253+
Direction string
4254+
Node string
4255+
CreateTime int64
4256+
Events string
4257+
SendBufferAllocated int64
4258+
SendBufferUsed int64
4259+
}
4260+
4261+
type ClusterLinksCmd struct {
4262+
baseCmd
4263+
4264+
val []ClusterLink
4265+
}
4266+
4267+
var _ Cmder = (*ClusterLinksCmd)(nil)
4268+
4269+
func NewClusterLinksCmd(ctx context.Context, args ...interface{}) *ClusterLinksCmd {
4270+
return &ClusterLinksCmd{
4271+
baseCmd: baseCmd{
4272+
ctx: ctx,
4273+
args: args,
4274+
},
4275+
}
4276+
}
4277+
4278+
func (cmd *ClusterLinksCmd) SetVal(val []ClusterLink) {
4279+
cmd.val = val
4280+
}
4281+
4282+
func (cmd *ClusterLinksCmd) Val() []ClusterLink {
4283+
return cmd.val
4284+
}
4285+
4286+
func (cmd *ClusterLinksCmd) Result() ([]ClusterLink, error) {
4287+
return cmd.Val(), cmd.Err()
4288+
}
4289+
4290+
func (cmd *ClusterLinksCmd) String() string {
4291+
return cmdString(cmd, cmd.val)
4292+
}
4293+
4294+
func (cmd *ClusterLinksCmd) readReply(rd *proto.Reader) error {
4295+
n, err := rd.ReadArrayLen()
4296+
if err != nil {
4297+
return err
4298+
}
4299+
cmd.val = make([]ClusterLink, n)
4300+
4301+
for i := 0; i < len(cmd.val); i++ {
4302+
m, err := rd.ReadMapLen()
4303+
if err != nil {
4304+
return err
4305+
}
4306+
4307+
for j := 0; j < m; j++ {
4308+
key, err := rd.ReadString()
4309+
if err != nil {
4310+
return err
4311+
}
4312+
4313+
switch key {
4314+
case "direction":
4315+
cmd.val[i].Direction, err = rd.ReadString()
4316+
case "node":
4317+
cmd.val[i].Node, err = rd.ReadString()
4318+
case "create-time":
4319+
cmd.val[i].CreateTime, err = rd.ReadInt()
4320+
case "events":
4321+
cmd.val[i].Events, err = rd.ReadString()
4322+
case "send-buffer-allocated":
4323+
cmd.val[i].SendBufferAllocated, err = rd.ReadInt()
4324+
case "send-buffer-used":
4325+
cmd.val[i].SendBufferUsed, err = rd.ReadInt()
4326+
default:
4327+
return fmt.Errorf("redis: unexpected key %q in CLUSTER LINKS reply", key)
4328+
}
4329+
4330+
if err != nil {
4331+
return err
4332+
}
4333+
}
4334+
}
4335+
4336+
return nil
4337+
}

commands.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ type Cmdable interface {
422422
PubSubShardNumSub(ctx context.Context, channels ...string) *MapStringIntCmd
423423

424424
ClusterSlots(ctx context.Context) *ClusterSlotsCmd
425+
ClusterLinks(ctx context.Context) *ClusterLinksCmd
425426
ClusterNodes(ctx context.Context) *StringCmd
426427
ClusterMeet(ctx context.Context, host, port string) *StatusCmd
427428
ClusterForget(ctx context.Context, nodeID string) *StatusCmd
@@ -3497,6 +3498,12 @@ func (c cmdable) ClusterSlots(ctx context.Context) *ClusterSlotsCmd {
34973498
return cmd
34983499
}
34993500

3501+
func (c cmdable) ClusterLinks(ctx context.Context) *ClusterLinksCmd {
3502+
cmd := NewClusterLinksCmd(ctx, "cluster", "links")
3503+
_ = c(ctx, cmd)
3504+
return cmd
3505+
}
3506+
35003507
func (c cmdable) ClusterNodes(ctx context.Context) *StringCmd {
35013508
cmd := NewStringCmd(ctx, "cluster", "nodes")
35023509
_ = c(ctx, cmd)

0 commit comments

Comments
 (0)