From 21ed15bbed2774a40fe831f7618332aa4f207fd1 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Wed, 14 Feb 2024 21:40:20 +0100 Subject: [PATCH] Add helpers to set libinfo without panic (#2724) * 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> --- command.go | 10 ++++++++++ commands_test.go | 4 ++-- example_test.go | 24 +++++++++++++++++++++++- redis.go | 7 +++---- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/command.go b/command.go index 06ed86ed0..9fb9a8310 100644 --- a/command.go +++ b/command.go @@ -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 { diff --git a/commands_test.go b/commands_test.go index 3d2ebf514..fbeff1c20 100644 --- a/commands_test.go +++ b/commands_test.go @@ -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) @@ -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) diff --git a/example_test.go b/example_test.go index 67559fcea..62aa8cb56 100644 --- a/example_test.go +++ b/example_test.go @@ -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() @@ -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. diff --git a/redis.go b/redis.go index 4dd862b84..c8f513119 100644 --- a/redis.go +++ b/redis.go @@ -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 != "" {