mirror of
https://github.com/redis/go-redis.git
synced 2025-04-19 07:22:17 +03:00
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:
parent
7b9e81fd41
commit
21ed15bbed
10
command.go
10
command.go
@ -5310,6 +5310,16 @@ type LibraryInfo struct {
|
|||||||
LibVer *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}
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------
|
// -------------------------------------------
|
||||||
|
|
||||||
type InfoCmd struct {
|
type InfoCmd struct {
|
||||||
|
@ -248,7 +248,7 @@ var _ = Describe("Commands", func() {
|
|||||||
|
|
||||||
// Test setting the libName
|
// Test setting the libName
|
||||||
libName := "go-redis"
|
libName := "go-redis"
|
||||||
libInfo := redis.LibraryInfo{LibName: &libName}
|
libInfo := redis.WithLibraryName(libName)
|
||||||
setInfo := pipe.ClientSetInfo(ctx, libInfo)
|
setInfo := pipe.ClientSetInfo(ctx, libInfo)
|
||||||
_, err := pipe.Exec(ctx)
|
_, err := pipe.Exec(ctx)
|
||||||
|
|
||||||
@ -258,7 +258,7 @@ var _ = Describe("Commands", func() {
|
|||||||
|
|
||||||
// Test setting the libVer
|
// Test setting the libVer
|
||||||
libVer := "vX.x"
|
libVer := "vX.x"
|
||||||
libInfo = redis.LibraryInfo{LibVer: &libVer}
|
libInfo = redis.WithLibraryVersion(libVer)
|
||||||
setInfo = pipe.ClientSetInfo(ctx, libInfo)
|
setInfo = pipe.ClientSetInfo(ctx, libInfo)
|
||||||
_, err = pipe.Exec(ctx)
|
_, err = pipe.Exec(ctx)
|
||||||
|
|
||||||
|
@ -154,7 +154,7 @@ func ExampleClient() {
|
|||||||
// missing_key does not exist
|
// missing_key does not exist
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleConn() {
|
func ExampleConn_name() {
|
||||||
conn := rdb.Conn()
|
conn := rdb.Conn()
|
||||||
|
|
||||||
err := conn.ClientSetName(ctx, "foobar").Err()
|
err := conn.ClientSetName(ctx, "foobar").Err()
|
||||||
@ -175,6 +175,28 @@ func ExampleConn() {
|
|||||||
// Output: foobar
|
// 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() {
|
func ExampleClient_Set() {
|
||||||
// Last argument is expiration. Zero means the key has no
|
// Last argument is expiration. Zero means the key has no
|
||||||
// expiration time.
|
// expiration time.
|
||||||
|
7
redis.go
7
redis.go
@ -315,13 +315,12 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
|
|||||||
if !c.opt.DisableIndentity {
|
if !c.opt.DisableIndentity {
|
||||||
libName := ""
|
libName := ""
|
||||||
libVer := Version()
|
libVer := Version()
|
||||||
|
|
||||||
if c.opt.IdentitySuffix != "" {
|
if c.opt.IdentitySuffix != "" {
|
||||||
libName = c.opt.IdentitySuffix
|
libName = c.opt.IdentitySuffix
|
||||||
}
|
}
|
||||||
libInfo := LibraryInfo{LibName: &libName}
|
conn.ClientSetInfo(ctx, WithLibraryName(libName))
|
||||||
conn.ClientSetInfo(ctx, libInfo)
|
conn.ClientSetInfo(ctx, WithLibraryVersion(libVer))
|
||||||
libInfo = LibraryInfo{LibVer: &libVer}
|
|
||||||
conn.ClientSetInfo(ctx, libInfo)
|
|
||||||
}
|
}
|
||||||
_, err := conn.Pipelined(ctx, func(pipe Pipeliner) error {
|
_, err := conn.Pipelined(ctx, func(pipe Pipeliner) error {
|
||||||
if !auth && password != "" {
|
if !auth && password != "" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user