Skip to content

Commit 21ed15b

Browse files
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 <[email protected]>
1 parent 7b9e81f commit 21ed15b

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

command.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5310,6 +5310,16 @@ type LibraryInfo struct {
53105310
LibVer *string
53115311
}
53125312

5313+
// WithLibraryName returns a valid LibraryInfo with library name only.
5314+
func WithLibraryName(libName string) LibraryInfo {
5315+
return LibraryInfo{LibName: &libName}
5316+
}
5317+
5318+
// WithLibraryVersion returns a valid LibraryInfo with library version only.
5319+
func WithLibraryVersion(libVer string) LibraryInfo {
5320+
return LibraryInfo{LibVer: &libVer}
5321+
}
5322+
53135323
// -------------------------------------------
53145324

53155325
type InfoCmd struct {

commands_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ var _ = Describe("Commands", func() {
248248

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

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

259259
// Test setting the libVer
260260
libVer := "vX.x"
261-
libInfo = redis.LibraryInfo{LibVer: &libVer}
261+
libInfo = redis.WithLibraryVersion(libVer)
262262
setInfo = pipe.ClientSetInfo(ctx, libInfo)
263263
_, err = pipe.Exec(ctx)
264264

example_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func ExampleClient() {
154154
// missing_key does not exist
155155
}
156156

157-
func ExampleConn() {
157+
func ExampleConn_name() {
158158
conn := rdb.Conn()
159159

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

178+
func ExampleConn_client_setInfo_libraryVersion() {
179+
conn := rdb.Conn()
180+
181+
err := conn.ClientSetInfo(ctx, redis.WithLibraryVersion("1.2.3")).Err()
182+
if err != nil {
183+
panic(err)
184+
}
185+
186+
// Open other connections.
187+
for i := 0; i < 10; i++ {
188+
go rdb.Ping(ctx)
189+
}
190+
191+
s, err := conn.ClientInfo(ctx).Result()
192+
if err != nil {
193+
panic(err)
194+
}
195+
196+
fmt.Println(s.LibVer)
197+
// Output: 1.2.3
198+
}
199+
178200
func ExampleClient_Set() {
179201
// Last argument is expiration. Zero means the key has no
180202
// expiration time.

redis.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,12 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
315315
if !c.opt.DisableIndentity {
316316
libName := ""
317317
libVer := Version()
318+
318319
if c.opt.IdentitySuffix != "" {
319320
libName = c.opt.IdentitySuffix
320321
}
321-
libInfo := LibraryInfo{LibName: &libName}
322-
conn.ClientSetInfo(ctx, libInfo)
323-
libInfo = LibraryInfo{LibVer: &libVer}
324-
conn.ClientSetInfo(ctx, libInfo)
322+
conn.ClientSetInfo(ctx, WithLibraryName(libName))
323+
conn.ClientSetInfo(ctx, WithLibraryVersion(libVer))
325324
}
326325
_, err := conn.Pipelined(ctx, func(pipe Pipeliner) error {
327326
if !auth && password != "" {

0 commit comments

Comments
 (0)