From 571a163721638e429b8e2498c637d36f9e6b2ed3 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 21 Sep 2023 09:45:30 +0200 Subject: [PATCH 1/9] add helpers to set library name and library info without risk of panic if we try to set both --- command.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/command.go b/command.go index 00e356bbd..bcdd6aebd 100644 --- a/command.go +++ b/command.go @@ -5298,3 +5298,13 @@ type LibraryInfo struct { LibName *string 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} +} From 5004750031854caad018bdeb35b82e5d9be6dfaa Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 21 Sep 2023 09:45:55 +0200 Subject: [PATCH 2/9] refactor code to use helpers --- redis.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/redis.go b/redis.go index 9430eb75c..7e4458107 100644 --- a/redis.go +++ b/redis.go @@ -302,10 +302,8 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error { if !c.opt.DisableIndentity { libName := "" libVer := Version() - 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 != "" { From 338096c1dc5095e034937eba6652eeeea903050b Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 21 Sep 2023 09:46:03 +0200 Subject: [PATCH 3/9] add example --- example_test.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/example_test.go b/example_test.go index 84f0ba91e..39dc68d82 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,35 @@ func ExampleConn() { // Output: foobar } +func ExampleConn_info() { + conn := rdb.Conn() + + err := conn.ClientSetInfo(ctx, redis.WithLibraryName("lib-name")).Err() + if err != nil { + panic(err) + } + + err = conn.ClientSetInfo(ctx, redis.WithLibraryVersion("1.0.0")).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("library name:", s.LibName) + fmt.Println("library version:", s.LibVer) + // Output: + // library name: go-redis(lib-name,go1.21.1) + // library version: 1.0.0 +} + func ExampleClient_Set() { // Last argument is expiration. Zero means the key has no // expiration time. From 15bdfcc1295df7905582b14a6acd099b4d0880d9 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 21 Sep 2023 09:46:10 +0200 Subject: [PATCH 4/9] refactor tests --- commands_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands_test.go b/commands_test.go index 8e00c8368..d0674ba03 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) From f4a0cdc93f57f92cc849103a95b38fe43c7c5bb2 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 21 Sep 2023 10:32:20 +0200 Subject: [PATCH 5/9] fix testable example --- example_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/example_test.go b/example_test.go index 39dc68d82..0aac0e33a 100644 --- a/example_test.go +++ b/example_test.go @@ -197,10 +197,11 @@ func ExampleConn_info() { if err != nil { panic(err) } - fmt.Println("library name:", s.LibName) + // under go1.21.1 will return library name: go-redis(lib-name,go1.21.1) + // fmt.Println("library name:", s.LibName) + fmt.Println("library version:", s.LibVer) // Output: - // library name: go-redis(lib-name,go1.21.1) // library version: 1.0.0 } From ba04bb19396888fcb4a0842fc406e141fd6916a9 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 21 Sep 2023 10:33:21 +0200 Subject: [PATCH 6/9] simplify example --- example_test.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/example_test.go b/example_test.go index 0aac0e33a..c0b41a5b5 100644 --- a/example_test.go +++ b/example_test.go @@ -178,12 +178,7 @@ func ExampleConn_name() { func ExampleConn_info() { conn := rdb.Conn() - err := conn.ClientSetInfo(ctx, redis.WithLibraryName("lib-name")).Err() - if err != nil { - panic(err) - } - - err = conn.ClientSetInfo(ctx, redis.WithLibraryVersion("1.0.0")).Err() + err := conn.ClientSetInfo(ctx, redis.WithLibraryVersion("1.0.0")).Err() if err != nil { panic(err) } @@ -197,12 +192,9 @@ func ExampleConn_info() { if err != nil { panic(err) } - // under go1.21.1 will return library name: go-redis(lib-name,go1.21.1) - // fmt.Println("library name:", s.LibName) - fmt.Println("library version:", s.LibVer) - // Output: - // library version: 1.0.0 + fmt.Println(s.LibVer) + // Output: 1.0.0 } func ExampleClient_Set() { From 716931e6cbe2bb32a42ef875cb29abcfd2beb576 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 21 Sep 2023 10:34:40 +0200 Subject: [PATCH 7/9] rename exampl --- example_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_test.go b/example_test.go index c0b41a5b5..2df74c260 100644 --- a/example_test.go +++ b/example_test.go @@ -175,7 +175,7 @@ func ExampleConn_name() { // Output: foobar } -func ExampleConn_info() { +func ExampleConn_client_setInfo_libraryVersion() { conn := rdb.Conn() err := conn.ClientSetInfo(ctx, redis.WithLibraryVersion("1.0.0")).Err() From d6db4b45576e65ec3da1f4f146394a2525906023 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Mon, 30 Oct 2023 14:21:13 +0100 Subject: [PATCH 8/9] fix ring.go --- ring.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ring.go b/ring.go index d39856bce..367a542e9 100644 --- a/ring.go +++ b/ring.go @@ -84,8 +84,6 @@ type RingOptions struct { WriteTimeout time.Duration ContextTimeoutEnabled bool - ContextTimeoutEnabled bool - // PoolFIFO uses FIFO mode for each node connection pool GET/PUT (default LIFO). PoolFIFO bool @@ -155,8 +153,6 @@ func (opt *RingOptions) clientOptions() *Options { WriteTimeout: opt.WriteTimeout, ContextTimeoutEnabled: opt.ContextTimeoutEnabled, - ContextTimeoutEnabled: opt.ContextTimeoutEnabled, - PoolFIFO: opt.PoolFIFO, PoolSize: opt.PoolSize, PoolTimeout: opt.PoolTimeout, From 38f11a8ff9b5494b373dda45994fe0208fa058f8 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 21 Dec 2023 13:48:58 +0100 Subject: [PATCH 9/9] update example --- example_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_test.go b/example_test.go index 2df74c260..2c91f100c 100644 --- a/example_test.go +++ b/example_test.go @@ -178,7 +178,7 @@ func ExampleConn_name() { func ExampleConn_client_setInfo_libraryVersion() { conn := rdb.Conn() - err := conn.ClientSetInfo(ctx, redis.WithLibraryVersion("1.0.0")).Err() + err := conn.ClientSetInfo(ctx, redis.WithLibraryVersion("1.2.3")).Err() if err != nil { panic(err) } @@ -194,7 +194,7 @@ func ExampleConn_client_setInfo_libraryVersion() { } fmt.Println(s.LibVer) - // Output: 1.0.0 + // Output: 1.2.3 } func ExampleClient_Set() {