Skip to content

Commit 5da49b1

Browse files
authored
bug: Fix SETINFO ensuring it is set-and-forget (#2915)
* Exexcute set-info without validation * Fix tests * Remove spaces from runtime.Version * fix typo * Send setinfo after auth * Add pipline * fix golangci * revert fixing typo * support sentinel
1 parent 99527f0 commit 5da49b1

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

commands.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func (c statefulCmdable) ClientSetInfo(ctx context.Context, info LibraryInfo) *S
309309

310310
var cmd *StatusCmd
311311
if info.LibName != nil {
312-
libName := fmt.Sprintf("go-redis(%s,%s)", *info.LibName, runtime.Version())
312+
libName := fmt.Sprintf("go-redis(%s,%s)", *info.LibName, internal.ReplaceSpaces(runtime.Version()))
313313
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-NAME", libName)
314314
} else {
315315
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-VER", *info.LibVer)

commands_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2105,7 +2105,7 @@ var _ = Describe("Commands", func() {
21052105

21062106
logEntries, err := client.ACLLog(ctx, 10).Result()
21072107
Expect(err).NotTo(HaveOccurred())
2108-
Expect(len(logEntries)).To(Equal(1))
2108+
Expect(len(logEntries)).To(Equal(4))
21092109

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

21222122
limitedLogEntries, err := client.ACLLog(ctx, 2).Result()
21232123
Expect(err).NotTo(HaveOccurred())
2124-
Expect(len(limitedLogEntries)).To(Equal(1))
2124+
Expect(len(limitedLogEntries)).To(Equal(2))
21252125
})
21262126

21272127
It("should ACL LOG RESET", Label("NonRedisEnterprise"), func() {

internal/util.go

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"context"
5+
"strings"
56
"time"
67

78
"github.com/redis/go-redis/v9/internal/util"
@@ -44,3 +45,22 @@ func isLower(s string) bool {
4445
}
4546
return true
4647
}
48+
49+
func ReplaceSpaces(s string) string {
50+
// Pre-allocate a builder with the same length as s to minimize allocations.
51+
// This is a basic optimization; adjust the initial size based on your use case.
52+
var builder strings.Builder
53+
builder.Grow(len(s))
54+
55+
for _, char := range s {
56+
if char == ' ' {
57+
// Replace space with a hyphen.
58+
builder.WriteRune('-')
59+
} else {
60+
// Copy the character as-is.
61+
builder.WriteRune(char)
62+
}
63+
}
64+
65+
return builder.String()
66+
}

redis.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -334,22 +334,24 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
334334
pipe.ClientSetName(ctx, c.opt.ClientName)
335335
}
336336

337-
if !c.opt.DisableIndentity {
338-
libName := ""
339-
libVer := Version()
340-
if c.opt.IdentitySuffix != "" {
341-
libName = c.opt.IdentitySuffix
342-
}
343-
pipe.ClientSetInfo(ctx, WithLibraryName(libName))
344-
pipe.ClientSetInfo(ctx, WithLibraryVersion(libVer))
345-
}
346-
347337
return nil
348338
})
349339
if err != nil {
350340
return err
351341
}
352342

343+
if !c.opt.DisableIndentity {
344+
libName := ""
345+
libVer := Version()
346+
if c.opt.IdentitySuffix != "" {
347+
libName = c.opt.IdentitySuffix
348+
}
349+
p := conn.Pipeline()
350+
p.ClientSetInfo(ctx, WithLibraryName(libName))
351+
p.ClientSetInfo(ctx, WithLibraryVersion(libVer))
352+
_, _ = p.Exec(ctx)
353+
}
354+
353355
if c.opt.OnConnect != nil {
354356
return c.opt.OnConnect(ctx, conn)
355357
}

sentinel.go

+6
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ func (opt *FailoverOptions) sentinelOptions(addr string) *Options {
153153
ConnMaxLifetime: opt.ConnMaxLifetime,
154154

155155
TLSConfig: opt.TLSConfig,
156+
157+
DisableIndentity: opt.DisableIndentity,
158+
IdentitySuffix: opt.IdentitySuffix,
156159
}
157160
}
158161

@@ -190,6 +193,9 @@ func (opt *FailoverOptions) clusterOptions() *ClusterOptions {
190193
ConnMaxLifetime: opt.ConnMaxLifetime,
191194

192195
TLSConfig: opt.TLSConfig,
196+
197+
DisableIndentity: opt.DisableIndentity,
198+
IdentitySuffix: opt.IdentitySuffix,
193199
}
194200
}
195201

0 commit comments

Comments
 (0)