Skip to content

Commit e493a12

Browse files
authored
Merge branch 'master' into xinfo-groups-nil-lag-support
2 parents 1fdbe5b + 03c2c0b commit e493a12

File tree

12 files changed

+607
-41
lines changed

12 files changed

+607
-41
lines changed

error.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
// ErrClosed performs any operation on the closed client will return this error.
1616
var ErrClosed = pool.ErrClosed
1717

18+
// ErrPoolExhausted is returned from a pool connection method
19+
// when the maximum number of database connections in the pool has been reached.
20+
var ErrPoolExhausted = pool.ErrPoolExhausted
21+
22+
// ErrPoolTimeout timed out waiting to get a connection from the connection pool.
23+
var ErrPoolTimeout = pool.ErrPoolTimeout
24+
1825
// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
1926
func HasErrorPrefix(err error, prefix string) bool {
2027
var rErr Error

example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func ExampleMapStringStringCmd_Scan() {
359359
// Get the map. The same approach works for HmGet().
360360
res := rdb.HGetAll(ctx, "map")
361361
if res.Err() != nil {
362-
panic(err)
362+
panic(res.Err())
363363
}
364364

365365
type data struct {
@@ -392,7 +392,7 @@ func ExampleSliceCmd_Scan() {
392392

393393
res := rdb.MGet(ctx, "name", "count", "empty", "correct")
394394
if res.Err() != nil {
395-
panic(err)
395+
panic(res.Err())
396396
}
397397

398398
type data struct {

export_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"github.com/redis/go-redis/v9/internal/pool"
1212
)
1313

14-
var ErrPoolTimeout = pool.ErrPoolTimeout
15-
1614
func (c *baseClient) Pool() pool.Pooler {
1715
return c.connPool
1816
}

internal/pool/pool_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,33 @@ var _ = Describe("race", func() {
387387
Expect(stats.WaitCount).To(Equal(uint32(1)))
388388
Expect(stats.WaitDurationNs).To(BeNumerically("~", time.Second.Nanoseconds(), 100*time.Millisecond.Nanoseconds()))
389389
})
390+
391+
It("timeout", func() {
392+
testPoolTimeout := 1 * time.Second
393+
opt := &pool.Options{
394+
Dialer: func(ctx context.Context) (net.Conn, error) {
395+
// Artificial delay to force pool timeout
396+
time.Sleep(3 * testPoolTimeout)
397+
398+
return &net.TCPConn{}, nil
399+
},
400+
PoolSize: 1,
401+
PoolTimeout: testPoolTimeout,
402+
}
403+
p := pool.NewConnPool(opt)
404+
405+
stats := p.Stats()
406+
Expect(stats.Timeouts).To(Equal(uint32(0)))
407+
408+
conn, err := p.Get(ctx)
409+
Expect(err).NotTo(HaveOccurred())
410+
_, err = p.Get(ctx)
411+
Expect(err).To(MatchError(pool.ErrPoolTimeout))
412+
p.Put(ctx, conn)
413+
conn, err = p.Get(ctx)
414+
Expect(err).NotTo(HaveOccurred())
415+
416+
stats = p.Stats()
417+
Expect(stats.Timeouts).To(Equal(uint32(1)))
418+
})
390419
})

internal/util.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,7 @@ func isLower(s string) bool {
4949
}
5050

5151
func ReplaceSpaces(s string) string {
52-
// Pre-allocate a builder with the same length as s to minimize allocations.
53-
// This is a basic optimization; adjust the initial size based on your use case.
54-
var builder strings.Builder
55-
builder.Grow(len(s))
56-
57-
for _, char := range s {
58-
if char == ' ' {
59-
// Replace space with a hyphen.
60-
builder.WriteRune('-')
61-
} else {
62-
// Copy the character as-is.
63-
builder.WriteRune(char)
64-
}
65-
}
66-
67-
return builder.String()
52+
return strings.ReplaceAll(s, " ", "-")
6853
}
6954

7055
func GetAddr(addr string) string {

internal/util/strconv_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package util
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
func TestAtoi(t *testing.T) {
9+
tests := []struct {
10+
input []byte
11+
expected int
12+
wantErr bool
13+
}{
14+
{[]byte("123"), 123, false},
15+
{[]byte("-456"), -456, false},
16+
{[]byte("abc"), 0, true},
17+
}
18+
19+
for _, tt := range tests {
20+
result, err := Atoi(tt.input)
21+
if (err != nil) != tt.wantErr {
22+
t.Errorf("Atoi(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
23+
}
24+
if result != tt.expected && !tt.wantErr {
25+
t.Errorf("Atoi(%q) = %d, want %d", tt.input, result, tt.expected)
26+
}
27+
}
28+
}
29+
30+
func TestParseInt(t *testing.T) {
31+
tests := []struct {
32+
input []byte
33+
base int
34+
bitSize int
35+
expected int64
36+
wantErr bool
37+
}{
38+
{[]byte("123"), 10, 64, 123, false},
39+
{[]byte("-7F"), 16, 64, -127, false},
40+
{[]byte("zzz"), 36, 64, 46655, false},
41+
{[]byte("invalid"), 10, 64, 0, true},
42+
}
43+
44+
for _, tt := range tests {
45+
result, err := ParseInt(tt.input, tt.base, tt.bitSize)
46+
if (err != nil) != tt.wantErr {
47+
t.Errorf("ParseInt(%q, base=%d) error = %v, wantErr %v", tt.input, tt.base, err, tt.wantErr)
48+
}
49+
if result != tt.expected && !tt.wantErr {
50+
t.Errorf("ParseInt(%q, base=%d) = %d, want %d", tt.input, tt.base, result, tt.expected)
51+
}
52+
}
53+
}
54+
55+
func TestParseUint(t *testing.T) {
56+
tests := []struct {
57+
input []byte
58+
base int
59+
bitSize int
60+
expected uint64
61+
wantErr bool
62+
}{
63+
{[]byte("255"), 10, 8, 255, false},
64+
{[]byte("FF"), 16, 16, 255, false},
65+
{[]byte("-1"), 10, 8, 0, true}, // negative should error for unsigned
66+
}
67+
68+
for _, tt := range tests {
69+
result, err := ParseUint(tt.input, tt.base, tt.bitSize)
70+
if (err != nil) != tt.wantErr {
71+
t.Errorf("ParseUint(%q, base=%d) error = %v, wantErr %v", tt.input, tt.base, err, tt.wantErr)
72+
}
73+
if result != tt.expected && !tt.wantErr {
74+
t.Errorf("ParseUint(%q, base=%d) = %d, want %d", tt.input, tt.base, result, tt.expected)
75+
}
76+
}
77+
}
78+
79+
func TestParseFloat(t *testing.T) {
80+
tests := []struct {
81+
input []byte
82+
bitSize int
83+
expected float64
84+
wantErr bool
85+
}{
86+
{[]byte("3.14"), 64, 3.14, false},
87+
{[]byte("-2.71"), 64, -2.71, false},
88+
{[]byte("NaN"), 64, math.NaN(), false},
89+
{[]byte("invalid"), 64, 0, true},
90+
}
91+
92+
for _, tt := range tests {
93+
result, err := ParseFloat(tt.input, tt.bitSize)
94+
if (err != nil) != tt.wantErr {
95+
t.Errorf("ParseFloat(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
96+
}
97+
if !tt.wantErr && !(math.IsNaN(tt.expected) && math.IsNaN(result)) && result != tt.expected {
98+
t.Errorf("ParseFloat(%q) = %v, want %v", tt.input, result, tt.expected)
99+
}
100+
}
101+
}

internal/util_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package internal
22

33
import (
4+
"runtime"
45
"strings"
56
"testing"
67

@@ -72,3 +73,36 @@ func TestGetAddr(t *testing.T) {
7273
Expect(GetAddr("127")).To(Equal(""))
7374
})
7475
}
76+
77+
func BenchmarkReplaceSpaces(b *testing.B) {
78+
version := runtime.Version()
79+
for i := 0; i < b.N; i++ {
80+
_ = ReplaceSpaces(version)
81+
}
82+
}
83+
84+
func ReplaceSpacesUseBuilder(s string) string {
85+
// Pre-allocate a builder with the same length as s to minimize allocations.
86+
// This is a basic optimization; adjust the initial size based on your use case.
87+
var builder strings.Builder
88+
builder.Grow(len(s))
89+
90+
for _, char := range s {
91+
if char == ' ' {
92+
// Replace space with a hyphen.
93+
builder.WriteRune('-')
94+
} else {
95+
// Copy the character as-is.
96+
builder.WriteRune(char)
97+
}
98+
}
99+
100+
return builder.String()
101+
}
102+
103+
func BenchmarkReplaceSpacesUseBuilder(b *testing.B) {
104+
version := runtime.Version()
105+
for i := 0; i < b.N; i++ {
106+
_ = ReplaceSpacesUseBuilder(version)
107+
}
108+
}

internal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,14 @@ var _ = Describe("ClusterClient", func() {
364364
It("select slot from args for GETKEYSINSLOT command", func() {
365365
cmd := NewStringSliceCmd(ctx, "cluster", "getkeysinslot", 100, 200)
366366

367-
slot := client.cmdSlot(context.Background(), cmd)
367+
slot := client.cmdSlot(cmd)
368368
Expect(slot).To(Equal(100))
369369
})
370370

371371
It("select slot from args for COUNTKEYSINSLOT command", func() {
372372
cmd := NewStringSliceCmd(ctx, "cluster", "countkeysinslot", 100)
373373

374-
slot := client.cmdSlot(context.Background(), cmd)
374+
slot := client.cmdSlot(cmd)
375375
Expect(slot).To(Equal(100))
376376
})
377377
})

osscluster.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ func (c *ClusterClient) Process(ctx context.Context, cmd Cmder) error {
981981
}
982982

983983
func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
984-
slot := c.cmdSlot(ctx, cmd)
984+
slot := c.cmdSlot(cmd)
985985
var node *clusterNode
986986
var moved bool
987987
var ask bool
@@ -1329,7 +1329,7 @@ func (c *ClusterClient) mapCmdsByNode(ctx context.Context, cmdsMap *cmdsMap, cmd
13291329

13301330
if c.opt.ReadOnly && c.cmdsAreReadOnly(ctx, cmds) {
13311331
for _, cmd := range cmds {
1332-
slot := c.cmdSlot(ctx, cmd)
1332+
slot := c.cmdSlot(cmd)
13331333
node, err := c.slotReadOnlyNode(state, slot)
13341334
if err != nil {
13351335
return err
@@ -1340,7 +1340,7 @@ func (c *ClusterClient) mapCmdsByNode(ctx context.Context, cmdsMap *cmdsMap, cmd
13401340
}
13411341

13421342
for _, cmd := range cmds {
1343-
slot := c.cmdSlot(ctx, cmd)
1343+
slot := c.cmdSlot(cmd)
13441344
node, err := state.slotMasterNode(slot)
13451345
if err != nil {
13461346
return err
@@ -1498,7 +1498,7 @@ func (c *ClusterClient) processTxPipeline(ctx context.Context, cmds []Cmder) err
14981498
return err
14991499
}
15001500

1501-
cmdsMap := c.mapCmdsBySlot(ctx, cmds)
1501+
cmdsMap := c.mapCmdsBySlot(cmds)
15021502
for slot, cmds := range cmdsMap {
15031503
node, err := state.slotMasterNode(slot)
15041504
if err != nil {
@@ -1537,10 +1537,10 @@ func (c *ClusterClient) processTxPipeline(ctx context.Context, cmds []Cmder) err
15371537
return cmdsFirstErr(cmds)
15381538
}
15391539

1540-
func (c *ClusterClient) mapCmdsBySlot(ctx context.Context, cmds []Cmder) map[int][]Cmder {
1540+
func (c *ClusterClient) mapCmdsBySlot(cmds []Cmder) map[int][]Cmder {
15411541
cmdsMap := make(map[int][]Cmder)
15421542
for _, cmd := range cmds {
1543-
slot := c.cmdSlot(ctx, cmd)
1543+
slot := c.cmdSlot(cmd)
15441544
cmdsMap[slot] = append(cmdsMap[slot], cmd)
15451545
}
15461546
return cmdsMap
@@ -1569,7 +1569,7 @@ func (c *ClusterClient) processTxPipelineNode(
15691569
}
15701570

15711571
func (c *ClusterClient) processTxPipelineNodeConn(
1572-
ctx context.Context, node *clusterNode, cn *pool.Conn, cmds []Cmder, failedCmds *cmdsMap,
1572+
ctx context.Context, _ *clusterNode, cn *pool.Conn, cmds []Cmder, failedCmds *cmdsMap,
15731573
) error {
15741574
if err := cn.WithWriter(c.context(ctx), c.opt.WriteTimeout, func(wr *proto.Writer) error {
15751575
return writeCmds(wr, cmds)
@@ -1858,7 +1858,7 @@ func (c *ClusterClient) cmdInfo(ctx context.Context, name string) *CommandInfo {
18581858
return info
18591859
}
18601860

1861-
func (c *ClusterClient) cmdSlot(ctx context.Context, cmd Cmder) int {
1861+
func (c *ClusterClient) cmdSlot(cmd Cmder) int {
18621862
args := cmd.Args()
18631863
if args[0] == "cluster" && (args[1] == "getkeysinslot" || args[1] == "countkeysinslot") {
18641864
return args[2].(int)

0 commit comments

Comments
 (0)