Skip to content

Commit

Permalink
Disable client setinfo
Browse files Browse the repository at this point in the history
Add DisableClientSetInfo path and test

Co-authored-by: Rahul Katre <[email protected]>
  • Loading branch information
rahulgkatre and Rahul Katre committed Aug 29, 2024
1 parent 9a347d7 commit 19662d8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
31 changes: 27 additions & 4 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,14 @@ func _newPipe(connFn func() (net.Conn, error), option *ClientOption, r2ps, nobg
if option.ClientNoEvict {
init = append(init, []string{"CLIENT", "NO-EVICT", "ON"})
}

addClientSetInfoCmds := true
if len(option.ClientSetInfo) == 2 {
init = append(init, []string{"CLIENT", "SETINFO", "LIB-NAME", option.ClientSetInfo[0]}, []string{"CLIENT", "SETINFO", "LIB-VER", option.ClientSetInfo[1]})
} else {
} else if option.ClientSetInfo == nil {
init = append(init, []string{"CLIENT", "SETINFO", "LIB-NAME", LibName}, []string{"CLIENT", "SETINFO", "LIB-VER", LibVer})
} else {
addClientSetInfoCmds = false
}

timeout := option.Dialer.Timeout
Expand All @@ -186,7 +190,14 @@ func _newPipe(connFn func() (net.Conn, error), option *ClientOption, r2ps, nobg
if !r2 && !r2ps {
resp := p.DoMulti(ctx, cmds.NewMultiCompleted(init)...)
defer resultsp.Put(resp)
for i, r := range resp.s[:len(resp.s)-2] { // skip error checking on the last CLIENT SETINFO

count := len(resp.s)
if addClientSetInfoCmds {
// skip error checking on the last CLIENT SETINFO
count -= 2
}

for i, r := range resp.s[:count] {
if i == 0 {
p.info, err = r.AsMap()
} else {
Expand Down Expand Up @@ -249,16 +260,28 @@ func _newPipe(connFn func() (net.Conn, error), option *ClientOption, r2ps, nobg
if option.ClientNoEvict {
init = append(init, []string{"CLIENT", "NO-EVICT", "ON"})
}

addClientSetInfoCmds := true
if len(option.ClientSetInfo) == 2 {
init = append(init, []string{"CLIENT", "SETINFO", "LIB-NAME", option.ClientSetInfo[0]}, []string{"CLIENT", "SETINFO", "LIB-VER", option.ClientSetInfo[1]})
} else {
} else if option.ClientSetInfo == nil {
init = append(init, []string{"CLIENT", "SETINFO", "LIB-NAME", LibName}, []string{"CLIENT", "SETINFO", "LIB-VER", LibVer})
} else {
addClientSetInfoCmds = false
}

p.version = 5
if len(init) != 0 {
resp := p.DoMulti(ctx, cmds.NewMultiCompleted(init)...)
defer resultsp.Put(resp)
for i, r := range resp.s[:len(resp.s)-2] { // skip error checking on the last CLIENT SETINFO

count := len(resp.s)
if addClientSetInfoCmds {
// skip error checking on the last CLIENT SETINFO
count -= 2
}

for i, r := range resp.s[:len(resp.s)-2] {
if init[i][0] == "READONLY" {
// ignore READONLY command error
continue
Expand Down
30 changes: 30 additions & 0 deletions pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,36 @@ func TestNewPipe(t *testing.T) {
n1.Close()
n2.Close()
})
t.Run("With DisableClientSetInfo", func(t *testing.T) {
n1, n2 := net.Pipe()
mock := &redisMock{buf: bufio.NewReader(n2), conn: n2, t: t}
go func() {
mock.Expect("HELLO", "3").
Reply(RedisMessage{
typ: '%',
values: []RedisMessage{
{typ: '+', string: "proto"},
{typ: ':', integer: 3},
},
})
mock.Expect("CLIENT", "TRACKING", "ON", "OPTIN").
ReplyString("OK")
}()
p, err := newPipe(func() (net.Conn, error) { return n1, nil }, &ClientOption{
ClientSetInfo: DisableClientSetInfo,
})
go func() {
mock.Expect("PING").
ReplyString("OK")
}()
if err != nil {
t.Fatalf("pipe setup failed: %v", err)
}
p.Close()
mock.Close()
n1.Close()
n2.Close()
})
}

func TestNewRESP2Pipe(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions rueidis.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var (
ErrWrongPipelineMultiplex = errors.New("ClientOption.PipelineMultiplex must not be bigger than MaxPipelineMultiplex")
// ErrDedicatedClientRecycled means the caller attempted to use the dedicated client which has been already recycled (after canceled/closed).
ErrDedicatedClientRecycled = errors.New("dedicated client should not be used after recycled")
// DisableClientSetInfo is the value that can be used for ClientOption.ClientSetInfo to disable making the CLIENT SETINFO command
DisableClientSetInfo = make([]string, 0)
)

// ClientOption should be passed to NewClient to construct a Client
Expand Down

0 comments on commit 19662d8

Please sign in to comment.