Skip to content

Commit

Permalink
Identify client on connect (#2708)
Browse files Browse the repository at this point in the history
* Update CLIENT-SETINFO to support suffixes

* Update CLIENT-SETINFO

* fix acl log test

* add setinfo option to cluster

* change to DisableIndentity

* change to DisableIndentity
  • Loading branch information
ofekshenawa committed Sep 20, 2023
1 parent 54a106e commit 0b6be62
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
3 changes: 3 additions & 0 deletions bench_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewClientStub(resp []byte) *ClientStub {
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
return stub.stubConn(initHello), nil
},
DisableIndentity: true,
})
return stub
}
Expand All @@ -45,6 +46,8 @@ func NewClusterClientStub(resp []byte) *ClientStub {
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
return stub.stubConn(initHello), nil
},
DisableIndentity: true,

ClusterSlots: func(_ context.Context) ([]ClusterSlot, error) {
return []ClusterSlot{
{
Expand Down
21 changes: 11 additions & 10 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ type ClusterOptions struct {
ConnMaxIdleTime time.Duration
ConnMaxLifetime time.Duration

TLSConfig *tls.Config
TLSConfig *tls.Config
DisableIndentity bool // Disable set-lib on connect. Default is false.
}

func (opt *ClusterOptions) init() {
Expand Down Expand Up @@ -277,15 +278,15 @@ func (opt *ClusterOptions) clientOptions() *Options {
ReadTimeout: opt.ReadTimeout,
WriteTimeout: opt.WriteTimeout,

PoolFIFO: opt.PoolFIFO,
PoolSize: opt.PoolSize,
PoolTimeout: opt.PoolTimeout,
MinIdleConns: opt.MinIdleConns,
MaxIdleConns: opt.MaxIdleConns,
ConnMaxIdleTime: opt.ConnMaxIdleTime,
ConnMaxLifetime: opt.ConnMaxLifetime,

TLSConfig: opt.TLSConfig,
PoolFIFO: opt.PoolFIFO,
PoolSize: opt.PoolSize,
PoolTimeout: opt.PoolTimeout,
MinIdleConns: opt.MinIdleConns,
MaxIdleConns: opt.MaxIdleConns,
ConnMaxIdleTime: opt.ConnMaxIdleTime,
ConnMaxLifetime: opt.ConnMaxLifetime,
DisableIndentity: opt.DisableIndentity,
TLSConfig: opt.TLSConfig,
// If ClusterSlots is populated, then we probably have an artificial
// cluster whose nodes are not in clustering mode (otherwise there isn't
// much use for ClusterSlots config). This means we cannot execute the
Expand Down
5 changes: 4 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"encoding"
"errors"
"fmt"
"io"
"net"
"reflect"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -584,7 +586,8 @@ func (c statefulCmdable) ClientSetInfo(ctx context.Context, info LibraryInfo) *S

var cmd *StatusCmd
if info.LibName != nil {
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-NAME", *info.LibName)
libName := fmt.Sprintf("go-redis(%s,%s)", *info.LibName, runtime.Version())
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-NAME", libName)
} else {
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-VER", *info.LibVer)
}
Expand Down
3 changes: 1 addition & 2 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2052,10 +2052,9 @@ var _ = Describe("Commands", func() {

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

for _, entry := range logEntries {
Expect(entry.Count).To(BeNumerically("==", 1))
Expect(entry.Reason).To(Equal("command"))
Expect(entry.Context).To(Equal("toplevel"))
Expect(entry.Object).NotTo(BeEmpty())
Expand Down
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ type Options struct {

// Enables read only queries on slave/follower nodes.
readOnly bool

// // Disable set-lib on connect. Default is false.
DisableIndentity bool
}

func (opt *Options) init() {
Expand Down
9 changes: 8 additions & 1 deletion redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,14 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
// difficult to rely on error strings to determine all results.
return err
}

if !c.opt.DisableIndentity {
libName := ""
libVer := Version()
libInfo := LibraryInfo{LibName: &libName}
conn.ClientSetInfo(ctx, libInfo)
libInfo = LibraryInfo{LibVer: &libVer}
conn.ClientSetInfo(ctx, libInfo)
}
_, err := conn.Pipelined(ctx, func(pipe Pipeliner) error {
if !auth && password != "" {
if username != "" {
Expand Down

0 comments on commit 0b6be62

Please sign in to comment.