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>
This commit is contained in:
Tiago Peczenyj 2024-02-14 21:40:20 +01:00 committed by GitHub
parent 7b9e81fd41
commit 21ed15bbed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 7 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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.

View File

@ -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 != "" {