Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpers to set libinfo without panic #2724

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.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(s.LibVer)
// Output: 1.0.0
}

func ExampleClient_Set() {
// Last argument is expiration. Zero means the key has no
// expiration time.
Expand Down
6 changes: 2 additions & 4 deletions redis.go
Expand Up @@ -315,10 +315,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 != "" {
Expand Down