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

bug: Fix SETINFO ensuring it is set-and-forget #2915

Merged
merged 10 commits into from Feb 20, 2024
2 changes: 1 addition & 1 deletion commands.go
Expand Up @@ -309,7 +309,7 @@ func (c statefulCmdable) ClientSetInfo(ctx context.Context, info LibraryInfo) *S

var cmd *StatusCmd
if info.LibName != nil {
libName := fmt.Sprintf("go-redis(%s,%s)", *info.LibName, runtime.Version())
libName := fmt.Sprintf("go-redis(%s,%s)", *info.LibName, internal.ReplaceSpaces(runtime.Version()))
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-NAME", libName)
} else {
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-VER", *info.LibVer)
Expand Down
4 changes: 2 additions & 2 deletions commands_test.go
Expand Up @@ -2105,7 +2105,7 @@ var _ = Describe("Commands", func() {

logEntries, err := client.ACLLog(ctx, 10).Result()
Expect(err).NotTo(HaveOccurred())
Expect(len(logEntries)).To(Equal(1))
Expect(len(logEntries)).To(Equal(4))

for _, entry := range logEntries {
Expect(entry.Reason).To(Equal("command"))
Expand All @@ -2121,7 +2121,7 @@ var _ = Describe("Commands", func() {

limitedLogEntries, err := client.ACLLog(ctx, 2).Result()
Expect(err).NotTo(HaveOccurred())
Expect(len(limitedLogEntries)).To(Equal(1))
Expect(len(limitedLogEntries)).To(Equal(2))
})

It("should ACL LOG RESET", Label("NonRedisEnterprise"), func() {
Expand Down
20 changes: 20 additions & 0 deletions internal/util.go
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"context"
"strings"
"time"

"github.com/redis/go-redis/v9/internal/util"
Expand Down Expand Up @@ -44,3 +45,22 @@ func isLower(s string) bool {
}
return true
}

func ReplaceSpaces(s string) string {
// Pre-allocate a builder with the same length as s to minimize allocations.
// This is a basic optimization; adjust the initial size based on your use case.
var builder strings.Builder
builder.Grow(len(s))

for _, char := range s {
if char == ' ' {
// Replace space with a hyphen.
builder.WriteRune('-')
} else {
// Copy the character as-is.
builder.WriteRune(char)
}
}

return builder.String()
}
20 changes: 10 additions & 10 deletions redis.go
Expand Up @@ -313,6 +313,16 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
return err
}

if !c.opt.DisableIndentity {
ofekshenawa marked this conversation as resolved.
Show resolved Hide resolved
libName := ""
libVer := Version()
if c.opt.IdentitySuffix != "" {
libName = c.opt.IdentitySuffix
}
conn.ClientSetInfo(ctx, WithLibraryName(libName))
conn.ClientSetInfo(ctx, WithLibraryVersion(libVer))
}

_, err := conn.Pipelined(ctx, func(pipe Pipeliner) error {
if !auth && password != "" {
if username != "" {
Expand All @@ -334,16 +344,6 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
pipe.ClientSetName(ctx, c.opt.ClientName)
}

if !c.opt.DisableIndentity {
libName := ""
libVer := Version()
if c.opt.IdentitySuffix != "" {
libName = c.opt.IdentitySuffix
}
pipe.ClientSetInfo(ctx, WithLibraryName(libName))
pipe.ClientSetInfo(ctx, WithLibraryVersion(libVer))
}

return nil
})
if err != nil {
Expand Down