Skip to content

Commit

Permalink
Add helpers to set libinfo without panic (#2724)
Browse files Browse the repository at this point in the history
* add helpers to set library name and library info without risk of panic if we try to set both

* refactor code to use helpers

* add example

* refactor tests

* fix testable example

* simplify example

* rename exampl

* fix ring.go

* update example

---------

Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
  • Loading branch information
peczenyj and ofekshenawa committed Feb 14, 2024
1 parent 7b9e81f commit 21ed15b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
10 changes: 10 additions & 0 deletions command.go
Expand Up @@ -5310,6 +5310,16 @@ type LibraryInfo struct {
LibVer *string
}

// WithLibraryName returns a valid LibraryInfo with library name only.
func WithLibraryName(libName string) LibraryInfo {
return LibraryInfo{LibName: &libName}
}

// WithLibraryVersion returns a valid LibraryInfo with library version only.
func WithLibraryVersion(libVer string) LibraryInfo {
return LibraryInfo{LibVer: &libVer}
}

// -------------------------------------------

type InfoCmd struct {
Expand Down
4 changes: 2 additions & 2 deletions commands_test.go
Expand Up @@ -248,7 +248,7 @@ var _ = Describe("Commands", func() {

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

Expand All @@ -258,7 +258,7 @@ var _ = Describe("Commands", func() {

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

Expand Down
24 changes: 23 additions & 1 deletion example_test.go
Expand Up @@ -154,7 +154,7 @@ func ExampleClient() {
// missing_key does not exist
}

func ExampleConn() {
func ExampleConn_name() {
conn := rdb.Conn()

err := conn.ClientSetName(ctx, "foobar").Err()
Expand All @@ -175,6 +175,28 @@ func ExampleConn() {
// Output: foobar
}

func ExampleConn_client_setInfo_libraryVersion() {
conn := rdb.Conn()

err := conn.ClientSetInfo(ctx, redis.WithLibraryVersion("1.2.3")).Err()
if err != nil {
panic(err)
}

// Open other connections.
for i := 0; i < 10; i++ {
go rdb.Ping(ctx)
}

s, err := conn.ClientInfo(ctx).Result()
if err != nil {
panic(err)
}

fmt.Println(s.LibVer)
// Output: 1.2.3
}

func ExampleClient_Set() {
// Last argument is expiration. Zero means the key has no
// expiration time.
Expand Down
7 changes: 3 additions & 4 deletions redis.go
Expand Up @@ -315,13 +315,12 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
if !c.opt.DisableIndentity {
libName := ""
libVer := Version()

if c.opt.IdentitySuffix != "" {
libName = c.opt.IdentitySuffix
}
libInfo := LibraryInfo{LibName: &libName}
conn.ClientSetInfo(ctx, libInfo)
libInfo = LibraryInfo{LibVer: &libVer}
conn.ClientSetInfo(ctx, libInfo)
conn.ClientSetInfo(ctx, WithLibraryName(libName))
conn.ClientSetInfo(ctx, WithLibraryVersion(libVer))
}
_, err := conn.Pipelined(ctx, func(pipe Pipeliner) error {
if !auth && password != "" {
Expand Down

0 comments on commit 21ed15b

Please sign in to comment.