Skip to content

Commit

Permalink
Support for CLIENT SETINFO (#2659)
Browse files Browse the repository at this point in the history
* feat: merge master

* feat: revert ring.go

* feat: add ClientSetInfo command

* fix: test and cmd:

* fix: test and cmd

* fix: test and cmd

* fix: test and cmd

* feat: redesigning the API

* fix: panic test

* fix: panic test

---------

Co-authored-by: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com>
Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 14, 2023
1 parent 017466b commit 3b0d10b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
6 changes: 6 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5292,3 +5292,9 @@ func (cmd *ACLLogCmd) readReply(rd *proto.Reader) error {

return nil
}

// LibraryInfo holds the library info.
type LibraryInfo struct {
LibName *string
LibVer *string
}
30 changes: 30 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ type StatefulCmdable interface {
Select(ctx context.Context, index int) *StatusCmd
SwapDB(ctx context.Context, index1, index2 int) *StatusCmd
ClientSetName(ctx context.Context, name string) *BoolCmd
ClientSetInfo(ctx context.Context, info LibraryInfo) *StatusCmd
Hello(ctx context.Context, ver int, username, password, clientName string) *MapStringInterfaceCmd
}

Expand Down Expand Up @@ -574,6 +575,35 @@ func (c statefulCmdable) ClientSetName(ctx context.Context, name string) *BoolCm
return cmd
}

// ClientSetInfo sends a CLIENT SETINFO command with the provided info.
func (c statefulCmdable) ClientSetInfo(ctx context.Context, info LibraryInfo) *StatusCmd {
err := info.Validate()
if err != nil {
panic(err.Error())
}

var cmd *StatusCmd
if info.LibName != nil {
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-NAME", *info.LibName)
} else {
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-VER", *info.LibVer)
}

_ = c(ctx, cmd)
return cmd
}

// Validate checks if only one field in the struct is non-nil.
func (info LibraryInfo) Validate() error {
if info.LibName != nil && info.LibVer != nil {
return errors.New("both LibName and LibVer cannot be set at the same time")
}
if info.LibName == nil && info.LibVer == nil {
return errors.New("at least one of LibName and LibVer should be set")
}
return nil
}

// Hello Set the resp protocol used.
func (c statefulCmdable) Hello(ctx context.Context,
ver int, username, password, clientName string) *MapStringInterfaceCmd {
Expand Down
51 changes: 51 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,57 @@ var _ = Describe("Commands", func() {
Expect(get.Val()).To(Equal("theclientname"))
})

It("should ClientSetInfo", func() {

pipe := client.Pipeline()

// Test setting the libName
libName := "go-redis"
libInfo := redis.LibraryInfo{LibName: &libName}
setInfo := pipe.ClientSetInfo(ctx, libInfo)
_, err := pipe.Exec(ctx)

Expect(err).NotTo(HaveOccurred())
Expect(setInfo.Err()).NotTo(HaveOccurred())
Expect(setInfo.Val()).To(Equal("OK"))

// Test setting the libVer
libVer := "vX.x"
libInfo = redis.LibraryInfo{LibVer: &libVer}
setInfo = pipe.ClientSetInfo(ctx, libInfo)
_, err = pipe.Exec(ctx)

Expect(err).NotTo(HaveOccurred())
Expect(setInfo.Err()).NotTo(HaveOccurred())
Expect(setInfo.Val()).To(Equal("OK"))

// Test setting both fields, expect a panic
libInfo = redis.LibraryInfo{LibName: &libName, LibVer: &libVer}

Expect(func() {
defer func() {
if r := recover(); r != nil {
err := r.(error)
Expect(err).To(MatchError("both LibName and LibVer cannot be set at the same time"))
}
}()
pipe.ClientSetInfo(ctx, libInfo)
}).To(Panic())

// Test setting neither field, expect a panic
libInfo = redis.LibraryInfo{}

Expect(func() {
defer func() {
if r := recover(); r != nil {
err := r.(error)
Expect(err).To(MatchError("at least one of LibName and LibVer should be set"))
}
}()
pipe.ClientSetInfo(ctx, libInfo)
}).To(Panic())
})

It("should ConfigGet", func() {
val, err := client.ConfigGet(ctx, "*").Result()
Expect(err).NotTo(HaveOccurred())
Expand Down

1 comment on commit 3b0d10b

@peczenyj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello

today we can read the connection name from url and query string and we try to set it

perhaps we can do the same with lib name and version

perhaps this dev is pretty simple, I can try to offer one pull request

Please sign in to comment.